Skip to content

Commit

Permalink
feat(govulncheck): add helper for checking all modules in repo
Browse files Browse the repository at this point in the history
Including logging helper that ensures that the current module is a
appended as a prefix to the log output.
  • Loading branch information
odsod committed Jul 23, 2023
1 parent a65be97 commit bc9e592
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
6 changes: 6 additions & 0 deletions sg/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func WithLogger(ctx context.Context, logger *log.Logger) context.Context {
return context.WithValue(ctx, loggerContextKey{}, logger)
}

// AppendLoggerPrefix appends a prefix to the current logger.
func AppendLoggerPrefix(ctx context.Context, prefix string) context.Context {
logger := Logger(ctx)
return WithLogger(ctx, log.New(logger.Writer(), logger.Prefix()+prefix, logger.Flags()))
}

// Logger returns the log.Logger attached to ctx, or a default logger.
func Logger(ctx context.Context) *log.Logger {
if value := ctx.Value(loggerContextKey{}); value != nil {
Expand Down
56 changes: 50 additions & 6 deletions tools/sggovulncheck/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,71 @@ package sggovulncheck

import (
"context"
"fmt"
"io/fs"
"os/exec"
"path/filepath"

"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
)

const (
name = "govulncheck"
version = "latest"
version = "v1.0.0"
)

// Command returns an [*exec.Cmd] for govulncheck.
func Command(ctx context.Context, args ...string) *exec.Cmd {
sg.Deps(ctx, PrepareCommand)
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

// RunAll runs govulncheck on all the specified module paths, or all module paths from the current git root by default.
func RunAll(ctx context.Context, modulePaths ...string) error {
var commands []*exec.Cmd
if err := filepath.WalkDir(sg.FromGitRoot(), func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || d.Name() != "go.mod" {
return nil
}
if len(modulePaths) > 0 {
var shouldRunInModulePath bool
for _, modulePath := range modulePaths {
if modulePath == path {
shouldRunInModulePath = true
break
}
}
if !shouldRunInModulePath {
return nil
}
}
relativePath, err := filepath.Rel(sg.FromGitRoot(), path)
if err != nil {
return err
}
cmd := Command(
sg.AppendLoggerPrefix(ctx, fmt.Sprintf(" (%s) ", relativePath)),
"-C",
filepath.Dir(path), "./...",

Check failure on line 54 in tools/sggovulncheck/tools.go

View workflow job for this annotation

GitHub Actions / build

each argument should start on a new line
)
commands = append(commands, cmd)
return cmd.Start()
}); err != nil {
return err
}
for _, cmd := range commands {
if err := cmd.Wait(); err != nil {
return err
}
}
return nil
}

func PrepareCommand(ctx context.Context) error {
_, err := sgtool.GoInstall(
ctx,
"golang.org/x/vuln/cmd/govulncheck",
version,
)
_, err := sgtool.GoInstall(ctx, "golang.org/x/vuln/cmd/govulncheck", version)
return err
}

0 comments on commit bc9e592

Please sign in to comment.