5
5
package cli
6
6
7
7
import (
8
- "os"
9
- "os/exec"
10
- "strings"
8
+ "github.com/Sirupsen/logrus"
11
9
12
- log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
13
-
14
- "github.com/scaleway/scaleway-cli/pkg/api"
15
- "github.com/scaleway/scaleway-cli/pkg/utils"
10
+ "github.com/scaleway/scaleway-cli/pkg/commands"
16
11
)
17
12
18
13
var cmdKill = & Command {
@@ -32,45 +27,21 @@ func init() {
32
27
var killHelp bool // -h, --help flag
33
28
var killGateway string // -g, --gateway flag
34
29
35
- func runKill (cmd * Command , args []string ) {
30
+ func runKill (cmd * Command , rawArgs []string ) {
36
31
if killHelp {
37
32
cmd .PrintUsage ()
38
33
}
39
- if len (args ) < 1 {
34
+ if len (rawArgs ) < 1 {
40
35
cmd .PrintShortUsage ()
41
36
}
42
37
43
- serverID := cmd .API .GetServerID (args [0 ])
44
- command := "halt"
45
- server , err := cmd .API .GetServer (serverID )
46
- if err != nil {
47
- log .Fatalf ("Failed to get server information for %s: %v" , serverID , err )
48
- }
49
-
50
- // Resolve gateway
51
- if killGateway == "" {
52
- killGateway = os .Getenv ("SCW_GATEWAY" )
38
+ args := commands.KillArgs {
39
+ Gateway : killGateway ,
40
+ Server : rawArgs [0 ],
53
41
}
54
- var gateway string
55
- if killGateway == serverID || killGateway == args [0 ] {
56
- gateway = ""
57
- } else {
58
- gateway , err = api .ResolveGateway (cmd .API , killGateway )
59
- if err != nil {
60
- log .Fatalf ("Cannot resolve Gateway '%s': %v" , killGateway , err )
61
- }
62
- }
63
-
64
- execCmd := append (utils .NewSSHExecCmd (server .PublicAddress .IP , server .PrivateIP , true , nil , []string {command }, gateway ))
65
-
66
- log .Debugf ("Executing: ssh %s" , strings .Join (execCmd , " " ))
67
-
68
- spawn := exec .Command ("ssh" , execCmd ... )
69
- spawn .Stdout = os .Stdout
70
- spawn .Stdin = os .Stdin
71
- spawn .Stderr = os .Stderr
72
- err = spawn .Run ()
42
+ ctx := cmd .GetContext (rawArgs )
43
+ err := commands .RunKill (ctx , args )
73
44
if err != nil {
74
- log . Fatal ( err )
45
+ logrus . Fatalf ( "Cannot execute 'kill': %v" , err )
75
46
}
76
47
}
0 commit comments