Skip to content

Commit

Permalink
Use go mod proxying
Browse files Browse the repository at this point in the history
This enables read build info both on binaries built by goreleaser, as
well as installed with `go install` directly.

More info: https://carlosbecker.com/posts/supply-chain-goreleaser-go-mod-proxy/

Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Jun 11, 2022
1 parent 329f7d1 commit 07456cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ builds:
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
gomod:
proxy: true
checksum:
name_template: 'checksums.txt'
changelog:
Expand Down
11 changes: 11 additions & 0 deletions internal/program/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"io"
"io/fs"
"runtime/debug"
)

// Option is a function that can be passed to WithOptions.
Expand All @@ -18,6 +19,16 @@ func WithBuildInfo(version, commit, date string) Option {
}
}

// WithGoModInfo reads build info and sets its fields, if its available.
func WithGoModInfo() Option {
return func(p *Program) {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
p.modVersion = info.Main.Version
p.modSum = info.Main.Sum
}
}
}

// WithLogFile sets the path to the log file.
func WithLogFile(path string) Option {
return func(p *Program) {
Expand Down
14 changes: 12 additions & 2 deletions internal/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type Program struct {
commit string
date string

modVersion string
modSum string

showVersion bool
profileFilename string
sortByCoverage bool
Expand All @@ -84,11 +87,18 @@ func (p *Program) Run() error {
}

if p.showVersion {
_, err := fmt.Fprintf(
p.output,
out := fmt.Sprintf(
"Version: %s\nCommit: %s\nDate: %s\n",
p.version, p.commit, p.date,
)
if p.modVersion != "" {
out += fmt.Sprintf(
"Module Version: %s\nModule Checksum: %s\n",
p.modVersion, p.modSum,
)
}

_, err := fmt.Fprint(p.output, out)

return err
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (

var (
// build information, set by goreleaser.
version string
version = "dev"
commit string
date string
)

func main() {
if err := program.New(
program.WithGoModInfo(),
program.WithBuildInfo(version, commit, date),
program.WithLogFile(os.Getenv("GOCOVSH_LOG_FILE")),
).Run(); err != nil {
Expand Down

0 comments on commit 07456cc

Please sign in to comment.