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 20, 2024
1 parent ce6decd commit 889109f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
58 changes: 48 additions & 10 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,34 +561,64 @@ 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")
}
return nil
if buildTime != "" {
ret = append(ret, fmt.Sprintf("build time: %s", buildTime))
}
if buildTainted {
ret = append(ret, "build status: tainted")
} else {
ret = append(ret, "build status: ok")
}
return strings.Join(ret, "\n"), nil
}

func buildCobraCmdline() (*cobra.Command, error) {
version, err := cmdVersion()
if err != nil {
logrus.Error(err)
version = "unknown"
}

rootCmd := &cobra.Command{
Use: "bootc-image-builder",
Long: "Create a bootable image from an ostree native container",
PersistentPreRunE: rootPreRunE,
SilenceErrors: true,
Version: version,
}

rootCmd.PersistentFlags().StringVar(&rootLogLevel, "log-level", "", "logging level (debug, info, error); default error")
Expand All @@ -605,6 +635,7 @@ func buildCobraCmdline() (*cobra.Command, error) {
SilenceUsage: true,
Example: rootCmd.Use + " build quay.io/centos-bootc/centos-bootc:stream9\n" +
rootCmd.Use + " quay.io/centos-bootc/centos-bootc:stream9\n",
Version: rootCmd.Version,
}
rootCmd.AddCommand(buildCmd)
manifestCmd := &cobra.Command{
Expand All @@ -614,13 +645,19 @@ func buildCobraCmdline() (*cobra.Command, error) {
DisableFlagsInUseLine: true,
RunE: cmdManifest,
SilenceUsage: true,
Version: rootCmd.Version,
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Show the version and quit",
SilenceUsage: true,
Hidden: true,
RunE: cmdVersion,
Run: func(cmd *cobra.Command, args []string) {
root := cmd.Root()
root.SetArgs([]string{"--version"})
root.Execute()
},
}

rootCmd.AddCommand(versionCmd)

rootCmd.AddCommand(manifestCmd)
Expand Down Expand Up @@ -687,6 +724,7 @@ func run() error {
if err != nil {
return err
}

return rootCmd.Execute()
}

Expand Down
5 changes: 3 additions & 2 deletions test/test_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def test_bib_errors_only_once(tmp_path, container_storage, build_fake_container)
assert res.stderr.count(needle) == 1


def test_bib_version(tmp_path, container_storage, build_fake_container):
@pytest.mark.parametrize("version_argument", ["version", "--version", "-v"])
def test_bib_version(tmp_path, container_storage, build_fake_container, version_argument):
output_path = tmp_path / "output"
output_path.mkdir(exist_ok=True)

Expand All @@ -159,7 +160,7 @@ def test_bib_version(tmp_path, container_storage, build_fake_container):
"-v", f"{container_storage}:/var/lib/containers/storage",
"-v", f"{output_path}:/output",
build_fake_container,
"version",
version_argument,
], check=True, capture_output=True, text=True)

expected_rev = "unknown"
Expand Down

0 comments on commit 889109f

Please sign in to comment.