-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (50 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
watchDir = flag.String("watch", "", "Which directory to watch for changes to do live reload")
gopath = os.Getenv("GOPATH")
softDir = filepath.Join(gopath, "soft")
softGopath = filepath.Join(softDir, "p")
)
func main() {
flag.Parse()
if *watchDir == "" {
log.Fatal("Must specify watch dir")
}
if gopath == "" {
log.Fatal("GOPATH must be set")
}
log.Printf("Starting to rewrite %s", gopath)
os.Stderr.Write([]byte("\n"))
syncDir(filepath.Join(gopath, "src"), filepath.Join(softGopath, "src"))
os.Setenv("GOPATH", softGopath)
os.Stderr.Write([]byte("\n"))
if wd, err := os.Getwd(); err == nil && strings.HasPrefix(wd, gopath+string(os.PathSeparator)) {
newDir := softGopath + string(os.PathSeparator) + strings.TrimPrefix(wd, gopath+string(os.PathSeparator))
log.Printf("Changing current directory to %s", newDir)
os.Chdir(newDir)
}
args := flag.Args()
var cmd *exec.Cmd
if len(args) > 1 {
cmd = exec.Command(args[0], args[1:]...)
} else {
cmd = exec.Command(args[0])
}
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
log.Printf("Couldn't open stdin pipe: %v", err)
}
go watchChanges(stdin)
log.Fatal(cmd.Run())
}