header fix: respect user configured license content (#60)
diff --git a/.golangci.yml b/.golangci.yml
index d96a69f..ef39333 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -22,7 +22,7 @@
linters-settings:
govet:
check-shadowing: true
- golint:
+ revive:
min-confidence: 0
gocyclo:
min-complexity: 20
@@ -106,12 +106,11 @@
- gocyclo
- gofmt
- goimports
- - golint
+ - revive
- gosec
- gosimple
- govet
- ineffassign
- - interfacer
- lll
- misspell
- nakedret
diff --git a/commands/root.go b/commands/root.go
index fbe6b01..361ce49 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -44,11 +44,7 @@
}
logger.Log.SetLevel(level)
- if err := Config.Parse(configFile); err != nil {
- return err
- }
-
- return nil
+ return Config.Parse(configFile)
},
Version: version,
}
diff --git a/pkg/config/config.go b/pkg/config/config.go
index afd8578..97d9df2 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -60,9 +60,5 @@
return err
}
- if err := config.Deps.Finalize(file); err != nil {
- return err
- }
-
- return nil
+ return config.Deps.Finalize(file)
}
diff --git a/pkg/deps/golang.go b/pkg/deps/golang.go
index 13a04d5..5f2ac5a 100644
--- a/pkg/deps/golang.go
+++ b/pkg/deps/golang.go
@@ -79,11 +79,7 @@
logger.Log.Debugln("Module size:", len(modules))
- if err := resolver.ResolvePackages(modules, report); err != nil {
- return err
- }
-
- return nil
+ return resolver.ResolvePackages(modules, report)
}
// ResolvePackages resolves the licenses of the given packages.
diff --git a/pkg/deps/maven.go b/pkg/deps/maven.go
index 7f9d05c..868732b 100644
--- a/pkg/deps/maven.go
+++ b/pkg/deps/maven.go
@@ -70,11 +70,7 @@
}
}
- if err := resolver.ResolveDependencies(deps, report); err != nil {
- return err
- }
-
- return nil
+ return resolver.ResolveDependencies(deps, report)
}
// CheckMVN check available maven tools, find local repositories and download all dependencies
@@ -205,7 +201,7 @@
if err != nil {
return nil, err
}
- defer file.Close()
+ defer func() { _ = file.Close() }()
dec := xml.NewDecoder(file)
dec.CharsetReader = charset.NewReaderLabel
@@ -224,7 +220,7 @@
if err != nil {
return "", err
}
- defer file.Close()
+ defer func() { _ = file.Close() }()
var comments string
@@ -300,7 +296,7 @@
stack := []Elem{}
unique := make(map[string]struct{})
- reFind := regexp.MustCompile(`(?im)^.*? ([\| ]*)(\+\-|\\\-) (\b.+):(\b.+):(\b.+):(\b.+):(\b.+)$`)
+ reFind := regexp.MustCompile(`(?im)^.*? ([| ]*)(\+-|\\-) (\b.+):(\b.+):(\b.+):(\b.+):(\b.+)$`)
rawDeps := reFind.FindAllSubmatch(data, -1)
deps := make([]*Dependency, 0, len(rawDeps))
@@ -436,7 +432,7 @@
}
}
- w.Flush()
+ _ = w.Flush()
return buf.String()
}
diff --git a/pkg/deps/maven_test.go b/pkg/deps/maven_test.go
index 906f7a3..b5b6881 100644
--- a/pkg/deps/maven_test.go
+++ b/pkg/deps/maven_test.go
@@ -22,9 +22,11 @@
"fmt"
"io/ioutil"
"os"
+ "os/exec"
"path/filepath"
"testing"
+ "github.com/apache/skywalking-eyes/license-eye/internal/logger"
"github.com/apache/skywalking-eyes/license-eye/pkg/deps"
)
@@ -51,7 +53,7 @@
if err != nil {
return err
}
- defer file.Close()
+ defer func() { _ = file.Close() }()
write := bufio.NewWriter(file)
_, err = write.WriteString(content)
@@ -59,7 +61,7 @@
return err
}
- write.Flush()
+ _ = write.Flush()
return nil
}
@@ -84,6 +86,11 @@
}
func TestResolveMaven(t *testing.T) {
+ if _, err := exec.Command("mvn", "--version").Output(); err != nil {
+ logger.Log.Warnf("Failed to find mvn, the test `TestResolveMaven` was skipped")
+ return
+ }
+
resolver := new(deps.MavenPomResolver)
path, err := tmpDir()
@@ -130,7 +137,7 @@
</dependencies>
</project>`, 107},
} {
- dumpPomFile(pomFile, test.pomContent)
+ _ = dumpPomFile(pomFile, test.pomContent)
if resolver.CanResolve(pomFile) {
report := deps.Report{}
diff --git a/pkg/header/check.go b/pkg/header/check.go
index e10b318..5498f01 100644
--- a/pkg/header/check.go
+++ b/pkg/header/check.go
@@ -99,10 +99,7 @@
if p == path { // when p is symbolic link file, it causes infinite recursive calls
return nil
}
- if err := checkPath(p, result, config); err != nil {
- return err
- }
- return nil
+ return checkPath(p, result, config)
}); err != nil {
return err
}
diff --git a/pkg/header/config.go b/pkg/header/config.go
index 6abb94a..900b726 100644
--- a/pkg/header/config.go
+++ b/pkg/header/config.go
@@ -133,7 +133,7 @@
func (config *ConfigHeader) GetLicenseContent() string {
if c := strings.TrimSpace(config.License.Content); c != "" {
- return c
+ return config.License.Content // Do not change anything in user config
}
c, err := readLicenseFromSpdx(config)
if err != nil {
diff --git a/pkg/header/fix.go b/pkg/header/fix.go
index 2cccf28..4e3d9a6 100644
--- a/pkg/header/fix.go
+++ b/pkg/header/fix.go
@@ -44,11 +44,7 @@
return fmt.Errorf("unsupported file: %v", file)
}
- if err := InsertComment(file, style, config, result); err != nil {
- return err
- }
-
- return nil
+ return InsertComment(file, style, config, result)
}
func InsertComment(file string, style *comments.CommentStyle, config *ConfigHeader, result *Result) error {
@@ -106,15 +102,22 @@
return "", err
}
- middleLines := strings.Split(config.GetLicenseContent(), "\n")
- for i, line := range middleLines {
- middleLines[i] = strings.TrimRight(fmt.Sprintf("%v %v", style.Middle, line), " ")
+ lines := strings.Split(config.GetLicenseContent(), "\n")
+ for i, line := range lines {
+ if line != "" {
+ lines[i] = fmt.Sprintf("%v %v", style.Middle, line)
+ } else {
+ lines[i] = style.Middle
+ }
}
- lines := fmt.Sprintf("%v\n%v\n", style.Start, strings.Join(middleLines, "\n"))
+ if style.Start != style.Middle {
+ lines = append([]string{style.Start}, lines...)
+ }
+
if style.End != style.Middle {
- lines += style.End
+ lines = append(lines, style.End)
}
- return strings.TrimSpace(lines) + "\n", nil
+ return strings.Join(lines, "\n") + "\n", nil
}
diff --git a/pkg/header/fix_test.go b/pkg/header/fix_test.go
index 5ab9503..e8dce75 100644
--- a/pkg/header/fix_test.go
+++ b/pkg/header/fix_test.go
@@ -49,8 +49,7 @@
},
{
filename: "Test.py",
- comments: `#
-# Apache License 2.0
+ comments: `# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
`,
@@ -84,8 +83,7 @@
content: `print_string "hello worlds!\n";;
`,
licenseHeader: getLicenseHeader("test.ml", t.Error),
- expectedContent: `(*
-(* Apache License 2.0
+ expectedContent: `(* Apache License 2.0
(* http://www.apache.org/licenses/LICENSE-2.0
(* Apache License 2.0
print_string "hello worlds!\n";;
@@ -93,14 +91,12 @@
{
name: "Python with Shebang",
style: comments.FileCommentStyle("test.py"),
- content: `
-#!/usr/bin/env python3
+ content: `#!/usr/bin/env python3
if __name__ == '__main__':
print('Hello World')
`,
licenseHeader: getLicenseHeader("test.py", t.Error),
expectedContent: `#!/usr/bin/env python3
-#
# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
@@ -110,19 +106,40 @@
{
name: "Python",
style: comments.FileCommentStyle("test.py"),
- content: `
-if __name__ == '__main__':
+ content: `if __name__ == '__main__':
print('Hello World')
`,
licenseHeader: getLicenseHeader("test.py", t.Error),
- expectedContent: `#
-# Apache License 2.0
+ expectedContent: `# Apache License 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Apache License 2.0
if __name__ == '__main__':
print('Hello World')
`},
{
+ name: "Python with Blank Line",
+ style: comments.FileCommentStyle("test.py"),
+ content: `if __name__ == '__main__':
+ print('Hello World')
+`,
+ licenseHeader: getLicenseHeaderCustomConfig("test.py", t.Error, &ConfigHeader{
+ License: LicenseConfig{
+ Content: `Apache License 2.0
+ http://www.apache.org/licenses/LICENSE-2.0
+Apache License 2.0
+
+`,
+ },
+ }),
+ expectedContent: `# Apache License 2.0
+# http://www.apache.org/licenses/LICENSE-2.0
+# Apache License 2.0
+#
+#
+if __name__ == '__main__':
+ print('Hello World')
+`},
+ {
name: "XML one line declaration",
style: comments.FileCommentStyle("test.xml"),
content: `
@@ -173,8 +190,7 @@
style: comments.FileCommentStyle("test.sql"),
content: `select * from user;`,
licenseHeader: getLicenseHeader("test.sql", t.Error),
- expectedContent: `--
--- Apache License 2.0
+ expectedContent: `-- Apache License 2.0
-- http://www.apache.org/licenses/LICENSE-2.0
-- Apache License 2.0
select * from user;`},
@@ -195,8 +211,7 @@
content: `echo 'Hello' | echo 'world!'
`,
licenseHeader: getLicenseHeader("test.vim", t.Error),
- expectedContent: `"
-" Apache License 2.0
+ expectedContent: `" Apache License 2.0
" http://www.apache.org/licenses/LICENSE-2.0
" Apache License 2.0
echo 'Hello' | echo 'world!'
@@ -319,3 +334,11 @@
}
return s
}
+
+func getLicenseHeaderCustomConfig(filename string, tError func(args ...interface{}), c *ConfigHeader) string {
+ s, err := GenerateLicenseHeader(comments.FileCommentStyle(filename), c)
+ if err != nil {
+ tError(err)
+ }
+ return s
+}
diff --git a/pkg/review/header.go b/pkg/review/header.go
index a60660e..048eceb 100644
--- a/pkg/review/header.go
+++ b/pkg/review/header.go
@@ -169,11 +169,7 @@
}
}
- if err := tryReview(result, config, comments); err != nil {
- return err
- }
-
- return nil
+ return tryReview(result, config, comments)
}
func tryReview(result *header2.Result, config *config2.Config, comments []*github.DraftReviewComment) error {