Skip to content

Commit

Permalink
[go] Allow overriding the Go package build command
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Weichel committed Jan 5, 2021
1 parent 98d7cf9 commit 42ce465
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ config:
dontCheckGoFmt: false
# If true disables the linting stage.
dontLint: false
# A list of flags passed to `go build`. Useful for passing `ldflags`.
# Overrides the `go build .` command. Supersedes buildFlags.
buildCommand: []
# [DEPRECATED: use buildCommand instead] A list of flags passed to `go build`. Useful for passing `ldflags`.
buildFlags: []
# Command that's executed to lint the code
lintCommand: ["golangci-lint", "run]
Expand Down
16 changes: 11 additions & 5 deletions pkg/leeway/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,11 +901,17 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (err error)
{"go", "test", "-v", "./..."},
}...)
}
if cfg.Packaging == GoApp {
cmd := []string{"go", "build"}
cmd = append(cmd, cfg.BuildFlags...)
cmd = append(cmd, ".")
commands = append(commands, cmd)

var buildCmd []string
if len(cfg.BuildCommand) > 0 {
buildCmd = cfg.BuildCommand
} else if cfg.Packaging == GoApp {
buildCmd = []string{"go", "build"}
buildCmd = append(buildCmd, cfg.BuildFlags...)
buildCmd = append(buildCmd, ".")
}
if len(buildCmd) > 0 {
commands = append(commands, buildCmd)
}
commands = append(commands, [][]string{
{"rm", "-rf", "_deps"},
Expand Down
5 changes: 5 additions & 0 deletions pkg/leeway/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ type GoPkgConfig struct {
DontCheckGoFmt bool `yaml:"dontCheckGoFmt,omitempty"`
DontLint bool `yaml:"dontLint,omitempty"`
BuildFlags []string `yaml:"buildFlags,omitempty"`
BuildCommand []string `yaml:"buildCommand,omitempty"`
LintCommand []string `yaml:"lintCommand,omitempty"`
}

Expand All @@ -420,6 +421,10 @@ func (cfg GoPkgConfig) Validate() error {
return xerrors.Errorf("unknown packaging: %s", cfg.Packaging)
}

if len(cfg.BuildCommand) != 0 && len(cfg.BuildFlags) > 0 {
return xerrors.Errorf("buildCommand and buildFlags are exclusive - use one or the other")
}

return nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/vet/golang.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package vet

import (
"fmt"
"strings"

"github.com/typefox/leeway/pkg/leeway"
)

func init() {
register(PackageCheck("has-gomod", "ensures all Go packages have a go.mod file in their source list", leeway.GoPackage, checkGolangHasGomod))
register(PackageCheck("has-buildflags", "checks for use of deprecated buildFlags config", leeway.GoPackage, checkGolangHasBuildFlags))
}

func checkGolangHasGomod(pkg *leeway.Package) ([]Finding, error) {
Expand Down Expand Up @@ -46,3 +48,21 @@ func checkGolangHasGomod(pkg *leeway.Package) ([]Finding, error) {
}
return f, nil
}

func checkGolangHasBuildFlags(pkg *leeway.Package) ([]Finding, error) {
goCfg, ok := pkg.Config.(*leeway.GoPkgConfig)
if !ok {
return nil, fmt.Errorf("Go package does not have go package config")
}

if len(goCfg.BuildFlags) > 0 {
return []Finding{{
Component: pkg.C,
Description: "buildFlags are deprecated, use buildCommand instead",
Error: false,
Package: pkg,
}}, nil
}

return nil, nil
}

0 comments on commit 42ce465

Please sign in to comment.