Skip to content

Commit

Permalink
main,test: tweak cmdVersion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 committed Jan 10, 2025
1 parent 1754664 commit 1950058
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
45 changes: 13 additions & 32 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions test/test_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 1950058

Please sign in to comment.