forked from markbates/refresh
/
config.go
60 lines (53 loc) · 1.44 KB
/
config.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
package refresh
import (
"crypto/md5"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
)
type Configuration struct {
AppRoot string `yaml:"app_root"`
IgnoredFolders []string `yaml:"ignored_folders"`
IncludedExtensions []string `yaml:"included_extensions"`
BuildTargetPath string `yaml:"build_target_path"`
BuildPath string `yaml:"build_path"`
BuildDelay time.Duration `yaml:"build_delay"`
BinaryName string `yaml:"binary_name"`
CommandFlags []string `yaml:"command_flags"`
CommandEnv []string `yaml:"command_env"`
EnableColors bool `yaml:"enable_colors"`
LogName string `yaml:"log_name"`
Debug bool `yaml:"-"`
}
func (c *Configuration) FullBuildPath() string {
buildPath := path.Join(c.BuildPath, c.BinaryName)
if runtime.GOOS == "windows" {
if !strings.HasSuffix(strings.ToLower(buildPath), ".exe") {
buildPath += ".exe"
}
}
return buildPath
}
func (c *Configuration) Load(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(data, c)
}
func (c *Configuration) Dump(path string) error {
data, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, 0666)
}
func ID() string {
d, _ := os.Getwd()
return fmt.Sprintf("%x", md5.Sum([]byte(d)))
}