Skip to content

Commit

Permalink
Custom location support
Browse files Browse the repository at this point in the history
  • Loading branch information
electrofocus committed Sep 16, 2024
1 parent 308b698 commit 3bea352
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3bea352

Please sign in to comment.