-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
84 lines (74 loc) · 1.75 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
package composectl
import (
"fmt"
"github.com/spf13/cobra"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
)
type (
stopOptions struct {
All bool
}
)
var (
stopCmd = &cobra.Command{
Use: "stop",
Short: "stop --all | <app-name> [<app-name>]",
Long: ``,
Args: cobra.ArbitraryArgs,
}
)
func init() {
opts := stopOptions{}
stopCmd.Flags().BoolVar(&opts.All, "all", false, "stop all installed and running apps")
stopCmd.Run = func(cmd *cobra.Command, args []string) {
stopApps(cmd, args, &opts)
}
rootCmd.AddCommand(stopCmd)
}
func stopApps(cmd *cobra.Command, args []string, opts *stopOptions) {
if len(args) > 0 && opts.All {
DieNotNil(fmt.Errorf("`--all` flag cannot be specified if at least one app is specified as parameter"))
}
if len(args) == 0 && !opts.All {
DieNotNil(fmt.Errorf("either `--all` flag or app name should be specified"))
}
appsToStop, err := getAllAppsToStop(config.ComposeRoot)
DieNotNil(err)
if len(args) > 0 && !opts.All {
for _, a := range args {
found := false
for _, installedApp := range appsToStop {
if a == installedApp {
found = true
break
}
}
if !found {
DieNotNil(fmt.Errorf("the specified app is not installed: %s", a))
}
}
appsToStop = args
}
for _, app := range appsToStop {
cmd := exec.Command("docker", "compose", "down")
cmd.Dir = path.Join(config.ComposeRoot, app)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
DieNotNil(cmd.Run())
}
}
func getAllAppsToStop(composeRoot string) ([]string, error) {
var apps []string
err := filepath.Walk(composeRoot, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() || path == config.ComposeRoot {
return nil
}
apps = append(apps, info.Name())
return nil
})
return apps, err
}