From 783af3bd50cebe19210bbcd61992e2f2353f3205 Mon Sep 17 00:00:00 2001 From: mcoops Date: Sat, 11 Sep 2021 02:17:48 +1000 Subject: [PATCH] golint: minor fixes (#36) --- deplist.go | 1 + internal/scan/golang.go | 15 ++++++++++----- internal/scan/jar.go | 1 + internal/scan/maven.go | 4 +++- internal/scan/nodejs.go | 4 ++++ internal/scan/python.go | 3 ++- internal/scan/ruby.go | 12 +++++++----- internal/utils/utils.go | 2 +- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/deplist.go b/deplist.go index ff2de32..6ae0b1a 100644 --- a/deplist.go +++ b/deplist.go @@ -43,6 +43,7 @@ func init() { } } +// GetLanguageStr returns from a bitmask return the ecosystem name func GetLanguageStr(bm Bitmask) string { if bm&LangGolang != 0 { return "go" diff --git a/internal/scan/golang.go b/internal/scan/golang.go index 65f1759..72933dd 100644 --- a/internal/scan/golang.go +++ b/internal/scan/golang.go @@ -14,6 +14,8 @@ import ( "golang.org/x/mod/semver" ) +// GoListDeps holds the import path, version and gofiles for a given go +// dependency type GoListDeps struct { ImportPath string `json:"ImportPath"` Module struct { @@ -25,6 +27,7 @@ type GoListDeps struct { GoFiles []string `json:"GoFiles"` } +// GoPkg holds the version and go paths/files for a given dep type GoPkg struct { Version string Gofiles []string @@ -32,7 +35,7 @@ type GoPkg struct { func getVersion(deps GoListDeps) string { /* if replace is specified, then use that version - * not seen when version and replace.version are differnt + * not seen when version and replace.version are different * but just in case */ if deps.Module.Replace.Version != "" { @@ -75,10 +78,10 @@ func runCmd(path string, mod bool) ([]byte, error) { if !mod { // assume some retrival error, we have to redo the cmd with mod=vendor return nil, err - } else { - if len(out) == 0 { - return nil, err - } + } + + if len(out) == 0 { + return nil, err } } @@ -102,6 +105,8 @@ func runGoList(path string) ([]byte, error) { return out, nil } +// GetGolangDeps uses `go list` gather a list of dependencies located at `path` +// returning an array of `GoPkg` structs func GetGolangDeps(path string) (map[string]GoPkg, error) { // need to use a map as we'll get lots of duplicate entries gathered := make(map[string]GoPkg) diff --git a/internal/scan/jar.go b/internal/scan/jar.go index 05ee37c..37964c2 100755 --- a/internal/scan/jar.go +++ b/internal/scan/jar.go @@ -6,6 +6,7 @@ import ( "github.com/mcoops/jargo" ) +// GetJarDeps uses github.com/mcoops/jargo retrieve the java dependencies func GetJarDeps(path string) (map[string]string, error) { gathered := make(map[string]string) diff --git a/internal/scan/maven.go b/internal/scan/maven.go index e82ff7b..ab8b727 100644 --- a/internal/scan/maven.go +++ b/internal/scan/maven.go @@ -6,6 +6,8 @@ import ( "strings" ) +// GetMvnDeps uses the mvn command to attempt to list the dependencies for a +// given project located at `path` func GetMvnDeps(path string) (map[string]string, error) { var gathered map[string]string var found map[string]bool @@ -27,7 +29,7 @@ func GetMvnDeps(path string) (map[string]string, error) { "-DincludeScope=runtime") cmd.Dir = dirPath - // supress error, it always returns errors + // suppress error, it always returns errors data, _ := cmd.Output() res := strings.Split(string(data), "\n") diff --git a/internal/scan/nodejs.go b/internal/scan/nodejs.go index caf81dc..d0f02c2 100644 --- a/internal/scan/nodejs.go +++ b/internal/scan/nodejs.go @@ -31,6 +31,7 @@ type npmListOutput struct { Dependencies map[string]npmDependency `json:"dependencies"` } +// NodeJSGather dependencies found, name and version type NodeJSGather struct { Name string Version string @@ -86,6 +87,9 @@ func gatherNPMNode(name string, dependency npmDependency) { } } +// GetNodeJSDeps scans the path for either `yarn.lock` or `package-lock.json`, +// then use the appropriate pkg managers to produce depencies lists of type +// `NodeJSGather` func GetNodeJSDeps(path string) (map[string]NodeJSGather, error) { switch filepath.Base(path) { case "yarn.lock": diff --git a/internal/scan/python.go b/internal/scan/python.go index 4812c26..09f211a 100755 --- a/internal/scan/python.go +++ b/internal/scan/python.go @@ -10,6 +10,7 @@ import ( // Account for >, <, >=, <=, ==, !=, ~= and * var /* const */ re = regexp.MustCompile(`[<>!~*]+`) +// GetPythonDeps scans path for python deps using the `requirements.txt` file func GetPythonDeps(path string) (map[string]string, error) { gathered := make(map[string]string) @@ -40,7 +41,7 @@ func GetPythonDeps(path string) (map[string]string, error) { } // every other permitation just use the name as we can't guarantee - // the version, just grab the name using first occurance + // the version, just grab the name using first occurrence match := re.FindStringIndex(line) if match != nil { diff --git a/internal/scan/ruby.go b/internal/scan/ruby.go index 4462f9b..e206663 100644 --- a/internal/scan/ruby.go +++ b/internal/scan/ruby.go @@ -8,25 +8,27 @@ import ( "strings" ) +// GetRubyDeps uses `bundle update --bundler` to list ruby dependencies when a +// Gemfile.lock file exists func GetRubyDeps(path string) (map[string]string, error) { gathered := make(map[string]string) dirPath := filepath.Dir(path) // override the gem path otherwise might hit perm issues and it's annoying - gem_path, err := os.MkdirTemp("", "gem_vendor") + gemPath, err := os.MkdirTemp("", "gem_vendor") if err != nil { return nil, err } // cleanup after ourselves - defer os.RemoveAll(gem_path) + defer os.RemoveAll(gemPath) //Make sure that the Gemfile we are loading is supported by the version of bundle currently installed. cmd := exec.Command("bundle", "update", "--bundler") cmd.Dir = dirPath cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "BUNDLE_PATH="+gem_path) + cmd.Env = append(cmd.Env, "BUNDLE_PATH="+gemPath) _, err = cmd.Output() if err != nil { return nil, err @@ -36,11 +38,11 @@ func GetRubyDeps(path string) (map[string]string, error) { cmd.Dir = dirPath cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "BUNDLE_PATH="+gem_path) + cmd.Env = append(cmd.Env, "BUNDLE_PATH="+gemPath) data, err := cmd.Output() if err != nil { - return nil, errors.New(gem_path + " " + err.Error()) + return nil, errors.New(gemPath + " " + err.Error()) } splitOutput := strings.Split(string(data), "\n") diff --git a/internal/utils/utils.go b/internal/utils/utils.go index eddfa99..b049666 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -1,6 +1,6 @@ package utils -// fastest way we can do a string compare on a list +// BelongsToIgnoreList is fastest way we can do a string compare on a list func BelongsToIgnoreList(needle string) bool { switch needle { case