This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
/
port.go
54 lines (46 loc) · 1.35 KB
/
port.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
// 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"
"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/utils"
)
// PortArgs are flags for the `RunPort` function
type PortArgs struct {
Gateway string
Server string
SSHUser string
SSHPort int
}
// RunPort is the handler for 'scw port'
func RunPort(ctx CommandContext, args PortArgs) error {
serverID, err := ctx.API.GetServerID(args.Server)
if err != nil {
return err
}
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)
}
}
command := []string{"netstat -lutn 2>/dev/null | grep LISTEN"}
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.SSHUser, args.SSHPort, command, true, gateway)
if err != nil {
return fmt.Errorf("command execution failed: %v", err)
}
return nil
}