This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_file.go
109 lines (89 loc) · 2.06 KB
/
config_file.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
/*
Copyright 2016 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package doit
import (
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"gopkg.in/yaml.v2"
)
const (
configFile = ".doctlcfg"
)
// ConfigFile is a doit config file.
type ConfigFile struct {
location string
}
// NewConfigFile creates an instance of ConfigFile.
func NewConfigFile() (*ConfigFile, error) {
usr, err := user.Current()
if err != nil {
return nil, err
}
location := filepath.Join(usr.HomeDir, configFile)
return &ConfigFile{
location: location,
}, nil
}
// Set sets a ConfigFile key to a value. The value should be something
// that serializes to a valid YAML value.
func (cf *ConfigFile) Set(key string, val interface{}) error {
c, err := cf.Open()
if err != nil {
switch err.(type) {
case *os.PathError:
err := cf.createConfigFile()
if err != nil {
return err
}
c, _ = cf.Open()
default:
return err
}
}
b, err := ioutil.ReadAll(c)
if err != nil {
return err
}
var m map[string]interface{}
err = yaml.Unmarshal(b, &m)
if err != nil {
return err
}
if m == nil {
m = map[string]interface{}{}
}
m[key] = val
out, err := yaml.Marshal(m)
if err != nil {
return err
}
return ioutil.WriteFile(cf.location, out, 0600)
}
// Open opens a ConfigFile.
func (cf *ConfigFile) Open() (io.Reader, error) {
_, err := os.Stat(cf.location)
if err != nil {
return nil, err
}
return os.Open(cf.location)
}
func (cf *ConfigFile) createConfigFile() error {
f, err := os.Create(cf.location)
if err != nil {
return err
}
return f.Close()
}