forked from canonical/lxd
/
action.go
59 lines (47 loc) · 1.2 KB
/
action.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
package main
import (
"fmt"
"github.com/gosexy/gettext"
"github.com/lxc/lxd"
"github.com/lxc/lxd/internal/gnuflag"
"github.com/lxc/lxd/shared"
)
type actionCmd struct {
action shared.ContainerAction
hasTimeout bool
}
func (c *actionCmd) showByDefault() bool {
return true
}
var timeout = -1
var force = false
func (c *actionCmd) usage() string {
return fmt.Sprintf(gettext.Gettext(
"Changes a containers state to %s.\n"+
"\n"+
"lxd %s <name>\n"), c.action, c.action)
}
func (c *actionCmd) flags() {
if c.hasTimeout {
gnuflag.IntVar(&timeout, "timeout", -1, gettext.Gettext("Time to wait for the container before killing it."))
gnuflag.BoolVar(&force, "force", false, gettext.Gettext("Force the container to shutdown."))
}
}
func (c *actionCmd) run(config *lxd.Config, args []string) error {
if len(args) != 1 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[0])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
resp, err := d.Action(name, c.action, timeout, force)
if err != nil {
return err
}
if resp.Type != lxd.Async {
return fmt.Errorf(gettext.Gettext("bad result type from action"))
}
return d.WaitForSuccess(resp.Operation)
}