forked from liamg/darktile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
73 lines (57 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/version"
)
func getConfig() *config.Config {
showVersion := false
flag.BoolVar(&showVersion, "version", showVersion, "Output version information")
ignore := false
flag.BoolVar(&ignore, "ignore-config", ignore, "Ignore user config files and use defauls")
if ignore {
return &config.DefaultConfig
}
conf := loadConfigFile()
flag.StringVar(&conf.Shell, "shell", conf.Shell, "Specify the shell to use")
flag.BoolVar(&conf.DebugMode, "debug", conf.DebugMode, "Enable debug logging")
flag.BoolVar(&conf.Slomo, "slomo", conf.Slomo, "Render in slow motion (useful for debugging)")
flag.Parse()
if showVersion {
v := version.Version
if v == "" {
v = "development"
}
fmt.Println(v)
os.Exit(0)
}
return conf
}
func loadConfigFile() *config.Config {
home := os.Getenv("HOME")
if home == "" {
return &config.DefaultConfig
}
places := []string{
fmt.Sprintf("%s/.aminal.toml", home),
}
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 {
if err := ioutil.WriteFile(fmt.Sprintf("%s/.aminal.toml", home), b, 0644); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err)
}
}
return &config.DefaultConfig
}