@@ -20,8 +20,8 @@ type ConfigParser interface {
20
20
Marshal (cfg * types.Config ) ([]byte , error )
21
21
}
22
22
23
- // Load to load config from confpath with specified parser.
24
- func Load (confpath string , parser ConfigParser ) (cfg * types.Config , err error ) {
23
+ // Load to load config from confPath with specified parser.
24
+ func Load (confPath string , parser ConfigParser ) (cfg * types.Config , err error ) {
25
25
if parser == nil {
26
26
parser = NewTOMLParser ()
27
27
}
@@ -30,22 +30,15 @@ func Load(confpath string, parser ConfigParser) (cfg *types.Config, err error) {
30
30
r io.Reader
31
31
)
32
32
cfg = new (types.Config )
33
- p , create := precheckConfigDirectory (confpath )
34
- if create {
35
- cfg = defaultConf
36
- if err = Save (p , cfg , parser ); err != nil {
37
- return nil , fmt .Errorf ("init config file failed: %v" , err )
38
- }
39
- }
40
-
33
+ p := precheckConfigDirectory (confPath )
41
34
r , err = os .OpenFile (p , os .O_RDONLY , 0777 )
42
35
err = parser .Unmarshal (r , cfg )
43
36
44
37
return cfg , err
45
38
}
46
39
47
40
// Save to save config with specified parser.
48
- func Save (confpath string , cfg * types.Config , parser ConfigParser ) error {
41
+ func Save (confPath string , cfg * types.Config , parser ConfigParser ) error {
49
42
if parser == nil {
50
43
parser = NewTOMLParser ()
51
44
}
@@ -54,12 +47,14 @@ func Save(confpath string, cfg *types.Config, parser ConfigParser) error {
54
47
if err != nil {
55
48
return err
56
49
}
57
- w , err := os .OpenFile (confpath , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0777 )
50
+
51
+ p := precheckConfigDirectory (confPath )
52
+ w , err := os .OpenFile (p , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0777 )
58
53
if err != nil {
59
54
return err
60
55
}
61
56
62
- _ , err = fmt .Fprint (w , data )
57
+ _ , err = fmt .Fprint (w , string ( data ) )
63
58
return err
64
59
}
65
60
@@ -106,32 +101,16 @@ const (
106
101
)
107
102
108
103
// precheckConfigDirectory could parse filename and path from configDirectory.
109
- func precheckConfigDirectory (configDirectory string ) (s string , create bool ) {
110
- if configDirectory != "" {
111
- return configDirectory , false
112
- }
113
-
114
- // generate default config directory
115
- home , err := os .UserHomeDir ()
104
+ func precheckConfigDirectory (configDirectory string ) string {
105
+ fi , err := os .Stat (configDirectory )
116
106
if err != nil {
117
- log .Errorf ("get user home failed: %v" , err )
107
+ log .Fatalf ("could not stat config file: %v" , err )
108
+ panic ("could not reach" )
118
109
}
119
110
120
- configDirectory = filepath .Join (home , _defaultConfigDirectory )
121
- s = filepath .Join (configDirectory , _configFilename )
122
- if _ , err = os .Stat (configDirectory ); err == nil {
123
- return s , false
111
+ if fi .IsDir () {
112
+ return filepath .Join (configDirectory , _configFilename )
124
113
}
125
114
126
- // check directory exists or not.
127
- if os .IsNotExist (err ) {
128
- // could not find the directory, then mkdir
129
- if err = os .MkdirAll (configDirectory , 0777 ); err != nil {
130
- panic (err )
131
- }
132
- return s , true
133
- }
134
-
135
- // other errors while stat config directory.
136
- panic (err )
115
+ return configDirectory
137
116
}
0 commit comments