forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
59 lines (49 loc) · 1.57 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
package main
import (
"fmt"
"github.com/spf13/cobra"
)
// stopDescription is used to describe stop command in detail and auto generate command doc.
var stopDescription = "Stop a running container in Pouchd. " +
"This is useful when you wish to stop a container.And Pouchd will stop this running container and release the resource. " +
"The container that you stopped will be closed. "
// StopCommand use to implement 'stop' command, it stops a container.
type StopCommand struct {
baseCommand
}
// Init initialize stop command.
func (s *StopCommand) Init(c *Cli) {
s.cli = c
s.cmd = &cobra.Command{
Use: "stop [container]",
Short: "Stop a running container",
Long: stopDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return s.runStop(args)
},
Example: stopExample(),
}
s.addFlags()
}
// addFlags adds flags for specific command.
func (s *StopCommand) addFlags() {
// TODO: add flags here
}
// runStop is the entry of stop command.
func (s *StopCommand) runStop(args []string) error {
apiClient := s.cli.Client()
container := args[0]
if err := apiClient.ContainerStop(container); err != nil {
return fmt.Errorf("failed to stop container %s: %v", container, err)
}
return nil
}
// stopExample shows examples in stop command, and is used in auto-generated cli docs.
func stopExample() string {
return `$ pouch ps
Name:foo ID:71b9c1 Status:Running Image:docker.io/library/busybox:latest
$ pouch stop foo
$ pouch ps
Name:foo ID:71b9c1 Status:Stopped Image:docker.io/library/busybox:latest`
}