-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
220 lines (202 loc) · 4.78 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package fresher
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/goccy/go-yaml"
)
type Config struct {
Build *BuildConfig `yaml:"build"`
Paths []*WatcherConfig `yaml:"path"`
ExcludePath *GlobalExclude `yaml:"exclude"`
Extensions Extensions `yaml:"extension"`
Interval time.Duration `yaml:"interval"`
}
type BuildConfig struct {
Target string `yaml:"target"`
Host *Host `yaml:"host"`
Output string `yaml:"output"`
Environ Environ `yaml:"env"`
Arg []string `yaml:"arg"`
WithoutRun bool `yaml:"without_run"`
BeforeCommands []*Command `yaml:"before"`
AfterCommands []*Command `yaml:"after"`
}
func (bc *BuildConfig) runBinaryPath() string {
if bc.Output != "" {
return bc.Output
}
name := "fresher_run"
var isIncludeGOOSEnv bool
for _, env := range bc.Environ {
if strings.Contains(env, "GOOS") {
isIncludeGOOSEnv = true
if strings.Contains(env, "windows") {
name += ".exe"
}
break
}
}
if !isIncludeGOOSEnv && runtime.GOOS == "windows" {
name += ".exe"
}
return filepath.Join(os.TempDir(), name)
}
func (bc *BuildConfig) buildArg() []string {
arg := []string{"build", "-o", bc.runBinaryPath()}
if len(bc.Arg) > 0 {
arg = append(arg, bc.Arg...)
}
arg = append(arg, bc.Target)
return arg
}
func (bc *BuildConfig) Commands() []Executor {
commands := []Executor{
bc.BuildCommand(),
}
if cmd := bc.RunCommand(); cmd != nil {
commands = append(commands, cmd)
}
if len(bc.BeforeCommands) > 0 {
cmds := make([]Executor, len(bc.BeforeCommands))
for idx, cmd := range bc.BeforeCommands {
cmds[idx] = cmd
}
commands = append(cmds, commands...)
}
if len(bc.AfterCommands) > 0 {
cmds := make([]Executor, len(bc.AfterCommands))
for idx, cmd := range bc.AfterCommands {
cmds[idx] = cmd
}
commands = append(commands, cmds...)
}
return commands
}
func (bc *BuildConfig) BuildCommand() Executor {
cmd := exec.Command("go", bc.buildArg()...)
cmd.Env = append(os.Environ(), bc.Environ...)
return &Command{
Name: "go",
Arg: bc.buildArg(),
Environ: append(os.Environ(), bc.Environ...),
IsAsync: false,
}
}
func (bc *BuildConfig) RunCommand() Executor {
if bc.WithoutRun {
return nil
}
if bc.Host == nil {
return &Command{
Name: bc.runBinaryPath(),
IsAsync: true,
}
}
return bc.Host.RunCommand(bc.runBinaryPath())
}
type Environ []string
func (e *Environ) UnmarshalYAML(b []byte) error {
m := make(map[string]string)
if err := yaml.Unmarshal(b, &m); err != nil {
return err
}
for k, v := range m {
*e = append(*e, fmt.Sprintf("%s=%s", k, v))
}
return nil
}
type ArgDecoder struct {
arg []string
}
type ArgDecoders []*ArgDecoder
func (a ArgDecoders) Argument() []string {
var arg []string
for _, dec := range a {
arg = append(arg, dec.arg...)
}
return arg
}
func (a *ArgDecoder) UnmarshalYAML(b []byte) error {
fmt.Println(string(b))
m := make(map[string]string)
if err := yaml.Unmarshal(b, &m); err != nil {
var s string
if err := yaml.Unmarshal(b, &s); err != nil {
return err
}
a.arg = append(a.arg, s)
return nil
}
for k, v := range m {
a.arg = append(a.arg, k, v)
}
return nil
}
func (bc *BuildConfig) UnmarshalYAML(b []byte) error {
st := struct {
Target string `yaml:"target"`
Host *Host `yaml:"host"`
Output string `yaml:"output"`
Environ Environ `yaml:"env"`
Arg ArgDecoders `yaml:"arg"`
WithoutRun bool `yaml:"without_run"`
BeforeCommands []*Command `yaml:"before"`
AfterCommands []*Command `yaml:"after"`
}{}
if err := yaml.Unmarshal(b, &st); err != nil {
var target string
if err := yaml.Unmarshal(b, &target); err != nil {
return err
}
bc.Target = target
return nil
}
bc.Target = st.Target
bc.Host = st.Host
bc.Output = st.Output
bc.Environ = st.Environ
bc.Arg = st.Arg.Argument()
bc.WithoutRun = st.WithoutRun
bc.BeforeCommands = st.BeforeCommands
bc.AfterCommands = st.AfterCommands
return nil
}
func LoadConfig(filename string) (*Config, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var conf Config
if err := yaml.Unmarshal(b, &conf); err != nil {
return nil, err
}
return &conf, nil
}
func (c *Config) Options() []OptionFunc {
if c == nil {
return nil
}
var funcs []OptionFunc
if c.Build != nil {
funcs = append(funcs, ExecTarget(c.Build))
}
if len(c.Paths) > 0 {
funcs = append(funcs, WatchConfigs(c.Paths))
}
if c.ExcludePath != nil {
funcs = append(funcs, GlobalExcludePath(c.ExcludePath))
}
if len(c.Extensions) > 0 {
funcs = append(funcs, ExtensionPaths(c.Extensions))
}
if c.Interval > 0 {
funcs = append(funcs, WatchInterval(c.Interval*time.Second))
}
return funcs
}