From 1950058af83c711344f379d0dc102138befc1919 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 9 Jan 2025 10:38:38 +0100 Subject: [PATCH] main,test: tweak cmdVersion This comit tweak the new and very nice functionality of cmdVersion in the following way: - Rename to versionFromBuildInfo as it is no longer a "cmd*" (i.e. it no longer takes a cobra.Command) - Use switch/case as it's slightly more compact than if/else - Just build the string directly instead of using a list (slightly shorter) - Change "build_status: ok" to "build_tainted" with a boolean value to ensure this is easier to parse in yaml (and more descriptive as "status" is quite generic and may mean many things to people). - Extend the test_bib_version to test for the full strings prefixes in test_opts. --- bib/cmd/bootc-image-builder/main.go | 45 +++++++++-------------------- test/test_opts.py | 7 +++-- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/bib/cmd/bootc-image-builder/main.go b/bib/cmd/bootc-image-builder/main.go index e88b549f2..1aac24206 100644 --- a/bib/cmd/bootc-image-builder/main.go +++ b/bib/cmd/bootc-image-builder/main.go @@ -561,57 +561,38 @@ func rootPreRunE(cmd *cobra.Command, _ []string) error { return nil } -func cmdVersion() (string, error) { +func versionFromBuildInfo() (string, error) { info, ok := debug.ReadBuildInfo() if !ok { return "", fmt.Errorf("cannot read build info") } - var gitRev string - var buildTime string var buildTainted bool - ret := []string{} + gitRev := "unknown" + buildTime := "unknown" for _, bs := range info.Settings { - if bs.Key == "vcs.revision" { - gitRev = bs.Value - continue - } - if bs.Key == "vcs.time" { + switch bs.Key { + case "vcs.revision": + gitRev = bs.Value[:7] + case "vcs.time": buildTime = bs.Value - continue - } - if bs.Key == "vcs.modified" { + case "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 != "" { - ret = append(ret, fmt.Sprintf("build_revision: %s", gitRev[:7])) - } else { - ret = append(ret, "build_revision: unknown") - } - 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") - } - - // append final newline - ret = append(ret, "") - return strings.Join(ret, "\n"), nil + return fmt.Sprintf(`build_revision: %s +build_time: %s +build_tainted: %v +`, gitRev, buildTime, buildTainted), nil } func buildCobraCmdline() (*cobra.Command, error) { - version, err := cmdVersion() + version, err := versionFromBuildInfo() if err != nil { return nil, err } diff --git a/test/test_opts.py b/test/test_opts.py index afb3d0962..94cb5efe8 100644 --- a/test/test_opts.py +++ b/test/test_opts.py @@ -169,8 +169,11 @@ def test_bib_version(tmp_path, container_storage, build_fake_container, version_ capture_output=True, text=True, check=False) if git_res.returncode == 0: expected_rev = git_res.stdout.strip() - needle = f"revision: {expected_rev}" - assert needle in res.stdout + assert f"build_revision: {expected_rev}" in res.stdout + assert "build_time: " in res.stdout + assert "build_tainted: " in res.stdout + # we have a final newline + assert res.stdout[-1] == "\n" def test_bib_no_outside_container_warning_in_container(tmp_path, container_storage, build_fake_container):