-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
66 lines (58 loc) · 1.49 KB
/
shell.go
File metadata and controls
66 lines (58 loc) · 1.49 KB
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
package cli
import (
"context"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"codebox/internal/app"
)
type shellOpts struct {
commonOpts
instance string
ports []string
}
func newShellCmd() *cobra.Command {
var ports []string
cmd := &cobra.Command{
Use: "shell INSTANCE",
Short: "Open an interactive shell into a sandbox instance",
Long: "Open an interactive shell into a sandbox instance over SSH.\n\n" +
"Use --port to set up TCP forwards while the shell is open. Pass the\n" +
"flag multiple times for multiple forwards.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: completeInstances,
RunE: func(cmd *cobra.Command, args []string) error {
opts := shellOpts{
commonOpts: readCommonOpts(cmd),
instance: args[0],
ports: ports,
}
return runShell(cmd.Context(),
cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr(), opts)
},
}
f := cmd.Flags()
f.SortFlags = false
f.StringArrayVar(&ports, "port", nil,
"Forward port LOCAL:REMOTE; repeat the flag for multiple forwards")
return cmd
}
func runShell(
ctx context.Context,
stdin io.Reader,
stdout, stderr io.Writer,
opts shellOpts,
) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("locate home directory: %w", err)
}
return app.New(home).Shell(ctx, stdin, stdout, stderr, app.ShellRequest{
Instance: opts.instance,
Orchestrator: opts.orchestrator,
Remote: opts.remote,
InstanceKey: opts.instanceKey,
Ports: opts.ports,
})
}