-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
111 lines (90 loc) · 2.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
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
package usher
import (
"fmt"
"log"
"os"
"gopkg.in/yaml.v2"
)
type Globals struct {
ConfigPath string `help:"Path to configuration yaml file." name:"config" type:"existingfile" yaml:"-"`
Debug bool `help:"Enable debug mode."`
DryRun bool `help:"Show actions that would be performed without doing anything."`
Copy bool `help:"Copy files to destination instead of using hard links."`
FileMapperRef string `help:"FileMapper type or external executable to map files from src to dest." name:"mapper" yaml:"mapper"`
SrcDir string `yaml:"src" kong:"-"`
DestDir string `yaml:"dest" kong:"-"`
}
type DirArgs struct {
SrcDir string `arg:"" help:"Source directory to watch/process." type:"path"`
DestDir string `arg:"" help:"Destination directory." type:"path"`
}
type WatchConfig struct {
EventBufferSize int `help:"Size of file event buffer (if buffer is full events are dropped). Default 1000."`
}
type Config struct {
Globals `yaml:",inline"`
RootPathMappings map[string]string
FileMapper FileMapper `yaml:"-"`
Watch WatchConfig
}
func (c *Config) Print() {
yamlBytes, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
fmt.Println(string(yamlBytes))
}
func (c *Config) UnmarshalConfigFile(configPath string) {
if len(configPath) == 0 {
return
}
configBytes, err := os.ReadFile(configPath)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(configBytes, &c)
if err != nil {
panic(err)
}
return
}
func (c *Config) ApplyCliConfig(globals *Globals, dirArgs *DirArgs) {
if globals.Debug {
c.Debug = globals.Debug
}
if globals.DryRun {
c.DryRun = globals.DryRun
}
if globals.Copy {
c.Copy = globals.Copy
}
if len(globals.FileMapperRef) > 0 {
c.FileMapperRef = globals.FileMapperRef
}
if len(dirArgs.SrcDir) > 0 {
c.SrcDir = dirArgs.SrcDir
}
if len(dirArgs.DestDir) > 0 {
c.DestDir = dirArgs.DestDir
}
}
func GetConfig(globals *Globals, dirArgs *DirArgs) Config {
config := Config{}
if len(globals.ConfigPath) == 0 {
//use default usher-config.yml if it exists
if _, err := os.Stat("./usher-config.yml"); err == nil {
globals.ConfigPath = "./usher-config.yml"
}
}
config.UnmarshalConfigFile(globals.ConfigPath)
config.ApplyCliConfig(globals, dirArgs)
fileMapper, err := GetFileMapper(config.FileMapperRef, config.Debug)
if err != nil {
log.Fatal(err)
}
config.FileMapper = fileMapper
if config.Debug {
config.Print()
}
return config
}