spiegel_podman/cmd/podman/system/version.go
Ashley Cui d3706bcad1 Add BuildOrigin field to podman info
BuildOrigin is a field that can be set at build time by packagers. This helps us trace how and where the binary was built and installed from, allowing us to see if the issue is due to a specfic installation or a general podman bug. This field shows up in podman version and in podman info when populated. Note that podman info has a new field, Client, that only appears when running podman info using the remote client.

Automatically set the BuildOrigin field when building the macOS pkginstaller to pkginstaller.

Usage: make podman-remote BUILD_ORIGIN="mypackaging"

Signed-off-by: Ashley Cui <acui@redhat.com>
2025-01-30 14:48:30 -05:00

115 lines
3.1 KiB
Go

package system
import (
"fmt"
"os"
"strings"
"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report"
"github.com/containers/podman/v5/cmd/podman/common"
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/cmd/podman/validate"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
versionCommand = &cobra.Command{
Use: "version [options]",
Args: validate.NoArgs,
Short: "Display the Podman version information",
RunE: version,
ValidArgsFunction: completion.AutocompleteNone,
Annotations: map[string]string{
registry.ParentNSRequired: "",
},
}
versionFormat string
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: versionCommand,
})
flags := versionCommand.Flags()
formatFlagName := "format"
flags.StringVarP(&versionFormat, formatFlagName, "f", "", "Change the output format to JSON or a Go template")
_ = versionCommand.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SystemVersionReport{}))
}
func version(cmd *cobra.Command, args []string) error {
versions, err := registry.ContainerEngine().Version(registry.Context())
if err != nil {
return err
}
if report.IsJSON(versionFormat) {
s, err := json.MarshalToString(versions)
if err != nil {
return err
}
fmt.Println(s)
return nil
}
if cmd.Flag("format").Changed {
rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()
// Use OriginUnknown so it does not add an extra range since it
// will only be called for a single element and not a slice.
rpt, err = rpt.Parse(report.OriginUnknown, versionFormat)
if err != nil {
return err
}
if err := rpt.Execute(versions); err != nil {
// only log at debug since we fall back to the client only template
logrus.Debugf("Failed to execute template: %v", err)
// On Failure, assume user is using older version of podman version --format and check client
versionFormat = strings.ReplaceAll(versionFormat, ".Server.", ".")
rpt, err := rpt.Parse(report.OriginUnknown, versionFormat)
if err != nil {
return err
}
if err := rpt.Execute(versions.Client); err != nil {
return err
}
}
return nil
}
rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()
rpt, err = rpt.Parse(report.OriginPodman, versionTemplate)
if err != nil {
return err
}
return rpt.Execute(versions)
}
const versionTemplate = `{{with .Client -}}
Client:\tPodman Engine
Version:\t{{.Version}}
API Version:\t{{.APIVersion}}
Go Version:\t{{.GoVersion}}
{{if .GitCommit -}}Git Commit:\t{{.GitCommit}}\n{{end -}}
Built:\t{{.BuiltTime}}
{{if .BuildOrigin -}}Build Origin:\t{{.BuildOrigin}}\n{{end -}}
OS/Arch:\t{{.OsArch}}
{{- end}}
{{- if .Server }}{{with .Server}}
Server:\tPodman Engine
Version:\t{{.Version}}
API Version:\t{{.APIVersion}}
Go Version:\t{{.GoVersion}}
{{if .GitCommit -}}Git Commit:\t{{.GitCommit}}\n{{end -}}
Built:\t{{.BuiltTime}}
{{if .BuildOrigin -}}Build Origin:\t{{.BuildOrigin}}\n{{end -}}
OS/Arch:\t{{.OsArch}}
{{- end}}{{- end}}
`