-
Notifications
You must be signed in to change notification settings - Fork 125
Add dmr sandbox config and launch command #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2be8943
934299f
e1e3fb1
064d96b
93c3726
6334d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,23 @@ Examples: | |
| appArgs = args[dashIdx:] | ||
| } | ||
|
|
||
| // If a sandbox tool is configured, launch host apps through it before | ||
| // resolving runner endpoints. This keeps sandbox launch independent of | ||
| // whether the host app binary itself is installed. | ||
| if _, ok := hostApps[app]; ok { | ||
| sandboxTool, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if sandboxTool != "" { | ||
| if err := validateSandboxTool(sandboxTool); err != nil { | ||
| return err | ||
| } | ||
| return launchSandboxedHostApp(cmd, sandboxTool, app, appArgs, dryRun) | ||
| } | ||
| } | ||
|
|
||
| runner, err := getStandaloneRunner(cmd.Context()) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to determine standalone runner endpoint: %w", err) | ||
|
|
@@ -155,9 +172,11 @@ Examples: | |
| if ca, ok := containerApps[app]; ok { | ||
| return launchContainerApp(cmd, ca, ep.container, image, port, detach, appArgs, dryRun) | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can remove these line space changes which i think is not part of changes |
||
| if cli, ok := hostApps[app]; ok { | ||
| return launchHostApp(cmd, app, ep.host, cli, model, runner, appArgs, dryRun) | ||
| } | ||
|
|
||
| return fmt.Errorf("unsupported app %q (supported: %s)", app, strings.Join(supportedApps, ", ")) | ||
| }, | ||
| } | ||
|
|
@@ -170,6 +189,31 @@ Examples: | |
| return c | ||
| } | ||
|
|
||
| func launchSandboxedHostApp(cmd *cobra.Command, sandboxTool, app string, appArgs []string, dryRun bool) error { | ||
| if err := validateSandboxTool(sandboxTool); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much better if we can retrieve so everything related to the sandbox happens inside the sandbox.go Then remove the |
||
| return err | ||
| } | ||
|
|
||
| args := append([]string{app}, appArgs...) | ||
|
|
||
| switch sandboxTool { | ||
| case "sbx": | ||
| if dryRun { | ||
| cmd.Printf("sbx %s\n", strings.Join(args, " ")) | ||
| return nil | ||
| } | ||
|
|
||
| launchCmd := exec.Command("sbx", args...) | ||
| launchCmd.Stdin = os.Stdin | ||
| launchCmd.Stdout = os.Stdout | ||
| launchCmd.Stderr = os.Stderr | ||
|
|
||
| return launchCmd.Run() | ||
| default: | ||
| return fmt.Errorf("unsupported sandbox tool %q", sandboxTool) | ||
| } | ||
| } | ||
|
|
||
| // listSupportedApps prints all supported apps with their descriptions and install status. | ||
| func listSupportedApps(cmd *cobra.Command) error { | ||
| cmd.Println("Supported apps:") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,7 @@ func NewRootCmd(cli *command.DockerCli) *cobra.Command { | |
| newShowCmd(), | ||
| newComposeCmd(), | ||
| newLaunchCmd(), | ||
| newSandboxConfigCmd(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need to do this if we do as a configs sub command
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be newConfigCmd, this will be a generic config system, sandbox is just one of the things |
||
| newTagCmd(), | ||
| newConfigureCmd(), | ||
| newPSCmd(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var allowedSandboxTools = map[string]struct{}{ | ||
| "sbx": {}, | ||
| } | ||
|
|
||
| func newSandboxConfigCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "config <key> <value>", | ||
| Short: "Set model runner configuration values", | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| key := args[0] | ||
| value := args[1] | ||
|
|
||
| if key != "sandbox.tool" { | ||
| return fmt.Errorf("unsupported config key %q", key) | ||
| } | ||
|
|
||
| if err := validateSandboxTool(value); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return writeSandboxToolConfig(value) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func validateSandboxTool(tool string) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can change as above mentioned suggestions |
||
| if _, ok := allowedSandboxTools[tool]; !ok { | ||
| return fmt.Errorf("unsupported sandbox tool %q", tool) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func dmrConfigPath() (string, error) { | ||
| configDir, err := os.UserConfigDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("unable to determine config directory: %w", err) | ||
| } | ||
|
|
||
| return filepath.Join(configDir, "dmr", "config.toml"), nil | ||
| } | ||
|
|
||
| func writeSandboxToolConfig(tool string) error { | ||
| path, err := dmrConfigPath() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { | ||
| return fmt.Errorf("unable to create config directory: %w", err) | ||
| } | ||
|
|
||
| content := fmt.Sprintf("[sandbox]\ntool = %q\n", tool) | ||
|
|
||
| if err := os.WriteFile(path, []byte(content), 0o644); err != nil { | ||
| return fmt.Errorf("unable to write config: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func readSandboxToolConfig() (string, error) { | ||
| path, err := dmrConfigPath() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| file, err := os.Open(path) | ||
| if os.IsNotExist(err) { | ||
| return "", nil | ||
| } | ||
| if err != nil { | ||
| return "", fmt.Errorf("unable to read config: %w", err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| inSandboxSection := false | ||
| scanner := bufio.NewScanner(file) | ||
|
|
||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
|
|
||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
|
|
||
| if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { | ||
| inSandboxSection = line == "[sandbox]" | ||
| continue | ||
| } | ||
|
|
||
| if !inSandboxSection { | ||
| continue | ||
| } | ||
|
|
||
| key, value, ok := strings.Cut(line, "=") | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if strings.TrimSpace(key) != "tool" { | ||
| continue | ||
| } | ||
|
|
||
| return strings.Trim(strings.TrimSpace(value), `"`), nil | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return "", fmt.Errorf("unable to parse config: %w", err) | ||
| } | ||
|
|
||
| return "", nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteAndReadSandboxToolConfig(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| if err := writeSandboxToolConfig("sbx"); err != nil { | ||
| t.Fatalf("writeSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "sbx" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want %q", got, "sbx") | ||
| } | ||
|
|
||
| configPath, err := dmrConfigPath() | ||
| if err != nil { | ||
| t.Fatalf("dmrConfigPath() error = %v", err) | ||
| } | ||
|
|
||
| content, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| t.Fatalf("ReadFile(%q) error = %v", configPath, err) | ||
| } | ||
|
|
||
| want := "[sandbox]\ntool = \"sbx\"\n" | ||
| if string(content) != want { | ||
| t.Fatalf("config content = %q, want %q", string(content), want) | ||
| } | ||
| } | ||
|
|
||
| func TestReadSandboxToolConfigMissingFile(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want empty string", got) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateSandboxToolAllowsSbx(t *testing.T) { | ||
| if err := validateSandboxTool("sbx"); err != nil { | ||
| t.Fatalf("validateSandboxTool() error = %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateSandboxToolRejectsUnsupportedTool(t *testing.T) { | ||
| err := validateSandboxTool("firejail") | ||
| if err == nil { | ||
| t.Fatal("validateSandboxTool() error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandRejectsUnsupportedKey(t *testing.T) { | ||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"unsupported.key", "sbx"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("config command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandRejectsUnsupportedTool(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"sandbox.tool", "firejail"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("config command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandWritesConfig(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"sandbox.tool", "sbx"}) | ||
|
|
||
| if err := cmd.Execute(); err != nil { | ||
| t.Fatalf("config command error = %v", err) | ||
| } | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "sbx" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want %q", got, "sbx") | ||
| } | ||
| } | ||
|
|
||
| func TestLaunchCommandRequiresConfiguredSandboxTool(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newLaunchCmd() | ||
| cmd.SetArgs([]string{"opencode"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("launch command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestLaunchCommandUsesConfiguredSandboxTool(t *testing.T) { | ||
| configDir := t.TempDir() | ||
| binDir := t.TempDir() | ||
| outputPath := filepath.Join(t.TempDir(), "output.txt") | ||
|
|
||
| t.Setenv("XDG_CONFIG_HOME", configDir) | ||
| t.Setenv("TEST_OUTPUT", outputPath) | ||
|
|
||
| sbxPath := filepath.Join(binDir, "sbx") | ||
| sbxScript := "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$TEST_OUTPUT\"\n" | ||
|
|
||
| if err := os.WriteFile(sbxPath, []byte(sbxScript), 0o755); err != nil { | ||
| t.Fatalf("WriteFile(%q) error = %v", sbxPath, err) | ||
| } | ||
|
|
||
| oldPath := os.Getenv("PATH") | ||
| t.Setenv("PATH", binDir+string(os.PathListSeparator)+oldPath) | ||
|
|
||
| if err := writeSandboxToolConfig("sbx"); err != nil { | ||
| t.Fatalf("writeSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| cmd := newLaunchCmd() | ||
| cmd.SetArgs([]string{"opencode", "--", "--help"}) | ||
|
|
||
| if err := cmd.Execute(); err != nil { | ||
| t.Fatalf("launch command error = %v", err) | ||
| } | ||
|
|
||
| content, err := os.ReadFile(outputPath) | ||
| if err != nil { | ||
| t.Fatalf("ReadFile(%q) error = %v", outputPath, err) | ||
| } | ||
|
|
||
| want := "opencode\n--help\n" | ||
| if string(content) != want { | ||
| t.Fatalf("sandbox output = %q, want %q", string(content), want) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping this wouldn't be removed we could keep this like this without removing if not it would break the style of
dmr configordocker model configstyle .What i suggest is we register it as a sub command in config. so one can
do dmr config sandbox.tool ... or docker model config sandbox.toollike
c.AddCommand(newSandboxConfigCmd())(note that sandbox.tool is not lke a cmd style format but not sure in this case there is any other approach)