Skip to content

Commit 5b5554e

Browse files
committed
fix(stop): add --name flag to stop named contexts and instances
The stop command previously only supported stopping the current context, with no way to target a specific context or instance by name. This adds a --name flag that resolves the target via two-step lookup: 1. Try the name as a context name directly (handles login --name) 2. Fall back to scanning contexts by Instance field (handles start --name, where context name is the server URL) Fixes #438 Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
1 parent 7f9da4d commit 5b5554e

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

cmd/stop.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import (
1111
)
1212

1313
func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
14+
var name string
1415

1516
var stopCmd = &cobra.Command{
1617
Use: "stop",
1718
Short: "stop microcks instance",
1819
Long: "stop microcks instance",
20+
Example: `# Stop the instance from the current context
21+
microcks stop
22+
23+
# Stop by context name or instance name
24+
microcks stop --name myinstance`,
1925
Run: func(cmd *cobra.Command, args []string) {
2026

2127
configFile := globalClientOpts.ConfigPath
@@ -27,8 +33,27 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
2733
return
2834
}
2935

30-
ctx, err := localConfig.ResolveContext("")
31-
errors.CheckError(err)
36+
var ctx *config.Context
37+
if name != "" {
38+
ctx, err = localConfig.ResolveContext(name)
39+
if err != nil {
40+
var ctxRef *config.ContextRef
41+
for _, c := range localConfig.Contexts {
42+
if c.Instance == name {
43+
ctxRef = &c
44+
break
45+
}
46+
}
47+
if ctxRef == nil {
48+
log.Fatalf("No context found for '%s'", name)
49+
}
50+
ctx, err = localConfig.ResolveContext(ctxRef.Name)
51+
errors.CheckError(err)
52+
}
53+
} else {
54+
ctx, err = localConfig.ResolveContext("")
55+
errors.CheckError(err)
56+
}
3257
instance := ctx.Instance
3358

3459
if instance.Name == "" {
@@ -73,5 +98,7 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
7398
},
7499
}
75100

101+
stopCmd.Flags().StringVar(&name, "name", "", "Name of the context or instance to stop (uses current context if empty)")
102+
76103
return stopCmd
77104
}

0 commit comments

Comments
 (0)