forked from weaveworks/footloose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
60 lines (52 loc) · 1.17 KB
/
ssh.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
package main
import (
"errors"
"fmt"
"os/user"
"strings"
"github.com/spf13/cobra"
"github.com/kingdonb/footloose/pkg/cluster"
)
var sshCmd = &cobra.Command{
Use: "ssh",
Short: "SSH into a machine",
Args: validateArgs,
RunE: ssh,
}
var sshOptions struct {
config string
}
func init() {
sshCmd.Flags().StringVarP(&sshOptions.config, "config", "c", Footloose, "Cluster configuration file")
footloose.AddCommand(sshCmd)
}
func ssh(cmd *cobra.Command, args []string) error {
cluster, err := cluster.NewFromFile(configFile(sshOptions.config))
if err != nil {
return err
}
var node string
var username string
if strings.Contains(args[0], "@") {
items := strings.Split(args[0], "@")
if len(items) != 2 {
return fmt.Errorf("bad syntax for user@node: %v", items)
}
username = items[0]
node = items[1]
} else {
node = args[0]
user, err := user.Current()
if err != nil {
return errors.New("error in getting current user")
}
username = user.Username
}
return cluster.SSH(node, username, args[1:]...)
}
func validateArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("missing machine name argument")
}
return nil
}