Skip to content

Commit bc6d794

Browse files
committed
Refactored 'kill' command (scaleway#80)
1 parent c3fa4e1 commit bc6d794

File tree

2 files changed

+67
-39
lines changed

2 files changed

+67
-39
lines changed

pkg/cli/kill.go

+10-39
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
package cli
66

77
import (
8-
"os"
9-
"os/exec"
10-
"strings"
8+
"github.com/Sirupsen/logrus"
119

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"
1611
)
1712

1813
var cmdKill = &Command{
@@ -32,45 +27,21 @@ func init() {
3227
var killHelp bool // -h, --help flag
3328
var killGateway string // -g, --gateway flag
3429

35-
func runKill(cmd *Command, args []string) {
30+
func runKill(cmd *Command, rawArgs []string) {
3631
if killHelp {
3732
cmd.PrintUsage()
3833
}
39-
if len(args) < 1 {
34+
if len(rawArgs) < 1 {
4035
cmd.PrintShortUsage()
4136
}
4237

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],
5341
}
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)
7344
if err != nil {
74-
log.Fatal(err)
45+
logrus.Fatalf("Cannot execute 'kill': %v", err)
7546
}
7647
}

pkg/commands/kill.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"os/exec"
11+
"strings"
12+
13+
"github.com/docker/machine/log"
14+
"github.com/scaleway/scaleway-cli/pkg/api"
15+
"github.com/scaleway/scaleway-cli/pkg/utils"
16+
)
17+
18+
// KillArgs are flags for the `RunKill` function
19+
type KillArgs struct {
20+
Gateway string
21+
Server string
22+
}
23+
24+
// RunKill is the handler for 'scw kill'
25+
func RunKill(ctx CommandContext, args KillArgs) error {
26+
serverID := ctx.API.GetServerID(args.Server)
27+
command := "halt"
28+
server, err := ctx.API.GetServer(serverID)
29+
if err != nil {
30+
return fmt.Errorf("failed to get server information for %s: %v", serverID, err)
31+
}
32+
33+
// Resolve gateway
34+
if args.Gateway == "" {
35+
args.Gateway = ctx.Getenv("SCW_GATEWAY")
36+
}
37+
var gateway string
38+
if args.Gateway == serverID || args.Gateway == args.Server {
39+
gateway = ""
40+
} else {
41+
gateway, err = api.ResolveGateway(ctx.API, args.Gateway)
42+
if err != nil {
43+
return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err)
44+
}
45+
}
46+
47+
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway))
48+
49+
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
50+
51+
spawn := exec.Command("ssh", execCmd...)
52+
spawn.Stdout = os.Stdout
53+
spawn.Stdin = os.Stdin
54+
spawn.Stderr = os.Stderr
55+
56+
return spawn.Run()
57+
}

0 commit comments

Comments
 (0)