forked from liamg/darktile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
122 lines (98 loc) · 2.71 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/version"
)
func getActuallyProvidedFlags() map[string]bool {
result := make(map[string]bool)
flag.Visit(func(f *flag.Flag) {
result[f.Name] = true
})
return result
}
func getConfig() *config.Config {
showVersion := false
ignoreConfig := false
shell := ""
debugMode := false
slomo := false
if flag.Parsed() == false {
flag.BoolVar(&showVersion, "version", showVersion, "Output version information")
flag.BoolVar(&ignoreConfig, "ignore-config", ignoreConfig, "Ignore user config files and use defaults")
flag.StringVar(&shell, "shell", shell, "Specify the shell to use")
flag.BoolVar(&debugMode, "debug", debugMode, "Enable debug logging")
flag.BoolVar(&slomo, "slomo", slomo, "Render in slow motion (useful for debugging)")
flag.Parse() // actual parsing and fetching flags from the command line
}
actuallyProvidedFlags := getActuallyProvidedFlags()
if showVersion {
v := version.Version
if v == "" {
v = "development"
}
fmt.Println(v)
os.Exit(0)
}
var conf *config.Config
if ignoreConfig {
conf = &config.DefaultConfig
} else {
conf = loadConfigFile()
}
// Override values in the configuration file with the values specified in the command line, if any.
if actuallyProvidedFlags["shell"] {
conf.Shell = shell
}
if actuallyProvidedFlags["debug"] {
conf.DebugMode = debugMode
}
if actuallyProvidedFlags["slomo"] {
conf.Slomo = slomo
}
return conf
}
func loadConfigFile() *config.Config {
usr, err := user.Current()
if err != nil {
fmt.Printf("Failed to get current user information: %s\n", err)
return &config.DefaultConfig
}
home := usr.HomeDir
if home == "" {
return &config.DefaultConfig
}
places := []string{}
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if xdgHome != "" {
places = append(places, filepath.Join(xdgHome, "aminal/config.toml"))
}
places = append(places, filepath.Join(home, ".config/aminal/config.toml"))
places = append(places, filepath.Join(home, ".aminal.toml"))
for _, place := range places {
if b, err := ioutil.ReadFile(place); err == nil {
if c, err := config.Parse(b); err == nil {
return c
}
fmt.Printf("Invalid config at %s: %s\n", place, err)
}
}
if b, err := config.DefaultConfig.Encode(); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err)
} else {
err = os.MkdirAll(filepath.Dir(places[0]), 0744)
if err != nil {
fmt.Printf("Failed to create config file directory: %s\n", err)
} else {
if err = ioutil.WriteFile(places[0], b, 0644); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err)
}
}
}
return &config.DefaultConfig
}