-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional -license.year
flag
#46
Changes from 1 commit
74015bf
74d5dae
e9bce36
7716d08
89c29b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,12 @@ | |
package license | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
@@ -25,11 +28,12 @@ var EEAnalyzer = &analysis.Analyzer{ | |
|
||
var ( | ||
ignoreFilesPattern string | ||
licenseYear string | ||
sourceAvailablePackageRe = regexp.MustCompile("/enterprise") | ||
) | ||
|
||
const ( | ||
defaultLicenseLine1 = "// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved." | ||
licenseLine1Format = "// Copyright (c) %d-present Mattermost, Inc. All Rights Reserved." | ||
defaultLicenseLine2 = "// See LICENSE.txt for license information." | ||
enterpriseLicenseLine2 = "// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information." | ||
sourceAvailableLicenseLine2 = "// See LICENSE.enterprise for license information." | ||
|
@@ -51,7 +55,9 @@ var generatedHeaders = []string{ | |
|
||
func init() { | ||
Analyzer.Flags.StringVar(&ignoreFilesPattern, "ignore", "", "Comma separated list of files to ignore") | ||
Analyzer.Flags.StringVar(&licenseYear, "year", "2015", "Year to use in license header (2015-present)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only ever really used as an integer, so could we parse it as one rather than a string that is later converted? I think this would also mean error checking happens automatically |
||
EEAnalyzer.Flags.StringVar(&ignoreFilesPattern, "ignore", "", "Comma separated list of files to ignore") | ||
EEAnalyzer.Flags.StringVar(&licenseYear, "year", "2015", "Year to use in license header (2015-present)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
|
@@ -60,7 +66,21 @@ func run(pass *analysis.Pass) (interface{}, error) { | |
ignoreFiles = strings.Split(ignoreFilesPattern, ",") | ||
} | ||
|
||
expectedLine1 := defaultLicenseLine1 | ||
// Validate license year | ||
year := 2015 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could make this a package-level constant. |
||
if licenseYear != "" { | ||
if y, err := strconv.Atoi(licenseYear); err != nil { | ||
return nil, fmt.Errorf("invalid license year: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd generally use |
||
} else { | ||
currentYear := time.Now().Year() | ||
if y < 2015 || y > currentYear { | ||
return nil, fmt.Errorf("license year must be between 2015 and %d", currentYear) | ||
} | ||
year = y | ||
} | ||
} | ||
|
||
expectedLine1 := fmt.Sprintf(licenseLine1Format, year) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the above change, this would simplify down to
Perhaps a good idea to use a constant for the |
||
expectedLine2 := defaultLicenseLine2 | ||
|
||
if pass.Analyzer.Name == "enterpriseLicense" { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information. | ||
|
||
package year2015 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved. | ||
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information. | ||
|
||
package year2024 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved. | ||
// See ENTERPRISE-LICENSE.txt and SOURCE-CODE-LICENSE.txt for license information. | ||
|
||
package yearcurrent | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.enterprise for license information. | ||
|
||
package year2015 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.enterprise for license information. | ||
|
||
package year2024 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.enterprise for license information. | ||
|
||
package yearcurrent | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package year2015 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2024-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package year2024 | ||
|
||
func Test() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) 2025-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
package yearcurrent | ||
|
||
func Test() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for the year to be parsed as string vs integer?