Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/sandbox/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"

"github.com/docker/cli/cli-plugins/plugin"

"github.com/docker/docker-agent/pkg/userconfig"
)

Expand Down Expand Up @@ -42,6 +44,7 @@ var (
func BuildCagentArgs(argv []string) []string {
out := make([]string, 0, len(argv))
runStripped := false
agentStripped := false
agentResolved := false
hasYolo := false
skipNext := false
Expand All @@ -67,6 +70,10 @@ func BuildCagentArgs(argv []string) []string {
runStripped = true
continue
}
if !plugin.RunningStandalone() && !agentStripped && a == "agent" {
agentStripped = true
continue
}
// The first positional arg after "run" is the agent reference.
// Resolve it if it's a user-defined alias.
if runStripped && !agentResolved && !strings.HasPrefix(a, "-") {
Expand Down
19 changes: 19 additions & 0 deletions pkg/sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"testing"

"github.com/docker/cli/cli-plugins/metadata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -120,9 +121,22 @@ func TestExisting_HasWorkspace(t *testing.T) {
func TestBuildCagentArgs(t *testing.T) {
tests := []struct {
name string
env map[string]string
argv []string
want []string
}{
{
name: "using cli plugin, strips run and --sandbox and --debug, adds --yolo",
env: map[string]string{metadata.ReexecEnvvar: "docker"}, // env var set by cli plugin execution
argv: []string{"docker", "agent", "run", "./agent.yaml", "--sandbox", "--debug"},
want: []string{"./agent.yaml", "--yolo"},
},
{
name: "using cli plugin, strips -d shorthand",
env: map[string]string{metadata.ReexecEnvvar: "docker"}, // env var set by cli plugin execution
argv: []string{"docker", "agent", "-d", "run", "./agent.yaml"},
want: []string{"./agent.yaml", "--yolo"},
},
{
name: "strips run and --sandbox and --debug, adds --yolo",
argv: []string{"cagent", "run", "./agent.yaml", "--sandbox", "--debug"},
Expand Down Expand Up @@ -204,6 +218,11 @@ func TestBuildCagentArgs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// Use an empty HOME so no real user aliases interfere.
t.Setenv("HOME", t.TempDir())
if tt.env != nil {
for k, v := range tt.env {
t.Setenv(k, v)
}
}

got := sandbox.BuildCagentArgs(tt.argv)
assert.Equal(t, tt.want, got)
Expand Down
Loading