/
custom_server_console.go
77 lines (66 loc) · 1.88 KB
/
custom_server_console.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package instance
import (
"context"
"fmt"
"reflect"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/gotty"
"github.com/scaleway/scaleway-cli/internal/interactive"
"github.com/scaleway/scaleway-cli/internal/terminal"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
type instanceConsoleServerArgs struct {
Zone scw.Zone
ServerID string
}
func serverConsoleCommand() *core.Command {
return &core.Command{
Short: `Connect to the serial console of an instance`,
Namespace: "instance",
Verb: "console",
Resource: "server",
ArgsType: reflect.TypeOf(instanceConsoleServerArgs{}),
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Short: "Server ID to connect to",
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
},
Run: instanceServerConsoleRun,
}
}
func instanceServerConsoleRun(ctx context.Context, argsI interface{}) (i interface{}, e error) {
args := argsI.(*instanceConsoleServerArgs)
client := core.ExtractClient(ctx)
apiInstance := instance.NewAPI(client)
serverResp, err := apiInstance.GetServer(&instance.GetServerRequest{
Zone: args.Zone,
ServerID: args.ServerID,
})
if err != nil {
return nil, err
}
server := serverResp.Server
secretKey, ok := client.GetSecretKey()
if !ok {
return nil, fmt.Errorf("could not get secret key")
}
ttyClient, err := gotty.NewClient(server.Zone, server.ID, secretKey)
if err != nil {
return nil, err
}
// Add hint on how to quit properly
fmt.Printf(terminal.Style("Open connection to %s (%s)\n", color.Bold), server.Name, server.ID)
fmt.Println(" - You may need to hit enter to start")
fmt.Println(" - Type Ctrl+q to quit.")
fmt.Println(interactive.Line("-"))
if err = ttyClient.Connect(); err != nil {
return nil, err
}
return nil, err
}