/
cfg.go
60 lines (50 loc) · 1.14 KB
/
cfg.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
package cfg
import (
_ "embed"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
//go:embed versions.yaml
var yamlFile []byte
var Data YamlData
var ConfigFile string
type YamlData struct {
Editor string
Commands []Command
}
type Command struct {
Name string `yaml:"name"`
VersionFlag string `yaml:"flag"`
VersionLine int `yaml:"line"`
}
func init() {
var (
cfgdir string
err error
)
log.SetFlags(0)
// Determine path for the "versions.yaml" file
// Create it if undefined
if cfgdir, err = os.UserConfigDir(); err != nil {
log.Fatalln("could not locate the user config folder")
}
cfgdir = filepath.Join(cfgdir, "versions")
os.MkdirAll(cfgdir, 0755)
// Read the config file if it exists, otherwise copy the embedded yaml
ConfigFile = filepath.Join(cfgdir, "versions.yaml")
ConfigData, err := os.ReadFile(ConfigFile)
if err != nil {
if os.IsNotExist(err) {
os.WriteFile(ConfigFile, yamlFile, 0664)
ConfigData = yamlFile
} else {
log.Fatalf("could not read the config file:%T", err)
}
}
err = yaml.Unmarshal(ConfigData, &Data)
if err != nil {
log.Fatalf("could not load the config file: %v", err)
}
}