Skip to content

Commit c6b1518

Browse files
committed
introduce context show command
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 0b78efe commit c6b1518

7 files changed

Lines changed: 113 additions & 45 deletions

File tree

cli/command/context/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewContextCommand(dockerCli command.Cli) *cobra.Command {
2323
newRemoveCommand(dockerCli),
2424
newUpdateCommand(dockerCli),
2525
newInspectCommand(dockerCli),
26+
newShowCommand(dockerCli),
2627
)
2728
return cmd
2829
}

cli/command/context/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type inspectOptions struct {
1515
refs []string
1616
}
1717

18-
// newInspectCommand creates a new cobra.Command for `docker image inspect`
18+
// newInspectCommand creates a new cobra.Command for `docker context inspect`
1919
func newInspectCommand(dockerCli command.Cli) *cobra.Command {
2020
var opts inspectOptions
2121

cli/command/context/show.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package context
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/cli/cli"
7+
"github.com/docker/cli/cli/command"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// newShowCommand creates a new cobra.Command for `docker context sow`
12+
func newShowCommand(dockerCli command.Cli) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "show",
15+
Short: "Print the name of the current context",
16+
Args: cli.NoArgs,
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
return runShow(dockerCli)
19+
},
20+
}
21+
return cmd
22+
}
23+
24+
func runShow(dockerCli command.Cli) error {
25+
context := dockerCli.CurrentContext()
26+
metadata, err := dockerCli.ContextStore().GetMetadata(context)
27+
if err != nil {
28+
return err
29+
}
30+
fmt.Fprintln(dockerCli.Out(), metadata.Name)
31+
return nil
32+
}

cli/command/context/show_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package context
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestShow(t *testing.T) {
10+
cli := makeFakeCli(t)
11+
createTestContext(t, cli, "current")
12+
cli.SetCurrentContext("current")
13+
14+
cli.OutBuffer().Reset()
15+
assert.NilError(t, runShow(cli))
16+
assert.Equal(t, cli.OutBuffer().String(), "current\n")
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
current
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: "context show"
3+
description: "The context show command description and usage"
4+
keywords: "context, show"
5+
---
6+
7+
# context show
8+
9+
```markdown
10+
Usage: docker context show
11+
12+
Print the name of the current context
13+
```
14+
15+
## Description
16+
17+
Print the name of the current context, possibly set by `DOCKER_CONTEXT` environment
18+
variable or `--context` global option.
19+
20+
## Examples
21+
22+
### Print the current context
23+
24+
The following example prints the currently used [`docker context`](context.md):
25+
26+
```console
27+
$ docker context show'
28+
default
29+
```
30+
31+
As an example, this output can be used to dynamically change your shell prompt
32+
to indicate your active context. The example below illustrates how this output
33+
could be used when using Bash as your shell.
34+
35+
Declare a function to obtain the current context in your `~/.bashrc`, and set
36+
this command as your `PROMPT_COMMAND`
37+
38+
```console
39+
function docker_context_prompt() {
40+
PS1="context: $(docker context show)> "
41+
}
42+
43+
PROMPT_COMMAND=docker_context_prompt
44+
```
45+
46+
After reloading the `~/.bashrc`, the prompt now shows the currently selected
47+
`docker context`:
48+
49+
```console
50+
$ source ~/.bashrc
51+
context: default> docker context create --docker host=unix:///var/run/docker.sock my-context
52+
my-context
53+
Successfully created context "my-context"
54+
context: default> docker context use my-context
55+
my-context
56+
Current context is now "my-context"
57+
context: my-context> docker context use default
58+
default
59+
Current context is now "default"
60+
context: default>
61+
```

docs/reference/commandline/version.md

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,3 @@ $ docker version --format '{{json .}}'
7676

7777
{"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"19.03.8","ApiVersion":"1.40","DefaultAPIVersion":"1.40","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"darwin","Arch":"amd64","BuildTime":"Wed Mar 11 01:21:11 2020","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.8","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"Wed Mar 11 01:29:16 2020","Experimental":"true","GitCommit":"afacb8b","GoVersion":"go1.12.17","KernelVersion":"4.19.76-linuxkit","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"v1.2.13","Details":{"GitCommit":"7ad184331fa3e55e52b890ea95e65ba581ae3429"}},{"Name":"runc","Version":"1.0.0-rc10","Details":{"GitCommit":"dc9208a3303feef5b3839f4323d9beb36df0a9dd"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.8","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"afacb8b","GoVersion":"go1.12.17","Os":"linux","Arch":"amd64","KernelVersion":"4.19.76-linuxkit","Experimental":true,"BuildTime":"2020-03-11T01:29:16.000000000+00:00"}}
7878
```
79-
80-
### Print the current context
81-
82-
The following example prints the currently used [`docker context`](context.md):
83-
84-
```console
85-
$ docker version --format='{{.Client.Context}}'
86-
default
87-
```
88-
89-
As an example, this output can be used to dynamically change your shell prompt
90-
to indicate your active context. The example below illustrates how this output
91-
could be used when using Bash as your shell.
92-
93-
Declare a function to obtain the current context in your `~/.bashrc`, and set
94-
this command as your `PROMPT_COMMAND`
95-
96-
```console
97-
function docker_context_prompt() {
98-
PS1="context: $(docker version --format='{{.Client.Context}}')> "
99-
}
100-
101-
PROMPT_COMMAND=docker_context_prompt
102-
```
103-
104-
After reloading the `~/.bashrc`, the prompt now shows the currently selected
105-
`docker context`:
106-
107-
```console
108-
$ source ~/.bashrc
109-
context: default> docker context create --docker host=unix:///var/run/docker.sock my-context
110-
my-context
111-
Successfully created context "my-context"
112-
context: default> docker context use my-context
113-
my-context
114-
Current context is now "my-context"
115-
context: my-context> docker context use default
116-
default
117-
Current context is now "default"
118-
context: default>
119-
```
120-
121-
Refer to the [`docker context` section](context.md) in the command line reference
122-
for more information about `docker context`.

0 commit comments

Comments
 (0)