Skip to content

Commit

Permalink
bootc-image-builder/main: extend version command
Browse files Browse the repository at this point in the history
Extend version with printing also the timestamp and
"tainted" if not all files are checked in.
Also support calling it with `--version` or `-V`.
  • Loading branch information
schuellerf committed Dec 17, 2024
1 parent 6a7f584 commit 511e009
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,25 +529,55 @@ func rootPreRunE(cmd *cobra.Command, _ []string) error {
return nil
}

// TODO: provide more version info (like actual version number) once we
// release a real version
func cmdVersion(_ *cobra.Command, _ []string) error {
func cmdVersion() (string, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Errorf("cannot read build info")
return "", fmt.Errorf("cannot read build info")
}
var gitRev string
var buildTime string
var buildTainted bool
ret := []string{}
for _, bs := range info.Settings {
if bs.Key == "vcs.revision" {
gitRev = bs.Value
break
continue
}
if bs.Key == "vcs.time" {
buildTime = bs.Value
continue
}
if bs.Key == "vcs.modified" {
bT, err := strconv.ParseBool(bs.Value)
if err != nil {
logrus.Errorf("Error parsing 'vcs.modified': %v", err)
bT = true
}

buildTainted = bT
continue
}
}
if gitRev != "" {
fmt.Printf("revision: %s\n", gitRev[:7])
ret = append(ret, fmt.Sprintf("revision: %s", gitRev[:7]))
} else {
fmt.Printf("revision: unknown\n")
ret = append(ret, "revision: unknown")
}
if buildTime != "" {
ret = append(ret, fmt.Sprintf("build time: %s", buildTime))
}
if buildTainted {
ret = append(ret, "tainted")
}
return strings.Join(ret, ", "), nil
}

func cmdVersionCobra(_ *cobra.Command, _ []string) error {
v, err := cmdVersion()
if err != nil {
return err
}
fmt.Println(v)
return nil
}

Expand Down Expand Up @@ -580,11 +610,13 @@ func buildCobraCmdline() (*cobra.Command, error) {
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Show the version and quit",
SilenceUsage: true,
Hidden: true,
RunE: cmdVersion,
RunE: cmdVersionCobra,
}

rootCmd.AddCommand(versionCmd)
rootCmd.Flags().BoolP("version", "V", false, "Show the version and quit")

rootCmd.AddCommand(manifestCmd)
manifestCmd.Flags().Bool("tls-verify", true, "require HTTPS and verify certificates when contacting registries")
Expand Down Expand Up @@ -649,6 +681,20 @@ func run() error {
if err != nil {
return err
}
printVersion, err := rootCmd.Flags().GetBool("version");
if err != nil {
return err
}
if printVersion {
v, err := cmdVersion()
if err != nil {
return err
}
fmt.Println(v)
return nil
}


return rootCmd.Execute()
}

Expand Down

0 comments on commit 511e009

Please sign in to comment.