Skip to content

Commit 0929c00

Browse files
committed
Moved low-level code to utils/utils.go
1 parent 86a249a commit 0929c00

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

commands/attach.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
package commands
66

77
import (
8-
"fmt"
9-
"os"
10-
"os/exec"
11-
128
log "github.com/Sirupsen/logrus"
139

1410
types "github.com/scaleway/scaleway-cli/commands/types"
11+
"github.com/scaleway/scaleway-cli/utils"
1512
)
1613

1714
var cmdAttach = &types.Command{
@@ -33,8 +30,6 @@ func init() {
3330
// Flags
3431
var attachHelp bool // -h, --help flag
3532

36-
const termjsBin string = "termjs-cli"
37-
3833
func runAttach(cmd *types.Command, args []string) {
3934
if attachHelp {
4035
cmd.PrintUsage()
@@ -45,27 +40,8 @@ func runAttach(cmd *types.Command, args []string) {
4540

4641
serverID := cmd.API.GetServerID(args[0])
4742

48-
termjsURL := fmt.Sprintf("https://tty.cloud.online.net?server_id=%s&type=serial&auth_token=%s", serverID, cmd.API.Token)
49-
50-
log.Debugf("Executing: %s %s", termjsBin, termjsURL)
51-
// FIXME: check if termjs-cli is installed
52-
spawn := exec.Command(termjsBin, termjsURL)
53-
spawn.Stdout = os.Stdout
54-
spawn.Stdin = os.Stdin
55-
spawn.Stderr = os.Stderr
56-
err := spawn.Run()
43+
err := utils.AttachToSerial(serverID, cmd.API.Token)
5744
if err != nil {
58-
log.Warnf("%v", err)
59-
fmt.Fprintf(os.Stderr, `
60-
You need to install '%s' from https://github.com/moul/term.js-cli
61-
62-
npm install -g term.js-cli
63-
64-
However, you can access your serial using a web browser:
65-
66-
%s
67-
68-
`, termjsBin, termjsURL)
69-
os.Exit(1)
45+
log.Fatalf("%v", err)
7046
}
7147
}

utils/utils.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,32 @@ func GetConfigFilePath() (string, error) {
144144

145145
return filepath.Join(homeDir, ".scwrc"), nil
146146
}
147+
148+
const termjsBin string = "termjs-cli"
149+
150+
// AttachToSerial tries to connect to server serial using 'term.js-cli' and fallback with a help message
151+
func AttachToSerial(serverID string, apiToken string) error {
152+
termjsURL := fmt.Sprintf("https://tty.cloud.online.net?server_id=%s&type=serial&auth_token=%s", serverID, apiToken)
153+
154+
log.Debugf("Executing: %s %s", termjsBin, termjsURL)
155+
// FIXME: check if termjs-cli is installed
156+
spawn := exec.Command(termjsBin, termjsURL)
157+
spawn.Stdout = os.Stdout
158+
spawn.Stdin = os.Stdin
159+
spawn.Stderr = os.Stderr
160+
err := spawn.Run()
161+
if err != nil {
162+
log.Warnf(`
163+
You need to install '%s' from https://github.com/moul/term.js-cli
164+
165+
npm install -g term.js-cli
166+
167+
However, you can access your serial using a web browser:
168+
169+
%s
170+
171+
`, termjsBin, termjsURL)
172+
return err
173+
}
174+
return nil
175+
}

0 commit comments

Comments
 (0)