diff --git a/cli/command/context/cmd.go b/cli/command/context/cmd.go index 6dce68aeaa..f8b9e80d93 100644 --- a/cli/command/context/cmd.go +++ b/cli/command/context/cmd.go @@ -23,6 +23,7 @@ func NewContextCommand(dockerCli command.Cli) *cobra.Command { newRemoveCommand(dockerCli), newUpdateCommand(dockerCli), newInspectCommand(dockerCli), + newShowCommand(dockerCli), ) return cmd } diff --git a/cli/command/context/inspect.go b/cli/command/context/inspect.go index 65233bae15..ddd0f92639 100644 --- a/cli/command/context/inspect.go +++ b/cli/command/context/inspect.go @@ -15,7 +15,7 @@ type inspectOptions struct { refs []string } -// newInspectCommand creates a new cobra.Command for `docker image inspect` +// newInspectCommand creates a new cobra.Command for `docker context inspect` func newInspectCommand(dockerCli command.Cli) *cobra.Command { var opts inspectOptions diff --git a/cli/command/context/show.go b/cli/command/context/show.go new file mode 100644 index 0000000000..6d87154079 --- /dev/null +++ b/cli/command/context/show.go @@ -0,0 +1,32 @@ +package context + +import ( + "fmt" + + "github.com/docker/cli/cli" + "github.com/docker/cli/cli/command" + "github.com/spf13/cobra" +) + +// newShowCommand creates a new cobra.Command for `docker context sow` +func newShowCommand(dockerCli command.Cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "show", + Short: "Print the name of the current context", + Args: cli.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return runShow(dockerCli) + }, + } + return cmd +} + +func runShow(dockerCli command.Cli) error { + context := dockerCli.CurrentContext() + metadata, err := dockerCli.ContextStore().GetMetadata(context) + if err != nil { + return err + } + fmt.Fprintln(dockerCli.Out(), metadata.Name) + return nil +} diff --git a/cli/command/context/show_test.go b/cli/command/context/show_test.go new file mode 100644 index 0000000000..a1ba6c62b8 --- /dev/null +++ b/cli/command/context/show_test.go @@ -0,0 +1,19 @@ +package context + +import ( + "testing" + + "gotest.tools/v3/assert" + "gotest.tools/v3/golden" +) + +func TestShow(t *testing.T) { + cli := makeFakeCli(t) + createTestContext(t, cli, "current") + cli.SetCurrentContext("current") + + cli.OutBuffer().Reset() + assert.NilError(t, runShow(cli)) + golden.Assert(t, cli.OutBuffer().String(), "show.golden") + +} diff --git a/cli/command/context/testdata/show.golden b/cli/command/context/testdata/show.golden new file mode 100644 index 0000000000..5b9023a315 --- /dev/null +++ b/cli/command/context/testdata/show.golden @@ -0,0 +1 @@ +current diff --git a/docs/reference/commandline/context_show.md b/docs/reference/commandline/context_show.md new file mode 100644 index 0000000000..ba08fa73a0 --- /dev/null +++ b/docs/reference/commandline/context_show.md @@ -0,0 +1,61 @@ +--- +title: "context show" +description: "The context show command description and usage" +keywords: "context, show" +--- + +# context show + +```markdown +Usage: docker context show + +Print the name of the current context +``` + +## Description + +Print the name of the current context, possibly set by `DOCKER_CONTEXT` environment +variable or `--context` global option. + +## Examples + +### Print the current context + +The following example prints the currently used [`docker context`](context.md): + +```console +$ docker context show' +default +``` + +As an example, this output can be used to dynamically change your shell prompt +to indicate your active context. The example below illustrates how this output +could be used when using Bash as your shell. + +Declare a function to obtain the current context in your `~/.bashrc`, and set +this command as your `PROMPT_COMMAND` + +```console +function docker_context_prompt() { + PS1="context: $(docker context show)> " +} + +PROMPT_COMMAND=docker_context_prompt +``` + +After reloading the `~/.bashrc`, the prompt now shows the currently selected +`docker context`: + +```console +$ 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> +``` diff --git a/docs/reference/commandline/version.md b/docs/reference/commandline/version.md index 2133de0d01..e6b8fbf148 100644 --- a/docs/reference/commandline/version.md +++ b/docs/reference/commandline/version.md @@ -76,47 +76,3 @@ $ docker version --format '{{json .}}' {"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"}} ``` - -### Print the current context - -The following example prints the currently used [`docker context`](context.md): - -```console -$ docker version --format='{{.Client.Context}}' -default -``` - -As an example, this output can be used to dynamically change your shell prompt -to indicate your active context. The example below illustrates how this output -could be used when using Bash as your shell. - -Declare a function to obtain the current context in your `~/.bashrc`, and set -this command as your `PROMPT_COMMAND` - -```console -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`: - -```console -$ 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> -``` - -Refer to the [`docker context` section](context.md) in the command line reference -for more information about `docker context`.