@@ -23,7 +23,9 @@ import (
23
23
"net"
24
24
"net/http"
25
25
"strings"
26
+ "sync"
26
27
28
+ "github.com/distribution/distribution/v3/uuid"
27
29
moby "github.com/docker/docker/api/types"
28
30
containerType "github.com/docker/docker/api/types/container"
29
31
"github.com/docker/docker/api/types/events"
@@ -35,6 +37,7 @@ import (
35
37
"github.com/docker/docker/api/types/volume"
36
38
"github.com/docker/docker/client"
37
39
specs "github.com/opencontainers/image-spec/specs-go/v1"
40
+ "github.com/pkg/errors"
38
41
)
39
42
40
43
const (
@@ -48,12 +51,19 @@ type DryRunKey struct{}
48
51
// DryRunClient implements APIClient by delegating to implementation functions. This allows lazy init and per-method overrides
49
52
type DryRunClient struct {
50
53
apiClient client.APIClient
54
+ execs sync.Map
55
+ }
56
+
57
+ type execDetails struct {
58
+ container string
59
+ command []string
51
60
}
52
61
53
62
// NewDryRunClient produces a DryRunClient
54
63
func NewDryRunClient (apiClient client.APIClient ) * DryRunClient {
55
64
return & DryRunClient {
56
65
apiClient : apiClient ,
66
+ execs : sync.Map {},
57
67
}
58
68
}
59
69
@@ -156,13 +166,23 @@ func (d *DryRunClient) VolumeRemove(ctx context.Context, volumeID string, force
156
166
}
157
167
158
168
func (d * DryRunClient ) ContainerExecCreate (ctx context.Context , container string , config moby.ExecConfig ) (moby.IDResponse , error ) {
159
- fmt .Printf ("%sCreating Exec configuration for container %s with command '%s'\n " , DRYRUN_PREFIX , container , strings .Join (config .Cmd , " " ))
160
- config .Cmd = []string {"true" }
161
- return d .apiClient .ContainerExecCreate (ctx , container , config )
169
+ id := uuid .Generate ().String ()
170
+ d .execs .Store (id , execDetails {
171
+ container : container ,
172
+ command : config .Cmd ,
173
+ })
174
+ return moby.IDResponse {
175
+ ID : id ,
176
+ }, nil
162
177
}
163
178
164
179
func (d * DryRunClient ) ContainerExecStart (ctx context.Context , execID string , config moby.ExecStartCheck ) error {
165
- fmt .Printf ("%sExecuting command in detach mode\n " , DRYRUN_PREFIX )
180
+ v , ok := d .execs .LoadAndDelete (execID )
181
+ if ! ok {
182
+ return fmt .Errorf ("invalid exec ID %q" , execID )
183
+ }
184
+ details := v .(execDetails )
185
+ fmt .Printf ("%sExecuting command %q in %s (detached mode)\n " , DRYRUN_PREFIX , details .command , details .container )
166
186
return nil
167
187
}
168
188
@@ -197,7 +217,7 @@ func (d *DryRunClient) ContainerDiff(ctx context.Context, container string) ([]c
197
217
}
198
218
199
219
func (d * DryRunClient ) ContainerExecAttach (ctx context.Context , execID string , config moby.ExecStartCheck ) (moby.HijackedResponse , error ) {
200
- return d . apiClient . ContainerExecAttach ( ctx , execID , config )
220
+ return moby. HijackedResponse {}, errors . New ( "interactive exec is not supported in dry-run mode" )
201
221
}
202
222
203
223
func (d * DryRunClient ) ContainerExecInspect (ctx context.Context , execID string ) (moby.ContainerExecInspect , error ) {
0 commit comments