-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathstop.go
More file actions
107 lines (90 loc) · 2.98 KB
/
Copy pathstop.go
File metadata and controls
107 lines (90 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cmd
import (
"fmt"
"log"
"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/microcks/microcks-cli/pkg/errors"
"github.com/spf13/cobra"
)
func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
var name string
var stopCmd = &cobra.Command{
Use: "stop",
Short: "stop microcks instance",
Long: "stop microcks instance",
Example: `# Stop the instance from the current context
microcks stop
# Stop by context name or instance name
microcks stop --name myinstance`,
Run: func(cmd *cobra.Command, args []string) {
configFile := globalClientOpts.ConfigPath
localConfig, err := config.ReadLocalConfig(configFile)
errors.CheckError(err)
if localConfig == nil {
fmt.Println("Config not found, nothing to stop")
return
}
var ctx *config.Context
if name != "" {
ctx, err = resolveStopTarget(name, localConfig)
} else {
ctx, err = localConfig.ResolveContext("")
}
errors.CheckError(err)
instance := ctx.Instance
if instance.Name == "" {
fmt.Println("No instance is associated with this context")
return
}
containerClient, err := connectors.NewContainerClient(instance.Driver)
errors.CheckError(err)
defer containerClient.CloseClient()
err = containerClient.StopContainer(instance.ContainerID)
if err != nil {
log.Fatalf("Failed to stop a container: %v", err)
return
}
fmt.Println("")
log.Printf("Instance %s stopped successfully", instance.Name)
// update configs
if instance.AutoRemove {
_, ok := localConfig.RemoveContext(ctx.Name)
if !ok {
log.Fatalf("Context %s does not exist", ctx.Name)
return
}
_ = localConfig.RemoveServer(ctx.Server.Server)
_ = localConfig.RemoveUser(ctx.User.Name)
_ = localConfig.RemoveAuth(ctx.Server.Server)
_ = localConfig.RemoveInstance(instance.Name)
localConfig.CurrentContext = ""
log.Printf("Instance %s removed successfully", instance.Name)
} else {
instance.Status = "Exited"
localConfig.UpsertInstance(instance)
log.Printf("Instance %s status updated to Exited", instance.Name)
}
err = config.WriteLocalConfig(*localConfig, configFile)
errors.CheckError(err)
},
}
stopCmd.Flags().StringVar(&name, "name", "", "Name of the context or instance to stop (uses current context if empty)")
return stopCmd
}
// resolveStopTarget resolves a stop target by name.
// Context name is checked first because it is the explicit user-assigned
// identifier. Instance name is a secondary label (from start --name) —
// used as fallback when no context with the given name exists.
func resolveStopTarget(name string, cfg *config.LocalConfig) (*config.Context, error) {
ctx, err := cfg.ResolveContext(name)
if err == nil {
return ctx, nil
}
for i := range cfg.Contexts {
if cfg.Contexts[i].Instance == name {
return cfg.ResolveContext(cfg.Contexts[i].Name)
}
}
return nil, fmt.Errorf("no context found for '%s'", name)
}