@@ -16,69 +16,84 @@ import (
16
16
17
17
// DevcontainerCLI is an interface for the devcontainer CLI.
18
18
type DevcontainerCLI interface {
19
- Up (ctx context.Context , workspaceFolder , configPath string , opts ... DevcontainerCLIOptions ) (id string , err error )
20
- Exec (ctx context.Context , workspaceFolder , configPath string , cmd string , cmdArgs []string , opts ... DevcontainerCLIOptions ) error
19
+ Up (ctx context.Context , workspaceFolder , configPath string , opts ... DevcontainerCLIUpOptions ) (id string , err error )
20
+ Exec (ctx context.Context , workspaceFolder , configPath string , cmd string , cmdArgs []string , opts ... DevcontainerCLIExecOptions ) error
21
21
}
22
22
23
- // DevcontainerCLIOptions are options for the devcontainer CLI commands.
24
- type DevcontainerCLIOptions func (* devcontainerCLIUpConfig )
23
+ // DevcontainerCLIUpOptions are options for the devcontainer CLI Up
24
+ // command.
25
+ type DevcontainerCLIUpOptions func (* devcontainerCLIUpConfig )
26
+
27
+ type devcontainerCLIUpConfig struct {
28
+ args []string // Additional arguments for the Up command.
29
+ stdout io.Writer
30
+ stderr io.Writer
31
+ }
25
32
26
33
// WithRemoveExistingContainer is an option to remove the existing
27
- // container. Can only be used with the Up command.
28
- func WithRemoveExistingContainer () DevcontainerCLIOptions {
34
+ // container.
35
+ func WithRemoveExistingContainer () DevcontainerCLIUpOptions {
29
36
return func (o * devcontainerCLIUpConfig ) {
30
- if o .command != "up" {
31
- panic ("developer error: WithRemoveExistingContainer can only be used with the Up command" )
32
- }
33
37
o .args = append (o .args , "--remove-existing-container" )
34
38
}
35
39
}
36
40
37
- // WithOutput sets additional stdout and stderr writers for logs.
38
- func WithOutput (stdout , stderr io.Writer ) DevcontainerCLIOptions {
41
+ // WithUpOutput sets additional stdout and stderr writers for logs
42
+ // during Up operations.
43
+ func WithUpOutput (stdout , stderr io.Writer ) DevcontainerCLIUpOptions {
39
44
return func (o * devcontainerCLIUpConfig ) {
40
45
o .stdout = stdout
41
46
o .stderr = stderr
42
47
}
43
48
}
44
49
50
+ // DevcontainerCLIExecOptions are options for the devcontainer CLI Exec
51
+ // command.
52
+ type DevcontainerCLIExecOptions func (* devcontainerCLIExecConfig )
53
+
54
+ type devcontainerCLIExecConfig struct {
55
+ args []string // Additional arguments for the Exec command.
56
+ stdout io.Writer
57
+ stderr io.Writer
58
+ }
59
+
60
+ // WithExecOutput sets additional stdout and stderr writers for logs
61
+ // during Exec operations.
62
+ func WithExecOutput (stdout , stderr io.Writer ) DevcontainerCLIExecOptions {
63
+ return func (o * devcontainerCLIExecConfig ) {
64
+ o .stdout = stdout
65
+ o .stderr = stderr
66
+ }
67
+ }
68
+
45
69
// WithContainerID sets the container ID to target a specific container.
46
- // Can only be used with the Exec command.
47
- func WithContainerID (id string ) DevcontainerCLIOptions {
48
- return func (o * devcontainerCLIUpConfig ) {
49
- if o .command != "exec" {
50
- panic ("developer error: WithContainerID can only be used with the Exec command" )
51
- }
70
+ func WithContainerID (id string ) DevcontainerCLIExecOptions {
71
+ return func (o * devcontainerCLIExecConfig ) {
52
72
o .args = append (o .args , "--container-id" , id )
53
73
}
54
74
}
55
75
56
76
// WithRemoteEnv sets environment variables for the Exec command.
57
- // Can only be used with the Exec command.
58
- func WithRemoteEnv (env ... string ) DevcontainerCLIOptions {
59
- return func (o * devcontainerCLIUpConfig ) {
60
- if o .command != "exec" {
61
- panic ("developer error: WithRemoteEnv can only be used with the Exec command" )
62
- }
77
+ func WithRemoteEnv (env ... string ) DevcontainerCLIExecOptions {
78
+ return func (o * devcontainerCLIExecConfig ) {
63
79
for _ , e := range env {
64
80
o .args = append (o .args , "--remote-env" , e )
65
81
}
66
82
}
67
83
}
68
84
69
- type devcontainerCLIUpConfig struct {
70
- command string // The devcontainer CLI command to run, e.g. "up", "exec".
71
- removeExistingContainer bool
72
- args []string // Additional arguments for the command.
73
- stdout io.Writer
74
- stderr io.Writer
85
+ func applyDevcontainerCLIUpOptions (opts []DevcontainerCLIUpOptions ) devcontainerCLIUpConfig {
86
+ conf := devcontainerCLIUpConfig {}
87
+ for _ , opt := range opts {
88
+ if opt != nil {
89
+ opt (& conf )
90
+ }
91
+ }
92
+ return conf
75
93
}
76
94
77
- func applyDevcontainerCLIOptions (command string , opts []DevcontainerCLIOptions ) devcontainerCLIUpConfig {
78
- conf := devcontainerCLIUpConfig {
79
- command : command ,
80
- removeExistingContainer : false ,
81
- }
95
+ func applyDevcontainerCLIExecOptions (opts []DevcontainerCLIExecOptions ) devcontainerCLIExecConfig {
96
+ conf := devcontainerCLIExecConfig {}
82
97
for _ , opt := range opts {
83
98
if opt != nil {
84
99
opt (& conf )
@@ -101,9 +116,9 @@ func NewDevcontainerCLI(logger slog.Logger, execer agentexec.Execer) Devcontaine
101
116
}
102
117
}
103
118
104
- func (d * devcontainerCLI ) Up (ctx context.Context , workspaceFolder , configPath string , opts ... DevcontainerCLIOptions ) (string , error ) {
105
- conf := applyDevcontainerCLIOptions ( "up" , opts )
106
- logger := d .logger .With (slog .F ("workspace_folder" , workspaceFolder ), slog .F ("config_path" , configPath ), slog . F ( "recreate" , conf . removeExistingContainer ) )
119
+ func (d * devcontainerCLI ) Up (ctx context.Context , workspaceFolder , configPath string , opts ... DevcontainerCLIUpOptions ) (string , error ) {
120
+ conf := applyDevcontainerCLIUpOptions ( opts )
121
+ logger := d .logger .With (slog .F ("workspace_folder" , workspaceFolder ), slog .F ("config_path" , configPath ))
107
122
108
123
args := []string {
109
124
"up" ,
@@ -145,8 +160,8 @@ func (d *devcontainerCLI) Up(ctx context.Context, workspaceFolder, configPath st
145
160
return result .ContainerID , nil
146
161
}
147
162
148
- func (d * devcontainerCLI ) Exec (ctx context.Context , workspaceFolder , configPath string , cmd string , cmdArgs []string , opts ... DevcontainerCLIOptions ) error {
149
- conf := applyDevcontainerCLIOptions ( "exec" , opts )
163
+ func (d * devcontainerCLI ) Exec (ctx context.Context , workspaceFolder , configPath string , cmd string , cmdArgs []string , opts ... DevcontainerCLIExecOptions ) error {
164
+ conf := applyDevcontainerCLIExecOptions ( opts )
150
165
logger := d .logger .With (slog .F ("workspace_folder" , workspaceFolder ), slog .F ("config_path" , configPath ))
151
166
152
167
args := []string {"exec" }
0 commit comments