-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathstop.go
101 lines (80 loc) · 2.49 KB
/
stop.go
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
package commands
import (
"kool-dev/kool/core/builder"
"kool-dev/kool/services/checker"
"time"
"github.com/spf13/cobra"
)
// KoolStopFlags holds the flags for the kool stop command
type KoolStopFlags struct {
Purge bool
}
// KoolStop holds handlers and functions to implement the stop command logic
type KoolStop struct {
DefaultKoolService
Flags *KoolStopFlags
check checker.Checker
down builder.Command
rm builder.Command
}
func AddKoolStop(root *cobra.Command) {
var (
stop = NewKoolStop()
stopCmd = NewStopCommand(stop)
)
root.AddCommand(stopCmd)
}
// NewKoolStop creates a new handler for stop logic with default dependencies
func NewKoolStop() *KoolStop {
defaultKoolService := newDefaultKoolService()
return &KoolStop{
*defaultKoolService,
&KoolStopFlags{false},
checker.NewChecker(defaultKoolService.shell),
builder.NewCommand("docker", "compose", "down"),
builder.NewCommand("docker", "compose", "rm"),
}
}
// Execute runs the stop logic with incoming arguments.
func (s *KoolStop) Execute(args []string) (err error) {
var stopCommand builder.Command
if err = s.check.Check(); err != nil {
return
}
if len(args) == 0 {
s.down.AppendArgs("--remove-orphans")
// no specific services passed in, so we gonna 'docker compose down'
if s.Flags.Purge {
s.down.AppendArgs("--volumes")
}
stopCommand = s.down
} else {
// we should only stop some services!
s.rm.AppendArgs("-s", "-f") // stops containers; no interactive
if s.Flags.Purge {
s.rm.AppendArgs("-v") // removes volumes
s.Shell().Warning("Attention: when stopping specific services, only anonymous volumes will be removed.")
}
s.rm.AppendArgs(args...)
stopCommand = s.rm
}
err = s.Shell().Interactive(stopCommand)
time.Sleep(time.Second * 2)
return
}
// NewStopCommand initializes new kool stop command
func NewStopCommand(stop *KoolStop) (stopCmd *cobra.Command) {
var task = NewKoolTask("Stopping all service containers", stop)
task.SetFrameOutput(false)
stopCmd = &cobra.Command{
Use: "stop [SERVICE...]",
SuggestFor: []string{"down"},
Short: "Stop and destroy running service containers",
Long: `Stop and destroy the specified [SERVICE] containers, which were started
using 'kool start'. If no [SERVICE] is provided, all running containers are stopped.`,
RunE: DefaultCommandRunFunction(task),
DisableFlagsInUseLine: true,
}
stopCmd.Flags().BoolVarP(&stop.Flags.Purge, "purge", "", false, "Remove all persistent data from volume mounts on containers")
return
}