Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 1395a3f

Browse files
authored
Merge pull request #2191 from nicks/nicks/use
cli: fix warnings on 'docker context use'
2 parents 69558b2 + bb0197e commit 1395a3f

File tree

3 files changed

+8
-64
lines changed

3 files changed

+8
-64
lines changed

cli/cmd/context/rm.go

+3-42
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@
1717
package context
1818

1919
import (
20-
"errors"
21-
"fmt"
22-
23-
"github.com/docker/compose/v2/cmd/formatter"
24-
"github.com/hashicorp/go-multierror"
20+
"github.com/docker/compose-cli/cli/mobycli"
2521
"github.com/spf13/cobra"
26-
27-
apicontext "github.com/docker/compose-cli/api/context"
28-
"github.com/docker/compose-cli/api/context/store"
2922
)
3023

3124
type removeOpts struct {
@@ -40,43 +33,11 @@ func removeCommand() *cobra.Command {
4033
Aliases: []string{"remove"},
4134
Args: cobra.MinimumNArgs(1),
4235
RunE: func(cmd *cobra.Command, args []string) error {
43-
return runRemove(args, opts.force)
36+
mobycli.Exec(cmd.Root())
37+
return nil
4438
},
4539
}
4640
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force removing current context")
4741

4842
return cmd
4943
}
50-
51-
func runRemove(args []string, force bool) error {
52-
currentContext := apicontext.Current()
53-
s := store.Instance()
54-
55-
var errs *multierror.Error
56-
for _, contextName := range args {
57-
if currentContext == contextName {
58-
if force {
59-
if err := runUse("default"); err != nil {
60-
errs = multierror.Append(errs, errors.New("cannot delete current context"))
61-
} else {
62-
errs = removeContext(s, contextName, errs)
63-
}
64-
} else {
65-
errs = multierror.Append(errs, errors.New("cannot delete current context"))
66-
}
67-
} else {
68-
errs = removeContext(s, contextName, errs)
69-
}
70-
}
71-
formatter.SetMultiErrorFormat(errs)
72-
return errs.ErrorOrNil()
73-
}
74-
75-
func removeContext(s store.Store, n string, errs *multierror.Error) *multierror.Error {
76-
if err := s.Remove(n); err != nil {
77-
errs = multierror.Append(errs, err)
78-
} else {
79-
fmt.Println(n)
80-
}
81-
return errs
82-
}

cli/cmd/context/use.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
package context
1818

1919
import (
20-
"fmt"
21-
2220
"github.com/spf13/cobra"
2321

24-
"github.com/docker/compose-cli/api/config"
25-
"github.com/docker/compose-cli/api/context/store"
22+
"github.com/docker/compose-cli/cli/mobycli"
2623
)
2724

2825
func useCommand() *cobra.Command {
@@ -31,22 +28,8 @@ func useCommand() *cobra.Command {
3128
Short: "Set the default context",
3229
Args: cobra.ExactArgs(1),
3330
RunE: func(cmd *cobra.Command, args []string) error {
34-
return runUse(args[0])
31+
mobycli.Exec(cmd.Root())
32+
return nil
3533
},
3634
}
3735
}
38-
39-
func runUse(name string) error {
40-
s := store.Instance()
41-
// Match behavior of existing CLI
42-
if name != store.DefaultContextName {
43-
if _, err := s.Get(name); err != nil {
44-
return err
45-
}
46-
}
47-
if err := config.WriteCurrentContext(config.Dir(), name); err != nil {
48-
return err
49-
}
50-
fmt.Println(name)
51-
return nil
52-
}

local/e2e/cli-only/e2e_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func TestContextMetrics(t *testing.T) {
206206
`{"command":"context use","context":"moby","source":"cli","status":"success"}`,
207207
`{"command":"ps","context":"local","source":"cli","status":"success"}`,
208208
`{"command":"stop","context":"local","source":"cli","status":"failure"}`,
209-
`{"command":"context use","context":"local","source":"cli","status":"success"}`,
209+
`{"command":"context use","context":"moby","source":"cli","status":"success"}`,
210210
`{"command":"ps","context":"local","source":"cli","status":"success"}`,
211211
`{"command":"context ls","context":"moby","source":"cli","status":"success"}`,
212212
}, usage)
@@ -250,7 +250,7 @@ func TestContextRemove(t *testing.T) {
250250
res = c.RunDockerOrExitError("context", "rm", "test-context-rm")
251251
res.Assert(t, icmd.Expected{
252252
ExitCode: 1,
253-
Err: "cannot delete current context",
253+
Err: "test-context-rm: context is in use, set -f flag to force remove",
254254
})
255255
})
256256

0 commit comments

Comments
 (0)