Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/sapcc/go-makefile-maker
go 1.18

require (
github.com/sapcc/go-bits v0.0.0-20220706183424-591e645cac86
golang.org/x/mod v0.5.1
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/sapcc/go-bits v0.0.0-20220706183424-591e645cac86 h1:kuaSdCXr5ShLDcPSPgIhva8MVGlR2eDzFD1oJQaMXsk=
github.com/sapcc/go-bits v0.0.0-20220706183424-591e645cac86/go.mod h1:dnwRlM+FlnRc8/IXvaeUADvOhHDTTgwQ/Rkkj0Dtobc=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
Expand Down
18 changes: 8 additions & 10 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package core

import (
"errors"
"fmt"
"os/exec"
"strings"

"github.com/sapcc/go-bits/logg"

"github.com/sapcc/go-makefile-maker/internal/renovate"
)

Expand Down Expand Up @@ -189,10 +189,10 @@ type Metadata struct {
///////////////////////////////////////////////////////////////////////////////
// Helper functions

func (c *Configuration) Validate() error {
func (c *Configuration) Validate() {
// Validate GolangciLintConfiguration.
if len(c.GolangciLint.ErrcheckExcludes) > 0 && !c.GolangciLint.CreateConfig {
return errors.New("golangciLint.createConfig needs to be set to 'true' if golangciLint.errcheckExcludes is defined")
logg.Fatal("golangciLint.createConfig needs to be set to 'true' if golangciLint.errcheckExcludes is defined")
}

// Validate GithubWorkflowConfiguration.
Expand All @@ -203,12 +203,12 @@ func (c *Configuration) Validate() error {
errMsg := "could not find default branch using git, you can define manually be setting 'githubWorkflow.global.defaultBranch' in config"
b, err := exec.Command("git", "symbolic-ref", "refs/remotes/origin/HEAD").CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %s", errMsg, err.Error())
logg.Fatal("%s: %s", errMsg, err.Error())
}

branch := strings.TrimPrefix(string(b), "refs/remotes/origin/")
if branch == string(b) {
return errors.New(errMsg)
logg.Fatal(errMsg)
} else {
c.GitHubWorkflow.Global.DefaultBranch = strings.TrimSpace(branch)
}
Expand All @@ -217,15 +217,13 @@ func (c *Configuration) Validate() error {
// Validate CI workflow configuration.
if ghwCfg.CI.Postgres.Enabled || ghwCfg.CI.KubernetesEnvtest.Enabled {
if !ghwCfg.CI.Enabled {
return errors.New("githubWorkflow.ci.enabled needs to be set to 'true' when githubWorkflow.ci.postgres or githubWorkflow.ci.kubernetesEnvtest is enabled")
logg.Fatal("githubWorkflow.ci.enabled needs to be set to 'true' when githubWorkflow.ci.postgres or githubWorkflow.ci.kubernetesEnvtest is enabled")
}
if len(ghwCfg.CI.RunnerOSList) > 0 {
if len(ghwCfg.CI.RunnerOSList) > 1 || !strings.HasPrefix(ghwCfg.CI.RunnerOSList[0], "ubuntu") {
return errors.New("githubWorkflow.ci.runOn must only define a single Ubuntu based runner when githubWorkflow.ci.postgres or githubWorkflow.ci.kubernetesEnvtest is enabled")
logg.Fatal("githubWorkflow.ci.runOn must only define a single Ubuntu based runner when githubWorkflow.ci.postgres or githubWorkflow.ci.kubernetesEnvtest is enabled")
}
}
}
}

return nil
}
21 changes: 7 additions & 14 deletions internal/core/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
package core

import (
"errors"
"os"

"github.com/sapcc/go-bits/logg"
"github.com/sapcc/go-bits/must"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"

"github.com/sapcc/go-makefile-maker/internal/util"
)

// ScanResult contains data obtained through a scan of the configuration files
Expand All @@ -38,16 +37,10 @@ type ScanResult struct {
HasBinInfo bool // whether we can produce linker instructions for "github.com/sapcc/go-api-declarations/bininfo"
}

func Scan() (ScanResult, error) {
func Scan() ScanResult {
modFilename := "go.mod"
modFileBytes, err := os.ReadFile(modFilename)
if err != nil {
return ScanResult{}, err
}
modFile, err := modfile.Parse(modFilename, modFileBytes, nil)
if err != nil {
return ScanResult{}, err
}
modFileBytes := must.Return(os.ReadFile(modFilename))
modFile := must.Return(modfile.Parse(modFilename, modFileBytes, nil))

var goDeps []module.Version
hasBinInfo := false
Expand All @@ -67,13 +60,13 @@ func Scan() (ScanResult, error) {
ModulePath: modFile.Module.Mod.Path,
GoDirectDependencies: goDeps,
HasBinInfo: hasBinInfo,
}, nil
}
}

//MustModulePath reads the ModulePath field, but fails if it is empty.
func (sr ScanResult) MustModulePath() string {
if sr.ModulePath == "" {
util.Must(errors.New("could not find module path from go.mod file, make sure it is defined"))
logg.Fatal("could not find module path from go.mod file, make sure it is defined")
}
return sr.ModulePath
}
65 changes: 28 additions & 37 deletions internal/dockerfile/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ import (

_ "embed"

"github.com/sapcc/go-bits/must"

"github.com/sapcc/go-makefile-maker/internal/core"
"github.com/sapcc/go-makefile-maker/internal/util"
)

func mustI(_ int, err error) {
util.Must(err)
}

//go:embed Dockerfile.template
var template []byte

var argStatementRx = regexp.MustCompile(`^ARG\s*(\w+)\s*=\s*(.+?)\s*$`)

func RenderConfig(cfg core.Configuration) error {
func RenderConfig(cfg core.Configuration) {
//read "ARG" statements from `Dockerfile.template`
buildArgs := make(map[string]string)
for _, line := range strings.Split(string(template), "\n") {
Expand Down Expand Up @@ -95,35 +92,29 @@ WORKDIR /var/empty
ENTRYPOINT [ %[7]s ]
`, buildArgs["GOLANG_VERSION"], buildArgs["ALPINE_VERSION"], goBuildflags, cfg.Metadata.URL, packages, user, entrypoint)

f, err := os.Create("Dockerfile")
util.Must(err)

mustI(f.WriteString(dockerfile))
util.Must(f.Close())

f, err = os.Create(".dockerignore")
util.Must(err)

mustI(f.WriteString(
`.dockerignore
# TODO: uncomment when applications no longer use git to get version information
#.git/
.github/
.gitignore
.goreleaser.yml
/*.env*
.golangci.yaml
build/
CONTRIBUTING.md
Dockerfile
docs/
LICENSE*
Makefile.maker.yaml
README.md
report.html
shell.nix
/testing/
`))
mustI(f.WriteString(strings.Join(cfg.Dockerfile.ExtraIgnores, "\n") + "\n"))
return f.Close()
must.Succeed(os.WriteFile("Dockerfile", []byte(dockerfile), 0666))

dockerignoreLines := append([]string{
`.dockerignore`,
`# TODO: uncomment when applications no longer use git to get version information`,
`#.git/`,
`.github/`,
`.gitignore`,
`.goreleaser.yml`,
`/*.env*`,
`.golangci.yaml`,
`build/`,
`CONTRIBUTING.md`,
`Dockerfile`,
`docs/`,
`LICENSE*`,
`Makefile.maker.yaml`,
`README.md`,
`report.html`,
`shell.nix`,
`/testing/`,
}, cfg.Dockerfile.ExtraIgnores...)
dockerignore := strings.Join(dockerignoreLines, "\n") + "\n"

must.Succeed(os.WriteFile(".dockerignore", []byte(dockerignore), 0666))
}
45 changes: 17 additions & 28 deletions internal/ghworkflow/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,39 @@ import (

"gopkg.in/yaml.v3"

"github.com/sapcc/go-bits/must"

"github.com/sapcc/go-makefile-maker/internal/core"
)

const workflowDir = ".github/workflows"

// Render renders GitHub workflows.
func Render(cfg *core.Configuration) error {
func Render(cfg *core.Configuration) {
ghwCfg := cfg.GitHubWorkflow

err := os.MkdirAll(workflowDir, 0o755)
if err == nil && ghwCfg.CI.Enabled {
err = ciWorkflow(ghwCfg, cfg.Vendoring.Enabled, len(cfg.Binaries) > 0)
}
if err == nil && ghwCfg.License.Enabled {
err = licenseWorkflow(ghwCfg)
must.Succeed(os.MkdirAll(workflowDir, 0o755))
if ghwCfg.CI.Enabled {
ciWorkflow(ghwCfg, cfg.Vendoring.Enabled, len(cfg.Binaries) > 0)
}
if err == nil && ghwCfg.SpellCheck.Enabled {
err = spellCheckWorkflow(ghwCfg, cfg.SpellCheck.IgnoreWords)
if ghwCfg.License.Enabled {
licenseWorkflow(ghwCfg)
}
if err == nil && ghwCfg.SecurityChecks.Enabled {
err = codeQLWorkflow(ghwCfg)
if ghwCfg.SpellCheck.Enabled {
spellCheckWorkflow(ghwCfg, cfg.SpellCheck.IgnoreWords)
}
if err == nil && ghwCfg.SecurityChecks.Enabled {
err = dependencyReviewWorkflow(ghwCfg)
if ghwCfg.SecurityChecks.Enabled {
codeQLWorkflow(ghwCfg)
}
if err != nil {
return err
if ghwCfg.SecurityChecks.Enabled {
dependencyReviewWorkflow(ghwCfg)
}

return nil
}

func writeWorkflowToFile(w *workflow) error {
func writeWorkflowToFile(w *workflow) {
name := strings.ToLower(strings.ReplaceAll(w.Name, " ", "-"))
path := filepath.Join(workflowDir, name+".yaml")
f, err := os.Create(path)
if err != nil {
return err
}
f := must.Return(os.Create(path))
defer f.Close()

encoder := yaml.NewEncoder(f)
Expand All @@ -69,10 +63,5 @@ func writeWorkflowToFile(w *workflow) error {

fmt.Fprintln(f, core.AutogeneratedHeader)
fmt.Fprintln(f, "")
err = encoder.Encode(w)
if err != nil {
return err
}

return nil
must.Succeed(encoder.Encode(w))
}
4 changes: 2 additions & 2 deletions internal/ghworkflow/workflow_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/sapcc/go-makefile-maker/internal/core"
)

func ciWorkflow(cfg *core.GithubWorkflowConfiguration, vendoring, hasBinaries bool) error {
func ciWorkflow(cfg *core.GithubWorkflowConfiguration, vendoring, hasBinaries bool) {
goVersion := cfg.Global.GoVersion
ignorePaths := cfg.Global.IgnorePaths
if cfg.CI.IgnorePaths != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ func ciWorkflow(cfg *core.GithubWorkflowConfiguration, vendoring, hasBinaries bo
}
w.Jobs["test"] = testJob

return writeWorkflowToFile(w)
writeWorkflowToFile(w)
}

type buildTestJobOpts struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/ghworkflow/workflow_codeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package ghworkflow

import "github.com/sapcc/go-makefile-maker/internal/core"

func codeQLWorkflow(cfg *core.GithubWorkflowConfiguration) error {
func codeQLWorkflow(cfg *core.GithubWorkflowConfiguration) {
w := newWorkflow("CodeQL", cfg.Global.DefaultBranch, nil)
w.Permissions.Actions = tokenScopeRead // for github/codeql-action/init to get workflow details
w.Permissions.SecurityEvents = tokenScopeWrite // for github/codeql-action/analyze to upload SARIF results
Expand All @@ -40,5 +40,5 @@ func codeQLWorkflow(cfg *core.GithubWorkflowConfiguration) error {
})
w.Jobs = map[string]job{"analyze": j}

return writeWorkflowToFile(w)
writeWorkflowToFile(w)
}
4 changes: 2 additions & 2 deletions internal/ghworkflow/workflow_dependency-review.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package ghworkflow

import "github.com/sapcc/go-makefile-maker/internal/core"

func dependencyReviewWorkflow(cfg *core.GithubWorkflowConfiguration) error {
func dependencyReviewWorkflow(cfg *core.GithubWorkflowConfiguration) {
w := newWorkflow("Dependency Review", cfg.Global.DefaultBranch, nil)
w.On.Push.Branches = []string{} // trigger only on pull requests

Expand All @@ -29,5 +29,5 @@ func dependencyReviewWorkflow(cfg *core.GithubWorkflowConfiguration) error {
},
})
w.Jobs = map[string]job{"review": j}
return writeWorkflowToFile(w)
writeWorkflowToFile(w)
}
4 changes: 2 additions & 2 deletions internal/ghworkflow/workflow_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/sapcc/go-makefile-maker/internal/core"
)

func licenseWorkflow(cfg *core.GithubWorkflowConfiguration) error {
func licenseWorkflow(cfg *core.GithubWorkflowConfiguration) {
ignorePaths := cfg.Global.IgnorePaths
if cfg.License.IgnorePaths != nil {
ignorePaths = cfg.License.IgnorePaths
Expand Down Expand Up @@ -58,5 +58,5 @@ func licenseWorkflow(cfg *core.GithubWorkflowConfiguration) error {
})
w.Jobs = map[string]job{"addlicense": j}

return writeWorkflowToFile(w)
writeWorkflowToFile(w)
}
4 changes: 2 additions & 2 deletions internal/ghworkflow/workflow_spellcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/sapcc/go-makefile-maker/internal/core"
)

func spellCheckWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []string) error {
func spellCheckWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []string) {
ignorePaths := cfg.Global.IgnorePaths
if cfg.SpellCheck.IgnorePaths != nil {
ignorePaths = cfg.SpellCheck.IgnorePaths
Expand All @@ -48,5 +48,5 @@ func spellCheckWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []str
})
w.Jobs = map[string]job{"misspell": j}

return writeWorkflowToFile(w)
writeWorkflowToFile(w)
}
Loading