@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "io"
7
6
"os"
8
7
"strings"
9
8
@@ -12,20 +11,6 @@ import (
12
11
"github.com/spf13/cobra"
13
12
)
14
13
15
- const (
16
- showSSHFormatCmd = "cmd"
17
- showSSHFormatArgs = "args"
18
- showSSHFormatOptions = "options"
19
- showSSHFormatConfig = "config"
20
- // TODO: consider supporting "url" format (ssh://USER@HOSTNAME:PORT)
21
-
22
- // TODO: consider supporting "json" format
23
- // It is unclear whether we can just map ssh "config" into JSON, as "config" has duplicated keys.
24
- // (JSON supports duplicated keys too, but not all JSON implementations expect JSON with duplicated keys)
25
- )
26
-
27
- var showSSHFormats = []string {showSSHFormatCmd , showSSHFormatArgs , showSSHFormatOptions , showSSHFormatConfig }
28
-
29
14
const showSSHExample = `
30
15
"cmd" format (default): Full ssh command line.
31
16
$ limactl show-ssh --format=cmd default
@@ -62,9 +47,9 @@ func newShowSSHCommand() *cobra.Command {
62
47
SilenceErrors : true ,
63
48
}
64
49
65
- shellCmd .Flags ().StringP ("format" , "f" , showSSHFormatCmd , "Format: " + strings .Join (showSSHFormats , ", " ))
50
+ shellCmd .Flags ().StringP ("format" , "f" , sshutil . FormatCmd , "Format: " + strings .Join (sshutil . Formats , ", " ))
66
51
_ = shellCmd .RegisterFlagCompletionFunc ("format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
67
- return showSSHFormats , cobra .ShellCompDirectiveNoFileComp
52
+ return sshutil . Formats , cobra .ShellCompDirectiveNoFileComp
68
53
})
69
54
return shellCmd
70
55
}
@@ -93,51 +78,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
93
78
}
94
79
opts = append (opts , "Hostname=127.0.0.1" )
95
80
opts = append (opts , fmt .Sprintf ("Port=%d" , inst .SSHLocalPort ))
96
- return formatSSH (w , instName , format , opts )
97
- }
98
-
99
- func quoteOption (o string ) string {
100
- // make sure the shell doesn't swallow quotes in option values
101
- if strings .ContainsRune (o , '"' ) {
102
- o = "'" + o + "'"
103
- }
104
- return o
105
- }
106
-
107
- func formatSSH (w io.Writer , instName , format string , opts []string ) error {
108
- fakeHostname := "lima-" + instName // corresponds to the default guest hostname
109
- switch format {
110
- case showSSHFormatCmd :
111
- args := []string {"ssh" }
112
- for _ , o := range opts {
113
- args = append (args , "-o" , quoteOption (o ))
114
- }
115
- args = append (args , fakeHostname )
116
- // the args are similar to `limactl shell` but not exactly same. (e.g., lacks -t)
117
- fmt .Fprintln (w , strings .Join (args , " " )) // no need to use shellescape.QuoteCommand
118
- case showSSHFormatArgs :
119
- var args []string
120
- for _ , o := range opts {
121
- args = append (args , "-o" , quoteOption (o ))
122
- }
123
- fmt .Fprintln (w , strings .Join (args , " " )) // no need to use shellescape.QuoteCommand
124
- case showSSHFormatOptions :
125
- for _ , o := range opts {
126
- fmt .Fprintln (w , o )
127
- }
128
- case showSSHFormatConfig :
129
- fmt .Fprintf (w , "Host %s\n " , fakeHostname )
130
- for _ , o := range opts {
131
- kv := strings .SplitN (o , "=" , 2 )
132
- if len (kv ) != 2 {
133
- return fmt .Errorf ("unexpected option %q" , o )
134
- }
135
- fmt .Fprintf (w , " %s %s\n " , kv [0 ], kv [1 ])
136
- }
137
- default :
138
- return fmt .Errorf ("unknown format: %q" , format )
139
- }
140
- return nil
81
+ return sshutil .Format (w , instName , format , opts )
141
82
}
142
83
143
84
func showSSHBashComplete (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
0 commit comments