Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootc-image-builder/main: extend version command (HMS-5211) #761

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,35 +561,69 @@ 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("build_revision: %s", gitRev[:7]))
} else {
fmt.Printf("revision: unknown\n")
ret = append(ret, "build_revision: unknown")
}
return nil
if buildTime != "" {
schuellerf marked this conversation as resolved.
Show resolved Hide resolved
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
}

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

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

rootCmd.PersistentFlags().StringVar(&rootLogLevel, "log-level", "", "logging level (debug, info, error); default error")

Expand All @@ -605,7 +639,10 @@ 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,
}
buildCmd.SetVersionTemplate(version)

rootCmd.AddCommand(buildCmd)
manifestCmd := &cobra.Command{
Use: "manifest",
Expand All @@ -614,13 +651,21 @@ func buildCobraCmdline() (*cobra.Command, error) {
DisableFlagsInUseLine: true,
RunE: cmdManifest,
SilenceUsage: true,
Version: rootCmd.Version,
}
manifestCmd.SetVersionTemplate(version)

versionCmd := &cobra.Command{
Use: "version",
Short: "Show the version and quit",
SilenceUsage: true,
Hidden: true,
RunE: cmdVersion,
RunE: func(cmd *cobra.Command, args []string) error {
root := cmd.Root()
root.SetArgs([]string{"--version"})
return root.Execute()
},
}

rootCmd.AddCommand(versionCmd)

rootCmd.AddCommand(manifestCmd)
Expand Down Expand Up @@ -687,6 +732,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
Loading