forked from rancher/cli
/
stop.go
75 lines (63 loc) · 1.36 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
package cmd
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
var (
stopTypes = cli.StringSlice([]string{"service", "container", "host", "account"})
)
func StopCommand() cli.Command {
return cli.Command{
Name: "stop",
ShortName: "deactivate",
Usage: "Stop or deactivate " + strings.Join(stopTypes, ", "),
Action: stopResources,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "type",
Usage: "Restrict stop to specific types",
Value: &stopTypes,
},
},
}
}
func stopResources(ctx *cli.Context) error {
c, err := GetClient(ctx)
if err != nil {
return err
}
types := ctx.StringSlice("type")
w, err := NewWaiter(ctx)
if err != nil {
return err
}
var lastErr error
for _, id := range ctx.Args() {
resource, err := Lookup(c, id, types...)
if err != nil {
lastErr = err
fmt.Println(lastErr)
continue
}
action := ""
if _, ok := resource.Actions["stop"]; ok {
action = "stop"
} else if _, ok := resource.Actions["deactivate"]; ok {
action = "deactivate"
}
if action == "" {
lastErr = fmt.Errorf("stop or deactivate not available on %s", id)
fmt.Println(lastErr)
} else if err := c.Action(resource.Type, action, resource, nil, resource); err != nil {
lastErr = err
fmt.Println(lastErr)
} else {
w.Add(resource.Id)
}
}
if lastErr != nil {
return lastErr
}
return w.Wait()
}