-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathshow-ssh.go
108 lines (96 loc) · 3.41 KB
/
show-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const showSSHExample = `
"cmd" format (default): Full ssh command line.
$ limactl show-ssh --format=cmd default
ssh -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022 lima-default
"args" format: Similar to the cmd format but omits "ssh" and the destination address
$ limactl show-ssh --format=args default
-o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022
"options" format: ssh option key value pairs
$ limactl show-ssh --format=options default
IdentityFile="/Users/example/.lima/_config/user"
User=example
Hostname=127.0.0.1
Port=60022
"config" format: ~/.ssh/config format
$ limactl show-ssh --format=config default
Host lima-default
IdentityFile "/Users/example/.lima/_config/user "
User example
Hostname 127.0.0.1
Port 60022
To show the config file path:
$ limactl ls --format='{{.SSHConfigFile}}' default
/Users/example/.lima/default/ssh.config
`
func newShowSSHCommand() *cobra.Command {
limaHome := "~/" + dirnames.DotLima
if s, err := dirnames.LimaDir(); err == nil {
limaHome = s
}
shellCmd := &cobra.Command{
Use: "show-ssh [flags] INSTANCE",
Short: "Show the ssh command line (DEPRECATED; use `ssh -F` instead)",
Long: fmt.Sprintf(`Show the ssh command line (DEPRECATED)
WARNING: 'limactl show-ssh' is deprecated.
Instead, use 'ssh -F %s/default/ssh.config lima-default' .
`, limaHome),
Example: showSSHExample,
Args: WrapArgsError(cobra.ExactArgs(1)),
RunE: showSSHAction,
ValidArgsFunction: showSSHBashComplete,
SilenceErrors: true,
GroupID: advancedCommand,
}
shellCmd.Flags().StringP("format", "f", sshutil.FormatCmd, "Format: "+strings.Join(sshutil.Formats, ", "))
_ = shellCmd.RegisterFlagCompletionFunc("format", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return sshutil.Formats, cobra.ShellCompDirectiveNoFileComp
})
return shellCmd
}
func showSSHAction(cmd *cobra.Command, args []string) error {
format, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
instName := args[0]
w := cmd.OutOrStdout()
inst, err := store.Inspect(instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q does not exist, run `limactl create %s` to create a new instance", instName, instName)
}
return err
}
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
opts, err := sshutil.SSHOpts(
inst.Dir,
*inst.Config.User.Name,
*inst.Config.SSH.LoadDotSSHPubKeys,
*inst.Config.SSH.ForwardAgent,
*inst.Config.SSH.ForwardX11,
*inst.Config.SSH.ForwardX11Trusted)
if err != nil {
return err
}
opts = append(opts, "Hostname=127.0.0.1")
opts = append(opts, fmt.Sprintf("Port=%d", inst.SSHLocalPort))
return sshutil.Format(w, instName, format, opts)
}
func showSSHBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return bashCompleteInstanceNames(cmd)
}