mirror of
https://github.com/docker/compose.git
synced 2025-07-24 22:24:41 +02:00
Add docker context inspect command relying on classic docker
This commit is contained in:
parent
832651b1dc
commit
528d47ce08
@ -47,6 +47,7 @@ func Command() *cobra.Command {
|
|||||||
showCommand(),
|
showCommand(),
|
||||||
useCommand(),
|
useCommand(),
|
||||||
login.Command(),
|
login.Command(),
|
||||||
|
inspectCommand(),
|
||||||
)
|
)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
47
cli/cmd/context/inspect.go
Normal file
47
cli/cmd/context/inspect.go
Normal 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
36
cli/dockerclassic/exec.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
33
cli/main.go
33
cli/main.go
@ -32,12 +32,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/api/cli/dockerclassic"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -95,7 +96,7 @@ func main() {
|
|||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
runningOwnCommand = isOwnCommand(cmd)
|
runningOwnCommand = isOwnCommand(cmd)
|
||||||
if !runningOwnCommand {
|
if !runningOwnCommand {
|
||||||
execMoby(cmd.Context())
|
dockerclassic.Exec(cmd.Context())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -119,7 +120,7 @@ func main() {
|
|||||||
root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
|
root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
|
||||||
runningOwnCommand = isOwnCommand(cmd)
|
runningOwnCommand = isOwnCommand(cmd)
|
||||||
if !runningOwnCommand {
|
if !runningOwnCommand {
|
||||||
execMoby(cmd.Context())
|
dockerclassic.Exec(cmd.Context())
|
||||||
}
|
}
|
||||||
helpFunc(cmd, args)
|
helpFunc(cmd, args)
|
||||||
})
|
})
|
||||||
@ -161,7 +162,7 @@ func main() {
|
|||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
execMoby(ctx)
|
dockerclassic.Exec(ctx)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -178,30 +179,6 @@ func newSigContext() (context.Context, func()) {
|
|||||||
return ctx, cancel
|
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) {
|
func determineCurrentContext(flag string, configDir string) (string, error) {
|
||||||
res := flag
|
res := flag
|
||||||
if res == "" {
|
if res == "" {
|
||||||
|
@ -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() {
|
func (s *E2eSuite) TestSetupError() {
|
||||||
It("should display an error if cannot shell out to docker-classic", func() {
|
It("should display an error if cannot shell out to docker-classic", func() {
|
||||||
err := os.Setenv("PATH", s.BinDir)
|
err := os.Setenv("PATH", s.BinDir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user