-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssh.go
70 lines (61 loc) · 1.47 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
61
62
63
64
65
66
67
68
69
// SPDX-FileCopyrightText: 2019 Weaveworks Ltd.
// SPDX-FileCopyrightText: 2023 bootloose authors
// SPDX-License-Identifier: Apache-2.0
package bootloose
import (
"errors"
"fmt"
"os/user"
"strings"
"github.com/spf13/cobra"
"github.com/k0sproject/bootloose/pkg/cluster"
)
type sshOptions struct {
verbose bool
}
func NewSSHCommand() *cobra.Command {
opts := &sshOptions{}
cmd := &cobra.Command{
Use: "ssh",
Short: "SSH into a machine",
Args: validateArgs,
RunE: opts.ssh,
}
cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "SSH verbose output")
return cmd
}
func (opts *sshOptions) ssh(cmd *cobra.Command, args []string) error {
cluster, err := cluster.NewFromFile(clusterConfigFile(cmd))
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
}
var remoteArgs []string
if opts.verbose {
remoteArgs = append(remoteArgs, "-v")
}
remoteArgs = append(remoteArgs, args[1:]...)
return cluster.SSH(node, username, remoteArgs...)
}
func validateArgs(_ *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("missing machine name argument")
}
return nil
}