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 a335203
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 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 @@ -579,12 +609,14 @@ func buildCobraCmdline() (*cobra.Command, error) {
SilenceUsage: true,
}
versionCmd := &cobra.Command{
Use: "version",
SilenceUsage: true,
Hidden: true,
RunE: cmdVersion,
}
Use: "version",
Short: "Show the version and quit",
SilenceUsage: true,
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 a335203

Please sign in to comment.