diff --git a/README.md b/README.md index ac3c6e9..aa5a1ca 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,9 @@ go install github.com/electrofocus/sandbox@latest To open editor with fresh project run ``` sandbox +``` + +By default, project is located in temporary directory. Pass a path to existing directory as command argument to use custom location for project +``` +sandbox /Users/John/Documents ``` \ No newline at end of file diff --git a/main.go b/main.go index 01963f5..1b420f6 100644 --- a/main.go +++ b/main.go @@ -8,35 +8,40 @@ import ( ) func main() { - path, err := os.MkdirTemp("", "sandbox_*") + var dir string + if len(os.Args) > 1 { + dir = os.Args[1] + } + + dir, err := os.MkdirTemp(dir, "sandbox_*") if err != nil { fmt.Printf("can't create temporary directory (%s)", err) return } - f1, err := os.Create(filepath.Join(path, "main.go")) + f, err := os.Create(filepath.Join(dir, "main.go")) if err != nil { fmt.Printf("can't create main.go file (%s)", err) return } - defer f1.Close() - f1.WriteString(`package main + defer f.Close() + f.WriteString(`package main func main() { } `) - os.Chdir(path) + os.Chdir(dir) execCmd("go", "mod", "init", "sandbox") if editor := os.Getenv("EDITOR"); editor != "" { - execCmd(editor, f1.Name()) + execCmd(editor, f.Name()) return } - execCmd("code", path, "--goto", f1.Name()+":4:2") + execCmd("code", dir, "--goto", f.Name()+":4:2") } func execCmd(name string, args ...string) {