-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathuserconfig.go
More file actions
188 lines (164 loc) · 3.78 KB
/
userconfig.go
File metadata and controls
188 lines (164 loc) · 3.78 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"github.com/writeas/writeas-cli/fileutils"
"go.code.as/writeas.v2"
"gopkg.in/ini.v1"
"io/ioutil"
"path/filepath"
"reflect"
"fmt"
"os"
"strings"
)
const (
userConfigFile = "config.ini"
userFile = "user.json"
)
type (
APIConfig struct {
}
PostsConfig struct {
Directory string `ini:"directory"`
}
UserConfig struct {
API APIConfig `ini:"api"`
Posts PostsConfig `ini:"posts"`
}
)
func loadConfig() (*UserConfig, error) {
// TODO: load config to var shared across app
cfg, err := ini.LooseLoad(filepath.Join(userDataDir(), userConfigFile))
if err != nil {
return nil, err
}
// Parse INI file
uc := &UserConfig{}
err = cfg.MapTo(uc)
if err != nil {
return nil, err
}
return uc, nil
}
func saveConfig(uc *UserConfig) error {
cfg := ini.Empty()
err := ini.ReflectFrom(cfg, uc)
if err != nil {
return err
}
return cfg.SaveTo(filepath.Join(userDataDir(), userConfigFile))
}
func loadUser() (*writeas.AuthUser, error) {
fname := filepath.Join(userDataDir(), userFile)
userJSON, err := ioutil.ReadFile(fname)
if err != nil {
if !fileutils.Exists(fname) {
// Don't return a file-not-found error
return nil, nil
}
return nil, err
}
// Parse JSON file
u := &writeas.AuthUser{}
err = json.Unmarshal(userJSON, u)
if err != nil {
return nil, err
}
return u, nil
}
func saveUser(u *writeas.AuthUser) error {
// Marshal struct into pretty-printed JSON
userJSON, err := json.MarshalIndent(u, "", " ")
if err != nil {
return err
}
// Save file
err = ioutil.WriteFile(filepath.Join(userDataDir(), userFile), userJSON, 0600)
if err != nil {
return err
}
return nil
}
// Prints all values of the given struct
// For subfields: the field name is separated with dots (ex: Posts.Directory=)
func printConfig(x interface{}, prefix string, showEmptyValues bool) {
values := reflect.ValueOf(x)
if values.Kind() == reflect.Ptr {
values = values.Elem()
}
typ := values.Type()
for i := 0; i < typ.NumField(); i++ {
val := values.Field(i)
name := typ.Field(i).Name
if prefix != "" {
name = prefix + "." + name
}
if(val.Kind() == reflect.Struct) {
printConfig(val.Interface(), name, showEmptyValues)
} else {
if showEmptyValues || val.Interface() != reflect.Zero(val.Type()).Interface() {
fmt.Printf("%s=%v\n", name, val)
}
}
}
}
// Get the value of a given field
// For subfields: the name should be separated with dots (ex: Posts.Directory)
func getConfigField(x interface{}, name string) (*reflect.Value, error) {
path := strings.Split(name, ".")
values := reflect.ValueOf(x)
if values.Kind() == reflect.Ptr {
values = values.Elem()
}
for _, part := range path {
values = values.FieldByName(part)
if !values.IsValid() {
err := fmt.Errorf("error: key does not contain a section: %v", name)
return nil, err
}
}
if values.Kind() == reflect.Struct {
err := fmt.Errorf("error: key does not contain a section: %v", name)
return nil, err
}
return &values, nil
}
// Opens an editor to modify the config file
func composeConfig() error {
filename := filepath.Join(userDataDir(), userConfigFile)
// Open the editor
cmd := editPostCmd(filename)
if cmd == nil {
fmt.Println(noEditorErr)
return nil
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Start(); err != nil {
if debug {
panic(err)
} else {
Errorln("Error starting editor: %s", err)
return nil
}
}
// Wait until the editor is closed
if err := cmd.Wait(); err != nil {
if debug {
panic(err)
} else {
Errorln("Editor finished with error: %s", err)
return nil
}
}
// Check if the config file is valid
_, err := loadConfig()
if err != nil {
if debug {
panic(err)
} else {
Errorln("Error loading config: %s", err)
return nil
}
}
return nil
}