зеркало из https://github.com/docker/compose-cli.git
Merge pull request #2191 from nicks/nicks/use
cli: fix warnings on 'docker context use'
This commit is contained in:
Коммит
1395a3fd7c
|
@ -17,15 +17,8 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/compose/v2/cmd/formatter"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/docker/compose-cli/cli/mobycli"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
apicontext "github.com/docker/compose-cli/api/context"
|
||||
"github.com/docker/compose-cli/api/context/store"
|
||||
)
|
||||
|
||||
type removeOpts struct {
|
||||
|
@ -40,43 +33,11 @@ func removeCommand() *cobra.Command {
|
|||
Aliases: []string{"remove"},
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runRemove(args, opts.force)
|
||||
mobycli.Exec(cmd.Root())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force removing current context")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runRemove(args []string, force bool) error {
|
||||
currentContext := apicontext.Current()
|
||||
s := store.Instance()
|
||||
|
||||
var errs *multierror.Error
|
||||
for _, contextName := range args {
|
||||
if currentContext == contextName {
|
||||
if force {
|
||||
if err := runUse("default"); err != nil {
|
||||
errs = multierror.Append(errs, errors.New("cannot delete current context"))
|
||||
} else {
|
||||
errs = removeContext(s, contextName, errs)
|
||||
}
|
||||
} else {
|
||||
errs = multierror.Append(errs, errors.New("cannot delete current context"))
|
||||
}
|
||||
} else {
|
||||
errs = removeContext(s, contextName, errs)
|
||||
}
|
||||
}
|
||||
formatter.SetMultiErrorFormat(errs)
|
||||
return errs.ErrorOrNil()
|
||||
}
|
||||
|
||||
func removeContext(s store.Store, n string, errs *multierror.Error) *multierror.Error {
|
||||
if err := s.Remove(n); err != nil {
|
||||
errs = multierror.Append(errs, err)
|
||||
} else {
|
||||
fmt.Println(n)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
|
|
@ -17,12 +17,9 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose-cli/api/config"
|
||||
"github.com/docker/compose-cli/api/context/store"
|
||||
"github.com/docker/compose-cli/cli/mobycli"
|
||||
)
|
||||
|
||||
func useCommand() *cobra.Command {
|
||||
|
@ -31,22 +28,8 @@ func useCommand() *cobra.Command {
|
|||
Short: "Set the default context",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runUse(args[0])
|
||||
mobycli.Exec(cmd.Root())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runUse(name string) error {
|
||||
s := store.Instance()
|
||||
// Match behavior of existing CLI
|
||||
if name != store.DefaultContextName {
|
||||
if _, err := s.Get(name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := config.WriteCurrentContext(config.Dir(), name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(name)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -206,7 +206,7 @@ func TestContextMetrics(t *testing.T) {
|
|||
`{"command":"context use","context":"moby","source":"cli","status":"success"}`,
|
||||
`{"command":"ps","context":"local","source":"cli","status":"success"}`,
|
||||
`{"command":"stop","context":"local","source":"cli","status":"failure"}`,
|
||||
`{"command":"context use","context":"local","source":"cli","status":"success"}`,
|
||||
`{"command":"context use","context":"moby","source":"cli","status":"success"}`,
|
||||
`{"command":"ps","context":"local","source":"cli","status":"success"}`,
|
||||
`{"command":"context ls","context":"moby","source":"cli","status":"success"}`,
|
||||
}, usage)
|
||||
|
@ -250,7 +250,7 @@ func TestContextRemove(t *testing.T) {
|
|||
res = c.RunDockerOrExitError("context", "rm", "test-context-rm")
|
||||
res.Assert(t, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: "cannot delete current context",
|
||||
Err: "test-context-rm: context is in use, set -f flag to force remove",
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче