Skip to content

Commit

Permalink
Adding more golang checks
Browse files Browse the repository at this point in the history
  • Loading branch information
TP Honey committed Jun 27, 2022
1 parent 05e89ff commit a6048a6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"mode": "auto",
"program": "${workspaceFolder}",
"env": {
"PLUGIN_WORKING_DIRECTORY": "/home/tp/workspace/drone-snyk",
"PLUGIN_WORKING_DIRECTORY": "/home/tp/workspace/drone",
},
},
]
Expand Down
41 changes: 41 additions & 0 deletions scanner/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package golang

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/tphoney/best_practice/outputter/bestpractice"
"github.com/tphoney/best_practice/outputter/dronebuild"
"github.com/tphoney/best_practice/scanner"
"github.com/tphoney/best_practice/types"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -74,6 +77,12 @@ func (sc *scannerConfig) Scan(ctx context.Context, requestedOutputs []string) (r
}
}
// find the main.go file
if sc.runAll || slices.Contains(requestedOutputs, "main") {
match, mainResult := sc.mainCheck()
if match {
returnVal = append(returnVal, mainResult...)
}
}
// find test files
return returnVal, nil
}
Expand Down Expand Up @@ -144,3 +153,35 @@ func (sc *scannerConfig) lintCheck() (match bool, outputResults []types.Scanlet)
}
return false, outputResults
}

func (sc *scannerConfig) mainCheck() (match bool, outputResults []types.Scanlet) {
matches, err := scanner.WalkMatch(sc.workingDirectory, "main.go")
if err == nil && len(matches) > 0 {
// we use the first one found
mainLocation := strings.TrimPrefix(matches[0], sc.workingDirectory)
mainLocation = strings.TrimSuffix(mainLocation, "main.go")
if len(mainLocation) == 1 {
// dont do anything if main is in the working directory
mainLocation = ""
} else {
mainLocation = "." + mainLocation
}

droneBuildResult := types.Scanlet{
Name: LintCheckName,
ScannerFamily: Name,
Description: "run go build",
OutputRenderer: dronebuild.Name,
Spec: dronebuild.OutputFields{
RawYaml: fmt.Sprintf(
` - name: build
image: golang:1
commands:
- go build %s`, mainLocation),
},
}
outputResults = append(outputResults, droneBuildResult)
return true, outputResults
}
return false, outputResults
}
28 changes: 28 additions & 0 deletions scanner/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package scanner

import (
"os"
"path/filepath"
)

func WalkMatch(workingDir, pattern string) ([]string, error) {
var matches []string
err := filepath.Walk(workingDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}

0 comments on commit a6048a6

Please sign in to comment.