Merge pull request #161 from docker/context_inspect

Add docker context inspect command relying on classic docker
This commit is contained in:
Guillaume Tardif 2020-05-29 15:51:10 +02:00 committed by GitHub
commit f1fb80ecc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 28 deletions

View File

@ -47,6 +47,7 @@ func Command() *cobra.Command {
showCommand(),
useCommand(),
login.Command(),
inspectCommand(),
)
return cmd

View File

@ -0,0 +1,47 @@
/*
Copyright (c) 2020 Docker Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package context
import (
"github.com/docker/api/cli/dockerclassic"
"github.com/spf13/cobra"
)
func inspectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "inspect",
Short: "Display detailed information on one or more contexts",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dockerclassic.Exec(cmd.Context())
return nil
},
}
return cmd
}

36
cli/dockerclassic/exec.go Normal file
View File

@ -0,0 +1,36 @@
package dockerclassic
import (
"context"
"fmt"
"os"
"os/exec"
apicontext "github.com/docker/api/context"
"github.com/docker/api/context/store"
)
// Exec will delegate the cli command to docker-classic
func Exec(ctx context.Context) {
currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx)
_, err := s.Get(currentContext)
// Only run original docker command if the current context is not
// ours.
if err != nil {
cmd := exec.CommandContext(ctx, "docker-classic", os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
fmt.Fprintln(os.Stderr, exiterr.Error())
os.Exit(exiterr.ExitCode())
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
}

View File

@ -32,12 +32,13 @@ import (
"fmt"
"math/rand"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/docker/api/cli/dockerclassic"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -95,7 +96,7 @@ func main() {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
runningOwnCommand = isOwnCommand(cmd)
if !runningOwnCommand {
execMoby(cmd.Context())
dockerclassic.Exec(cmd.Context())
}
return nil
},
@ -119,7 +120,7 @@ func main() {
root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
runningOwnCommand = isOwnCommand(cmd)
if !runningOwnCommand {
execMoby(cmd.Context())
dockerclassic.Exec(cmd.Context())
}
helpFunc(cmd, args)
})
@ -161,7 +162,7 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
execMoby(ctx)
dockerclassic.Exec(ctx)
fmt.Println(err)
os.Exit(1)
}
@ -178,30 +179,6 @@ func newSigContext() (context.Context, func()) {
return ctx, cancel
}
func execMoby(ctx context.Context) {
currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx)
_, err := s.Get(currentContext)
// Only run original docker command if the current context is not
// ours.
if err != nil {
cmd := exec.CommandContext(ctx, "docker-classic", os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
fmt.Fprintln(os.Stderr, exiterr.Error())
os.Exit(exiterr.ExitCode())
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
}
func determineCurrentContext(flag string, configDir string) (string, error) {
res := flag
if res == "" {

View File

@ -69,6 +69,13 @@ func (s *E2eSuite) TestContextDefault() {
})
}
func (s *E2eSuite) TestContextLegacy() {
It("should inspect default", func() {
output := s.NewDockerCommand("context", "inspect", "default").ExecOrDie()
Expect(output).To(ContainSubstring(`"Name": "default"`))
})
}
func (s *E2eSuite) TestSetupError() {
It("should display an error if cannot shell out to docker-classic", func() {
err := os.Setenv("PATH", s.BinDir)