forked from ivpusic/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
123 lines (98 loc) · 2.61 KB
/
conf.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"time"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
defaultConfigPath string = ".rerun.json"
)
var (
verbose = kingpin.Flag("verbose", "Verbose mode. It will show rerun internal messages. Default: false").Short('v').Bool()
ignore = kingpin.Flag("ignore", "List of ignored files and directories.").Default("").Short('i').String()
args = kingpin.Flag("args", "Application arguments.").Default("").Short('a').String()
suffixes = kingpin.Flag("suffixes", "File suffixes to watch.").Short('s').String()
confPath = kingpin.Flag("config", "JSON configuration location").Short('c').String()
test = kingpin.Flag("test", "Run tests").Short('t').Bool()
attrib = kingpin.Flag("attrib", "Also watch attribute changes").Bool()
)
type config struct {
Ignore []string
Args []string
Suffixes []string
Test bool
Attrib bool
build string
}
func newConfig() (*config, error) {
if len(*confPath) > 0 {
return parseConf(*confPath)
}
if _, err := os.Stat(defaultConfigPath); err != nil {
if os.IsNotExist(err) {
return new(config), nil
}
return nil, err
}
return parseConf(defaultConfigPath)
}
func parseConf(path string) (*config, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Did not find specified configuration file %s", path)
}
conf := &config{}
err = json.Unmarshal(content, &conf)
if err != nil {
return nil, fmt.Errorf("Error while unmarshaling %s", err.Error())
}
return conf, nil
}
func loadConfiguration() (*config, error) {
if !TEST_MODE {
kingpin.Version("1.0.0")
kingpin.Parse()
}
conf, err := newConfig()
if err != nil {
return nil, err
}
if len(*ignore) > 0 {
conf.Ignore = append(conf.Ignore, strings.Split(*ignore, ",")...)
}
if len(*args) > 0 {
conf.Args = append(conf.Args, strings.Split(*args, ",")...)
}
if len(*suffixes) > 0 {
conf.Suffixes = append(conf.Suffixes, strings.Split(*suffixes, ",")...)
}
if len(conf.Suffixes) == 0 {
conf.Suffixes = append(conf.Suffixes, ".go")
}
if test != nil {
conf.Test = *test
}
if attrib != nil {
conf.Attrib = *attrib
}
buildName := "application-build-" + strconv.FormatInt(time.Now().UnixNano(), 10)
if runtime.GOOS == "windows" {
buildName += ".exe"
}
conf.build, err = convertAbsolute(buildName)
if err != nil {
return nil, err
}
// ignore build files
conf.Ignore = append(conf.Ignore, conf.build)
// make absolute paths out of ignored files
conf.Ignore = parseGlobs(conf.Ignore)
conf.Ignore = convertAbsolutes(conf.Ignore)
return conf, nil
}