Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version support #210

Merged
merged 13 commits into from
May 13, 2024
14 changes: 13 additions & 1 deletion bincapz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/chainguard-dev/bincapz/pkg/compile"
"github.com/chainguard-dev/bincapz/pkg/profile"
"github.com/chainguard-dev/bincapz/pkg/render"
"github.com/chainguard-dev/bincapz/pkg/version"
"github.com/chainguard-dev/bincapz/rules"
thirdparty "github.com/chainguard-dev/bincapz/third_party"
"github.com/chainguard-dev/clog"
Expand Down Expand Up @@ -50,6 +51,8 @@ func main() {
statsFlag := flag.Bool("stats", false, "Show statistics about the scan")
thirdPartyFlag := flag.Bool("third-party", true, "Include third-party rules, which may have licensing restrictions")
verboseFlag := flag.Bool("verbose", false, "Emit verbose logging messages to stderr")
versionFlag := flag.Bool("version", false, "show version information")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize "Show" for consistency with your more recent flag changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9b582f9 (#210).


flag.Parse()
args := flag.Args()

Expand All @@ -72,7 +75,7 @@ func main() {
}
}

if len(args) == 0 {
if len(args) == 0 && !*versionFlag {
fmt.Printf("usage: bincapz [flags] <directories>")
returnCode = ExitInvalidArgument
return
Expand All @@ -83,6 +86,15 @@ func main() {
logLevel.Set(slog.LevelDebug)
}

if *versionFlag {
ver, err := version.Version()
if err != nil {
fmt.Printf("bincapz unknown version\n")
}
fmt.Printf("%s\n", ver)
os.Exit(0)
}

ctx := clog.WithLogger(context.Background(), log)
clog.FromContext(ctx).Info("bincapz starting")

Expand Down
41 changes: 41 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package version

import (
"fmt"
"runtime/debug"
)

const (
ID string = "0.11.0"
)

// Check if the build info contains a version.
func getBinaryVersion() (string, error) {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("failed to read build info")
}

for _, setting := range buildInfo.Settings {
if setting.Key == "main.BuildVersion" {
return setting.Value, nil
}
}

return "", nil
}

func Version() (string, error) {
var v string
var err error
// Check for the version in the binary first
if v, err = getBinaryVersion(); err != nil {
return "bincapz unknown version", err
}
// If present, return that value
// Otherwise, fall back to the contents of the VERSION const
if v != "" {
return fmt.Sprintf("bincapz %s", v), nil
}
return fmt.Sprintf("bincapz %s", ID), nil
}