Skip to content

Commit

Permalink
Refactored 'top' command (scaleway#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 7, 2015
1 parent 09b1e57 commit ca2ae82
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
45 changes: 10 additions & 35 deletions pkg/cli/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
package cli

import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"

log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/utils"
"github.com/scaleway/scaleway-cli/pkg/commands"
)

var cmdTop = &Command{
Expand All @@ -32,40 +26,21 @@ func init() {
var topHelp bool // -h, --help flag
var topGateway string // -g, --gateway flag

func runTop(cmd *Command, args []string) {
func runTop(cmd *Command, rawArgs []string) {
if topHelp {
cmd.PrintUsage()
}
if len(args) != 1 {
if len(rawArgs) != 1 {
cmd.PrintShortUsage()
}

serverID := cmd.API.GetServerID(args[0])
command := "ps"
server, err := cmd.API.GetServer(serverID)
if err != nil {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

// Resolve gateway
if topGateway == "" {
topGateway = os.Getenv("SCW_GATEWAY")
}
var gateway string
if topGateway == serverID || topGateway == args[0] {
gateway = ""
} else {
gateway, err = api.ResolveGateway(cmd.API, topGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", topGateway, err)
}
args := commands.TopArgs{
Gateway: topGateway,
Server: rawArgs[0],
}

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway)
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
fmt.Printf("%s", out)
ctx := cmd.GetContext(rawArgs)
err := commands.RunTop(ctx, args)
if err != nil {
log.Fatal(err)
logrus.Fatalf("Cannot execute 'top': %v", err)
}
}
51 changes: 51 additions & 0 deletions pkg/commands/top.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.

package commands

import (
"fmt"
"os/exec"
"strings"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/utils"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
)

// TopArgs are flags for the `RunTop` function
type TopArgs struct {
Server string
Gateway string
}

// RunTop is the handler for 'scw top'
func RunTop(ctx CommandContext, args TopArgs) error {
serverID := ctx.API.GetServerID(args.Server)
command := "ps"
server, err := ctx.API.GetServer(serverID)
if err != nil {
return fmt.Errorf("failed to get server information for %s: %v", serverID, err)
}

// Resolve gateway
if args.Gateway == "" {
args.Gateway = ctx.Getenv("SCW_GATEWAY")
}
var gateway string
if args.Gateway == serverID || args.Gateway == args.Server {
gateway = ""
} else {
gateway, err = api.ResolveGateway(ctx.API, args.Gateway)
if err != nil {
return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err)
}
}

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway)
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
fmt.Printf("%s", out)
return err
}

0 comments on commit ca2ae82

Please sign in to comment.