浏览文件
format.go
105 行 · 3.1 KB历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package sshutil |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/lima-vm/lima/v2/pkg/instance/hostname" |
| 12 | ) |
| 13 | |
| 14 | // FormatT specifies the format type. |
| 15 | type FormatT = string |
| 16 | |
| 17 | const ( |
| 18 | // FormatCmd prints the full ssh command line. |
| 19 | // |
| 20 | // ssh -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022 lima-default |
| 21 | FormatCmd = FormatT("cmd") |
| 22 | |
| 23 | // FormatArgs is similar to FormatCmd but omits "ssh" and the destination address. |
| 24 | // |
| 25 | // -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022 |
| 26 | FormatArgs = FormatT("args") |
| 27 | |
| 28 | // FormatOptions prints the ssh option key value pairs. |
| 29 | // |
| 30 | // IdentityFile="/Users/example/.lima/_config/user" |
| 31 | // User=example |
| 32 | // Hostname=127.0.0.1 |
| 33 | // Port=60022 |
| 34 | FormatOptions = FormatT("options") |
| 35 | |
| 36 | // FormatConfig uses the ~/.ssh/config format |
| 37 | // |
| 38 | // Host lima-default |
| 39 | // IdentityFile "/Users/example/.lima/_config/user " |
| 40 | // User example |
| 41 | // Hostname 127.0.0.1 |
| 42 | // Port 60022 |
| 43 | FormatConfig = FormatT("config") |
| 44 | |
| 45 | // TODO: consider supporting "url" format (ssh://USER@HOSTNAME:PORT). |
| 46 | // |
| 47 | // TODO: consider supporting "json" format. |
| 48 | // It is unclear whether we can just map ssh "config" into JSON, as "config" has duplicated keys. |
| 49 | // (JSON supports duplicated keys too, but not all JSON implementations expect JSON with duplicated keys). |
| 50 | ) |
| 51 | |
| 52 | // Formats is the list of the supported formats. |
| 53 | var Formats = []FormatT{FormatCmd, FormatArgs, FormatOptions, FormatConfig} |
| 54 | |
| 55 | func quoteOption(o string) string { |
| 56 | // make sure the shell doesn't swallow quotes in option values |
| 57 | if strings.ContainsRune(o, '"') { |
| 58 | o = "'" + o + "'" |
| 59 | } |
| 60 | return o |
| 61 | } |
| 62 | |
| 63 | // Format formats the ssh options. |
| 64 | func Format(w io.Writer, sshPath, instName string, format FormatT, opts []string) error { |
| 65 | fakeHostname := hostname.FromInstName(instName) // TODO: support customization |
| 66 | for _, o := range opts { |
| 67 | // A line break in an option value (e.g. a crafted user.name) would inject |
| 68 | // extra ssh_config directives such as ProxyCommand into the generated config. |
| 69 | if strings.ContainsAny(o, "\r\n") { |
| 70 | return fmt.Errorf("ssh option %#q contains a line break", o) |
| 71 | } |
| 72 | } |
| 73 | switch format { |
| 74 | case FormatCmd: |
| 75 | args := []string{sshPath} |
| 76 | for _, o := range opts { |
| 77 | args = append(args, "-o", quoteOption(o)) |
| 78 | } |
| 79 | args = append(args, fakeHostname) |
| 80 | // the args are similar to `limactl shell` but not exactly same. (e.g., lacks -t) |
| 81 | fmt.Fprintln(w, strings.Join(args, " ")) // no need to use shellescape.QuoteCommand |
| 82 | case FormatArgs: |
| 83 | var args []string |
| 84 | for _, o := range opts { |
| 85 | args = append(args, "-o", quoteOption(o)) |
| 86 | } |
| 87 | fmt.Fprintln(w, strings.Join(args, " ")) // no need to use shellescape.QuoteCommand |
| 88 | case FormatOptions: |
| 89 | for _, o := range opts { |
| 90 | fmt.Fprintln(w, o) |
| 91 | } |
| 92 | case FormatConfig: |
| 93 | fmt.Fprintf(w, "Host %s\n", fakeHostname) |
| 94 | for _, o := range opts { |
| 95 | kv := strings.SplitN(o, "=", 2) |
| 96 | if len(kv) != 2 { |
| 97 | return fmt.Errorf("unexpected option %#q", o) |
| 98 | } |
| 99 | fmt.Fprintf(w, " %s %s\n", kv[0], kv[1]) |
| 100 | } |
| 101 | default: |
| 102 | return fmt.Errorf("unknown format: %#q", format) |
| 103 | } |
| 104 | return nil |
| 105 | } |

