From 889109f7f78201679ccd5fc75546e1211240b8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Tue, 17 Dec 2024 14:48:14 +0100 Subject: [PATCH] bootc-image-builder/main: extend version command Extend version with printing also the timestamp and "tainted" if not all files are checked in. Also support calling it with `--version` or `-v`. --- bib/cmd/bootc-image-builder/main.go | 58 ++++++++++++++++++++++++----- test/test_opts.py | 5 ++- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/bib/cmd/bootc-image-builder/main.go b/bib/cmd/bootc-image-builder/main.go index e3bc59c0..150bd2b3 100644 --- a/bib/cmd/bootc-image-builder/main.go +++ b/bib/cmd/bootc-image-builder/main.go @@ -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") @@ -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{ @@ -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) @@ -687,6 +724,7 @@ func run() error { if err != nil { return err } + return rootCmd.Execute() } diff --git a/test/test_opts.py b/test/test_opts.py index 188b4f1f..afb3d096 100644 --- a/test/test_opts.py +++ b/test/test_opts.py @@ -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) @@ -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"