-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
112 lines (92 loc) · 2.77 KB
/
parse.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
package config
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"gopkg.in/yaml.v2"
)
// ParseFromFile reads the align config file from the home directory. It returns
// any errors it encounters with parsing the file.
func ParseFromFile() (*Config, error) {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("Failed getting home directory: %v", err.Error())
}
conf := defaultConfig
dExists, err := DirExists()
if err != nil {
return nil, fmt.Errorf("config: parse from file: dir exists: %v", err.Error())
}
if !dExists {
err := CreateDir()
if err != nil {
return nil, fmt.Errorf("config: parse from file: create config dir: %v", err.Error())
}
return &conf, nil
}
fExists, err := FileExists()
if err != nil {
return nil, fmt.Errorf("parse from file: file exists: %v", err.Error())
}
if !fExists {
return &conf, nil
}
b, err := ioutil.ReadFile(filepath.Join(usr.HomeDir, confDir, confFile))
if err != nil {
return nil, fmt.Errorf("Failed to read config file: %v", err.Error())
}
err = yaml.Unmarshal(b, &conf)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal config file: %v", err.Error())
}
return &conf, nil
}
// DirExists returns a bool and error representing whether or not a config
// directory exists for the current user, and any errors it encounters with
// statting the existence of the directory.
func DirExists() (bool, error) {
usr, err := user.Current()
if err != nil {
return false, fmt.Errorf("Failed getting home directory: %v", err.Error())
}
_, err = os.Stat(filepath.Join(usr.HomeDir, confDir))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("Failed to confirm config dir existence: %v", err.Error())
}
return true, nil
}
// FileExists returns a bool and error representing whether or not a
// config file exists for the current user, and any errors it encounters with
// statting the existence of the file.
func FileExists() (bool, error) {
usr, err := user.Current()
if err != nil {
return false, fmt.Errorf("Failed getting home directory: %v", err.Error())
}
_, err = os.Stat(filepath.Join(usr.HomeDir, confDir, confFile))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("Failed to confirm config file existence: %v", err.Error())
}
return true, nil
}
// CreateDir creates the config directory and all necessary parent directories
// missing. It returns any error it encounters with creating the directory.
func CreateDir() error {
usr, err := user.Current()
if err != nil {
return fmt.Errorf("config: get home directory: %v", err.Error())
}
err = os.MkdirAll(filepath.Join(usr.HomeDir, confDir), 0700)
if err != nil {
return fmt.Errorf("config: write file: %v", err.Error())
}
return nil
}