docker version: add "context" to output

This adds the currently selected "docker context" to the output
of "docker version", which allows users to see which context
is selected to produce the version output, and can be used (for
example), to set the prompt to the currently selected context:

(in `~/.bashrc`):

```bash
function docker_context_prompt() {
        PS1="context: $(docker version --format='{{.Client.Context}}')> "
}

PROMPT_COMMAND=docker_context_prompt
```

After reloading the `~/.bashrc`, the prompt now shows the currently selected
`docker context`:

```bash
$ source ~/.bashrc
context: default> docker context create --docker host=unix:///var/run/docker.sock my-context
my-context
Successfully created context "my-context"
context: default> docker context use my-context
my-context
Current context is now "my-context"
context: my-context> docker context use default
default
Current context is now "default"
context: default>
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2020-05-07 12:57:20 +02:00
parent 4f058143c7
commit 181e60499f
4 changed files with 50 additions and 0 deletions

View File

@ -5,6 +5,7 @@ Client:
Git commit: deadbeef
Built: Wed May 30 22:21:05 2018
OS/Arch: linux/amd64
Context: my-context
Experimental: true
Server: Docker Enterprise Edition (EE) 2.0

View File

@ -32,6 +32,7 @@ Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}}
Git commit: {{.GitCommit}}
Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}}
Context: {{.Context}}
Experimental: {{.Experimental}}
{{- end}}
@ -80,6 +81,7 @@ type clientVersion struct {
Os string
Arch string
BuildTime string `json:",omitempty"`
Context string
Experimental bool
}
@ -147,6 +149,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Experimental: dockerCli.ClientInfo().HasExperimental,
Context: dockerCli.CurrentContext(),
},
}

View File

@ -41,6 +41,7 @@ func TestVersionAlign(t *testing.T) {
Os: "linux",
Arch: "amd64",
BuildTime: "Wed May 30 22:21:05 2018",
Context: "my-context",
Experimental: true,
},
Server: &types.Version{},