-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexec.go
More file actions
100 lines (82 loc) · 2.65 KB
/
exec.go
File metadata and controls
100 lines (82 loc) · 2.65 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
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
package sandbox
import (
"fmt"
"os"
"connectrpc.com/connect"
"github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/helpers"
civ1 "github.com/depot/cli/pkg/proto/depot/ci/v1"
"github.com/spf13/cobra"
)
func newSandboxExec() *cobra.Command {
cmd := &cobra.Command{
Use: "exec [flags]",
Short: "Execute a command within the compute",
Long: "Execute a command within the compute",
Example: `
# execute command within the compute
depot sandbox exec --sandbox-id 1234567890 --session-id 1234567890 -- /bin/bash -lc whoami
# execute command with timeout (30 seconds)
depot sandbox exec --sandbox-id 1234567890 --session-id 1234567890 --timeout 30000 -- /bin/bash -lc whoami
# execute complex command
depot sandbox exec --sandbox-id 1234567890 --session-id 1234567890 -- /bin/bash -lc 'for i in {1..10}; do echo $i; sleep 1; done'
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
token, err := cmd.Flags().GetString("token")
cobra.CheckErr(err)
token, err = helpers.ResolveOrgAuth(ctx, token)
if err != nil {
return fmt.Errorf("failed to resolve token: %w", err)
}
sandboxID, err := cmd.Flags().GetString("sandbox-id")
cobra.CheckErr(err)
if sandboxID == "" {
return fmt.Errorf("sandbox-id is required")
}
sessionID, err := cmd.Flags().GetString("session-id")
cobra.CheckErr(err)
if sessionID == "" {
return fmt.Errorf("session-id is required")
}
timeout, err := cmd.Flags().GetInt("timeout")
cobra.CheckErr(err)
client := api.NewComputeClient()
stream, err := client.RemoteExec(ctx, api.WithAuthentication(connect.NewRequest(&civ1.ExecuteCommandRequest{
SandboxId: sandboxID,
SessionId: sessionID,
Command: &civ1.Command{
CommandArray: args,
TimeoutMs: int32(timeout),
},
}), token))
if err != nil {
// nolint:wrapcheck
return err
}
for stream.Receive() {
msg := stream.Msg()
switch v := msg.Message.(type) {
case *civ1.ExecuteCommandResponse_Stdout:
fmt.Fprint(os.Stdout, v.Stdout)
case *civ1.ExecuteCommandResponse_Stderr:
fmt.Fprint(os.Stderr, v.Stderr)
case *civ1.ExecuteCommandResponse_ExitCode:
if v.ExitCode != 0 {
os.Exit(int(v.ExitCode))
}
return nil
}
}
if err := stream.Err(); err != nil {
return fmt.Errorf("stream error: %w", err)
}
return nil
},
}
cmd.Flags().String("sandbox-id", "", "ID of the compute to execute the command against")
cmd.Flags().String("session-id", "", "The session the compute belongs to")
cmd.Flags().Int("timeout", 0, "The execution timeout in milliseconds")
return cmd
}