-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
157 lines (130 loc) · 3.24 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Copyright (c) 2018 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2018-01-26
//
package main
import (
"fmt"
"io/ioutil"
"time"
"github.com/BurntSushi/toml"
"github.com/deanishe/awgo/util"
)
const (
// DefaultDepth is how deep to search directories by default.
// 1 means the immediate children of the specified path, 2 means
// its grandchildren, etc.
DefaultDepth = 2
// DefaultFindInterval is how often to run find
DefaultFindInterval = 5 * time.Minute
// DefaultMDFindInterval is how often to run mdfind
DefaultMDFindInterval = 5 * time.Minute
// DefaultLocateInterval is how often to run locate
DefaultLocateInterval = 24 * time.Hour
defaultConfig = `# How many directories deep to search by default.
# 0 = the directory itself
# 1 = immediate children of the directory
# 2 = grandchildren of the directory
# etc.
# default: 2
#
# depth = 2
# How long to cache the list of projects for.
# default: 5m
#
# cache-age = "5m"
# git-style glob patterns of paths to ignore.
# default: []
#
# E.g.:
#
# excludes = [
# "/Applications/*",
# "**/vim/undo/**",
# ]
# Additional paths to search with "find".
# Each search path is specified by a [[paths]] header and requires a path value.
# E.g.:
#
# [[paths]]
# path = "~/Dropbox"
#
# You can override the default depth:
#
# [[paths]]
# path = "~/Code"
# depth = 3
`
)
var conf *config
func init() {
conf = &config{
Depth: DefaultDepth,
SearchPaths: []*searchPath{},
FindInterval: DefaultFindInterval,
MDFindInterval: DefaultMDFindInterval,
LocateInterval: DefaultLocateInterval,
}
}
type config struct {
// From workflow environment variables
FindInterval time.Duration `toml:"-"`
MDFindInterval time.Duration `toml:"-"`
LocateInterval time.Duration `toml:"-"`
VSCode bool `toml:"-" env:"VSCODE"`
ActionProjectFile bool `toml:"-" env:"ACTION_PROJECT_FILE"`
// From config file
Excludes []string `toml:"excludes"`
Depth int `toml:"depth"`
SearchPaths []*searchPath `toml:"paths"`
}
type searchPath struct {
Path string `toml:"path"`
Excludes []string `toml:"excludes"`
Depth int `toml:"depth"`
}
// Copy default settings file to data directory if there is no
// existing settings file.
func initConfig() error {
if !util.PathExists(configFile) {
if err := ioutil.WriteFile(configFile, []byte(defaultConfig), 0600); err != nil {
return fmt.Errorf("write config: %w", err)
}
}
return nil
}
// Load configuration file.
func loadConfig(path string) (*config, error) {
defer util.Timed(time.Now(), "load config")
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err := toml.Unmarshal(data, &conf); err != nil {
return nil, err
}
// Load workflow variables
if err := wf.Config.To(conf); err != nil {
return nil, err
}
// Update depths and expand paths
if conf.Depth == 0 {
conf.Depth = DefaultDepth
}
for i, s := range conf.Excludes {
conf.Excludes[i] = expandPath(s)
}
for _, sp := range conf.SearchPaths {
if sp.Depth == 0 {
sp.Depth = conf.Depth
}
sp.Path = expandPath(sp.Path)
for i, s := range sp.Excludes {
sp.Excludes[i] = expandPath(s)
}
}
return conf, nil
}