Skip to content

Commit 3347d7b

Browse files
committed
cli/command/system: add newClientVersion() utility
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3124e77 commit 3347d7b

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

cli/command/system/version.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ type clientVersion struct {
8383
Context string `json:"Context"`
8484
}
8585

86+
// newClientVersion constructs a new clientVersion. If a dockerCLI is
87+
// passed as argument, additional information is included (API version),
88+
// which may invoke an API connection. Pass nil to omit the additional
89+
// information.
90+
func newClientVersion(contextName string, dockerCli command.Cli) clientVersion {
91+
v := clientVersion{
92+
Version: version.Version,
93+
GoVersion: runtime.Version(),
94+
GitCommit: version.GitCommit,
95+
BuildTime: reformatDate(version.BuildTime),
96+
Os: runtime.GOOS,
97+
Arch: arch(),
98+
Context: contextName,
99+
}
100+
if version.PlatformName != "" {
101+
v.Platform = &platformInfo{Name: version.PlatformName}
102+
}
103+
if dockerCli != nil {
104+
v.APIVersion = dockerCli.CurrentVersion()
105+
v.DefaultAPIVersion = dockerCli.DefaultVersion()
106+
}
107+
return v
108+
}
109+
86110
// NewVersionCommand creates a new cobra.Command for `docker version`
87111
func NewVersionCommand(dockerCli command.Cli) *cobra.Command {
88112
var opts versionOptions
@@ -130,22 +154,8 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
130154
// TODO print error if kubernetes is used?
131155

132156
vd := versionInfo{
133-
Client: clientVersion{
134-
Version: version.Version,
135-
APIVersion: dockerCli.CurrentVersion(),
136-
DefaultAPIVersion: dockerCli.DefaultVersion(),
137-
GoVersion: runtime.Version(),
138-
GitCommit: version.GitCommit,
139-
BuildTime: reformatDate(version.BuildTime),
140-
Os: runtime.GOOS,
141-
Arch: arch(),
142-
Context: dockerCli.CurrentContext(),
143-
},
157+
Client: newClientVersion(dockerCli.CurrentContext(), dockerCli),
144158
}
145-
if version.PlatformName != "" {
146-
vd.Client.Platform = &platformInfo{Name: version.PlatformName}
147-
}
148-
149159
sv, err := dockerCli.Client().ServerVersion(context.Background())
150160
if err == nil {
151161
vd.Server = &sv

0 commit comments

Comments
 (0)