Просмотр исходного кода

restart the command when the file changes.

only works when the program has no errors, i think.
Lucas Stadler лет назад: 11
Родитель
Сommit
a37990e2a4
1 измененных файлов с 34 добавлено и 3 удалено
  1. 34 3
      go/qst.go

+ 34 - 3
go/qst.go

7
import "os/exec"
7
import "os/exec"
8
import "path"
8
import "path"
9
import "strings"
9
import "strings"
10
import "sync"
10
import "time"
11
import "time"
11
12
12
/*
13
/*
30
}
31
}
31
32
32
func main() {
33
func main() {
34
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
35
33
	flag.Usage = func() {
36
	flag.Usage = func() {
34
		fmt.Fprintf(os.Stderr, "Usage: %s <file>\n", os.Args[0])
37
		fmt.Fprintf(os.Stderr, "Usage: %s <file>\n", os.Args[0])
35
		flag.PrintDefaults()
38
		flag.PrintDefaults()
59
	}
62
	}
60
}
63
}
61
64
62
func runAndWatch(file string, cmd string) {
65
func runAndWatch(file string, cmdLine string) {
63
	// run command, if file changes (mtime) restart command
66
	// run command, if file changes (mtime) restart command
64
	lastMtime := time.Unix(0, 0)
67
	lastMtime := time.Now()
68
	cmd := ShellCmd(cmdLine)
69
	cmd.Start()
70
65
	for {
71
	for {
66
		info, err := os.Stat(file)
72
		info, err := os.Stat(file)
67
		if err != nil {
73
		if err != nil {
71
		mtime := info.ModTime()
77
		mtime := info.ModTime()
72
		if mtime.After(lastMtime) {
78
		if mtime.After(lastMtime) {
73
			log.Printf("%s changed, rerunning", file)
79
			log.Printf("%s changed, rerunning", file)
74
			runShellCmd(cmd)
80
			cmd.Restart()
75
		}
81
		}
76
82
77
		lastMtime = mtime
83
		lastMtime = mtime
79
	}
85
	}
80
}
86
}
81
87
88
type RestartableCommand struct {
89
	Cmd  *exec.Cmd
90
	Lock sync.Mutex
91
	Name string
92
	Args []string
93
}
94
95
func ShellCmd(cmd string) RestartableCommand {
96
	return RestartableCommand{nil, sync.Mutex{}, "sh", []string{"-c", cmd}}
97
}
98
99
func (c *RestartableCommand) Start() {
100
	c.Cmd = exec.Command(c.Name, c.Args...)
101
	c.Lock.Lock()
102
	go func() {
103
		c.Cmd.Run()
104
		c.Lock.Unlock()
105
	}()
106
}
107
108
func (c *RestartableCommand) Restart() {
109
	c.Cmd.Process.Kill()
110
	c.Start()
111
}
112
82
func runShellCmd(cmd string) {
113
func runShellCmd(cmd string) {
83
	log.Printf("Running: `%s'", cmd)
114
	log.Printf("Running: `%s'", cmd)
84
	output, err := exec.Command("sh", "-c", cmd).CombinedOutput()
115
	output, err := exec.Command("sh", "-c", cmd).CombinedOutput()