-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (82 loc) · 1.61 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"log"
"os/exec"
"path/filepath"
"os"
"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
)
type Config struct {
Target []string
}
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
var conf Config
_, err = toml.DecodeFile("tindalos.toml", &conf)
if err != nil {
panic(err)
}
var targets []string
for _, target := range conf.Target {
baseDir, err := os.Getwd()
if err != nil {
panic(err)
}
ts, err := filepath.Glob(baseDir + target)
for _, t := range ts {
targets = append(targets, t)
if err != nil {
panic(err)
}
}
}
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
continue
}
fileName := event.Name
for _, t := range targets {
if fileName == t {
err := exec.Command("goimports", "-w", fileName).Run()
if err != nil {
log.Println("Error: ", err)
}
} else {
swpFileName := filepath.Dir(t) + "/." + filepath.Base(t) + ".swp"
if swpFileName == fileName {
fmt.Println(t)
err := exec.Command("goimports", "-w", t).Run()
if err != nil {
log.Println("Error: ", err)
}
}
}
}
fmt.Println("Success!!")
case err := <-watcher.Errors:
log.Println("Error: ", err)
}
}
}()
for _, target := range conf.Target {
baseDir, err := os.Getwd()
if err != nil {
panic(err)
}
err = watcher.Add(filepath.Dir(baseDir + target))
if err != nil {
panic(err)
}
}
<-done
}