跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

show-ssh.go

119 行 · 3.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/sirupsen/logrus"
14 "github.com/spf13/cobra"
15
16 "github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
17 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
18 "github.com/lima-vm/lima/v2/pkg/sshutil"
19 "github.com/lima-vm/lima/v2/pkg/store"
20)
21
22const showSSHExample = `
23 "cmd" format (default): Full SSH command line
24 $ limactl show-ssh --format=cmd default
25 ssh -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022 lima-default
26
27 "args" format: Similar to the cmd format but omits "ssh" and the destination address
28 $ limactl show-ssh --format=args default
29 -o IdentityFile="/Users/example/.lima/_config/user" -o User=example -o Hostname=127.0.0.1 -o Port=60022
30
31 "options" format: SSH option key value pairs
32 $ limactl show-ssh --format=options default
33 IdentityFile="/Users/example/.lima/_config/user"
34 User=example
35 Hostname=127.0.0.1
36 Port=60022
37
38 "config" format: ~/.ssh/config format
39 $ limactl show-ssh --format=config default
40 Host lima-default
41 IdentityFile "/Users/example/.lima/_config/user "
42 User example
43 Hostname 127.0.0.1
44 Port 60022
45
46 To show the config file path:
47 $ limactl ls --format='{{.SSHConfigFile}}' default
48 /Users/example/.lima/default/ssh.config
49`
50
51func newShowSSHCommand() *cobra.Command {
52 limaHome := "~/" + dirnames.DotLima
53 if s, err := dirnames.LimaDir(); err == nil {
54 limaHome = s
55 }
56 shellCmd := &cobra.Command{
57 Use: "show-ssh [flags] INSTANCE",
58 Short: "Show the SSH command line (DEPRECATED; use `ssh -F` instead)",
59 Long: fmt.Sprintf(`Show the SSH command line (DEPRECATED)
60
61WARNING: 'limactl show-ssh' is deprecated.
62Instead, use 'ssh -F %s/default/ssh.config lima-default' .
63`, limaHome),
64 Example: showSSHExample,
65 Args: WrapArgsError(cobra.ExactArgs(1)),
66 RunE: showSSHAction,
67 ValidArgsFunction: showSSHBashComplete,
68 SilenceErrors: true,
69 GroupID: advancedCommand,
70 }
71
72 shellCmd.Flags().StringP("format", "f", sshutil.FormatCmd, "Format: "+strings.Join(sshutil.Formats, ", "))
73 _ = shellCmd.RegisterFlagCompletionFunc("format", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
74 return sshutil.Formats, cobra.ShellCompDirectiveNoFileComp
75 })
76 return shellCmd
77}
78
79func showSSHAction(cmd *cobra.Command, args []string) error {
80 ctx := cmd.Context()
81 format, err := cmd.Flags().GetString("format")
82 if err != nil {
83 return err
84 }
85 instName := args[0]
86 w := cmd.OutOrStdout()
87 inst, err := store.Inspect(ctx, instName)
88 if err != nil {
89 if errors.Is(err, os.ErrNotExist) {
90 return fmt.Errorf("instance %#q does not exist, run `limactl create %s` to create a new instance", instName, instName)
91 }
92 return err
93 }
94 logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
95 filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
96 sshExe, err := sshutil.NewSSHExe()
97 if err != nil {
98 return err
99 }
100 opts, err := sshutil.SSHOpts(
101 ctx,
102 sshExe,
103 inst.Dir,
104 *inst.Config.User.Name,
105 *inst.Config.SSH.LoadDotSSHPubKeys,
106 *inst.Config.SSH.ForwardAgent,
107 *inst.Config.SSH.ForwardX11,
108 *inst.Config.SSH.ForwardX11Trusted)
109 if err != nil {
110 return err
111 }
112 opts = append(opts, "Hostname=127.0.0.1")
113 opts = append(opts, fmt.Sprintf("Port=%d", inst.SSHLocalPort))
114 return sshutil.Format(w, "ssh", instName, format, opts)
115}
116
117func showSSHBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
118 return bashCompleteInstanceNames(cmd)
119}