github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/top.go (about) 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/exec" 10 11 "github.com/Sirupsen/logrus" 12 "github.com/scaleway/scaleway-cli/pkg/api" 13 "github.com/scaleway/scaleway-cli/pkg/utils" 14 ) 15 16 // TopArgs are flags for the `RunTop` function 17 type TopArgs struct { 18 Server string 19 Gateway string 20 SSHUser string 21 SSHPort int 22 } 23 24 // RunTop is the handler for 'scw top' 25 func RunTop(ctx CommandContext, args TopArgs) error { 26 serverID, err := ctx.API.GetServerID(args.Server) 27 if err != nil { 28 return err 29 } 30 command := "ps" 31 server, err := ctx.API.GetServer(serverID) 32 if err != nil { 33 return fmt.Errorf("failed to get server information for %s: %v", serverID, err) 34 } 35 36 // Resolve gateway 37 if args.Gateway == "" { 38 args.Gateway = ctx.Getenv("SCW_GATEWAY") 39 } 40 var gateway string 41 if args.Gateway == serverID || args.Gateway == args.Server { 42 gateway = "" 43 } else { 44 gateway, err = api.ResolveGateway(ctx.API, args.Gateway) 45 if err != nil { 46 return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err) 47 } 48 } 49 50 sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, args.SSHUser, args.SSHPort, true, []string{command}, gateway) 51 logrus.Debugf("Executing: %s", sshCommand) 52 out, err := exec.Command("ssh", sshCommand.Slice()[1:]...).CombinedOutput() 53 if err == nil { 54 fmt.Fprintf(ctx.Stdout, "%s", out) 55 } 56 return err 57 }