-
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 1 commit
2be8943
934299f
e1e3fb1
064d96b
93c3726
6334d03
de714be
03c6e25
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 |
|---|---|---|
|
|
@@ -2,10 +2,14 @@ | |
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
|
|
||
| "github.com/docker/cli/cli/command" | ||
|
|
@@ -27,6 +31,15 @@ func main() { | |
| } | ||
|
|
||
| func run() error { | ||
| if len(os.Args) > 1 { | ||
| switch os.Args[1] { | ||
| case "config": | ||
| return runConfig(os.Args[2:]) | ||
| case "launch": | ||
| return runLaunch(os.Args[2:]) | ||
| } | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
| } | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
|
|
||
| cli, err := command.NewDockerCli() | ||
| if err != nil { | ||
| return fmt.Errorf("unable to initialize CLI: %w", err) | ||
|
|
@@ -67,3 +80,121 @@ func newServeCmd() *cobra.Command { | |
| }, | ||
| } | ||
| } | ||
|
|
||
| func runConfig(args []string) error { | ||
| if len(args) != 2 { | ||
| return fmt.Errorf("usage: dmr config sandbox.tool <tool>") | ||
| } | ||
|
|
||
| key := args[0] | ||
| value := args[1] | ||
|
|
||
| if key != "sandbox.tool" { | ||
| return fmt.Errorf("unsupported config key %q", key) | ||
| } | ||
|
|
||
|
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. perhaps much better if we can add a list of supported tools in the top of the file and only support here like
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. Sure but lets just start with sbx for this PR, these can be follow on PRs
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. @ericcurtin yes, this was my exact point, like it's better if we can specify the supported ones, both here and the launch |
||
| return writeSandboxToolConfig(value) | ||
| } | ||
|
|
||
| func runLaunch(args []string) error { | ||
| if len(args) == 0 { | ||
| return fmt.Errorf("usage: dmr launch <tool> [args...]") | ||
| } | ||
|
|
||
| sandboxTool, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
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. same thing like above can be done here for verifying supported tools |
||
| if sandboxTool == "" { | ||
| return fmt.Errorf("sandbox.tool is not configured. Run: dmr config sandbox.tool <tool>") | ||
| } | ||
|
|
||
| cmd := exec.Command(sandboxTool, args...) | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Outdated
|
||
| cmd.Stdin = os.Stdin | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| return cmd.Run() | ||
| } | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 { | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
| return fmt.Errorf("unable to write config: %w", err) | ||
| } | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
CoderHariswar marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return "", fmt.Errorf("unable to parse config: %w", err) | ||
| } | ||
|
|
||
| return "", nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.