-
Notifications
You must be signed in to change notification settings - Fork 17
/
stop.go
56 lines (50 loc) · 1.34 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
package imagerunner
import (
"context"
"errors"
"fmt"
cmds "github.com/saucelabs/saucectl/internal/cmd"
"github.com/saucelabs/saucectl/internal/http"
"github.com/saucelabs/saucectl/internal/segment"
"github.com/saucelabs/saucectl/internal/usage"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func StopCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "stop <runID>",
Short: "Stop the running container",
SilenceUsage: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return errors.New("no run ID specified")
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
err := http.CheckProxy()
if err != nil {
return fmt.Errorf("invalid HTTP_PROXY value")
}
tracker := segment.DefaultTracker
go func() {
tracker.Collect(
cases.Title(language.English).String(cmds.FullName(cmd)),
usage.Properties{}.SetFlags(cmd.Flags()),
)
_ = tracker.Close()
}()
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ID := args[0]
fmt.Printf("Stopping container %s...\n", ID)
if err := imagerunnerClient.StopRun(context.Background(), ID); err != nil {
return fmt.Errorf("failed to stop the container: %v", err)
}
return nil
},
}
return cmd
}