cli/command/system: add newClientVersion() utility

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-10 02:44:04 +02:00
parent 3124e779c9
commit 3347d7b5aa
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 25 additions and 15 deletions

View File

@ -83,6 +83,30 @@ type clientVersion struct {
Context string `json:"Context"`
}
// newClientVersion constructs a new clientVersion. If a dockerCLI is
// passed as argument, additional information is included (API version),
// which may invoke an API connection. Pass nil to omit the additional
// information.
func newClientVersion(contextName string, dockerCli command.Cli) clientVersion {
v := clientVersion{
Version: version.Version,
GoVersion: runtime.Version(),
GitCommit: version.GitCommit,
BuildTime: reformatDate(version.BuildTime),
Os: runtime.GOOS,
Arch: arch(),
Context: contextName,
}
if version.PlatformName != "" {
v.Platform = &platformInfo{Name: version.PlatformName}
}
if dockerCli != nil {
v.APIVersion = dockerCli.CurrentVersion()
v.DefaultAPIVersion = dockerCli.DefaultVersion()
}
return v
}
// NewVersionCommand creates a new cobra.Command for `docker version`
func NewVersionCommand(dockerCli command.Cli) *cobra.Command {
var opts versionOptions
@ -130,22 +154,8 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
// TODO print error if kubernetes is used?
vd := versionInfo{
Client: clientVersion{
Version: version.Version,
APIVersion: dockerCli.CurrentVersion(),
DefaultAPIVersion: dockerCli.DefaultVersion(),
GoVersion: runtime.Version(),
GitCommit: version.GitCommit,
BuildTime: reformatDate(version.BuildTime),
Os: runtime.GOOS,
Arch: arch(),
Context: dockerCli.CurrentContext(),
},
Client: newClientVersion(dockerCli.CurrentContext(), dockerCli),
}
if version.PlatformName != "" {
vd.Client.Platform = &platformInfo{Name: version.PlatformName}
}
sv, err := dockerCli.Client().ServerVersion(context.Background())
if err == nil {
vd.Server = &sv