This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
174 lines (134 loc) · 3.54 KB
/
config.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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
)
type Config struct {
Interface InterfaceConfig `json:"interface,omitempty"`
API APIConfig `json:"api,omitempty"`
Misc MiscConfig `json:"misc,omitempty"`
}
type InterfaceConfig struct {
Color bool `json:"color"`
}
type APIConfig struct {
Endpoint string `json:"endpoint,omitempty"`
Key string `json:"key,omitempty"`
}
type MiscConfig struct {
DisableUpdateCheck bool `json:"disable_update_check,omitempty"`
}
func LoadConfig() (*Config, error) {
config := DefaultConfig()
filePath := ConfigPath()
if err := config.CheckPermissions(filePath); err != nil {
return nil, err
}
p.Debug(1, "loading configuration from %s", filePath)
err := config.LoadFile(filePath)
if errors.Is(err, os.ErrNotExist) {
if err := config.Write(); err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("cannot load %q: %w", filePath, err)
}
return config, nil
}
func (c *Config) Write() error {
filePath := ConfigPath()
p.Debug(1, "writing configuration to %s", filePath)
dirPath := filepath.Dir(filePath)
if err := os.MkdirAll(dirPath, 0700); err != nil {
return fmt.Errorf("cannot create directory %s: %w", dirPath, err)
}
return c.WriteFile(filePath)
}
func ConfigPath() string {
if path := os.Getenv("EVCLI_CONFIG_PATH"); path != "" {
return path
}
homePath, err := os.UserHomeDir()
if err != nil {
p.Fatal("cannot locate user home directory: %v", err)
}
return path.Join(homePath, ".config", "evcli", "config.json")
}
func DefaultConfig() *Config {
return &Config{
Interface: InterfaceConfig{
Color: true,
},
API: APIConfig{
Endpoint: "https://api.eventline.net",
},
}
}
func (c *Config) CheckPermissions(filePath string) error {
info, err := os.Stat(filePath)
if errors.Is(err, os.ErrNotExist) {
return nil
} else if err != nil {
return fmt.Errorf("cannot stat %q: %w", filePath, err)
}
mode := uint32(info.Mode().Perm())
// 8 7 6 5 4 3 2 1 0
// +---+---+---+---+---+---+---+---+---+
// | UR| UW| UX| GR| GW| GX| OR| OW| OX|
// +---+---+---+---+---+---+---+---+---+
if (mode & (1 << 2)) != 0 {
return fmt.Errorf("%q must not be world readable", filePath)
}
if (mode & (1 << 5)) != 0 {
return fmt.Errorf("%q must not be group readable", filePath)
}
return nil
}
func (c *Config) LoadFile(filePath string) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return fmt.Errorf("cannot read file: %w", err)
}
return c.LoadData(data)
}
func (c *Config) LoadData(data []byte) error {
if err := json.Unmarshal(data, c); err != nil {
return fmt.Errorf("cannot parse json data: %w", err)
}
return nil
}
func (c *Config) WriteFile(filePath string) error {
var buf bytes.Buffer
e := json.NewEncoder(&buf)
e.SetIndent("", " ")
if err := e.Encode(c); err != nil {
return fmt.Errorf("cannot encode configuration: %w", err)
}
if err := ioutil.WriteFile(filePath, buf.Bytes(), 0600); err != nil {
return fmt.Errorf("cannot write file: %w", err)
}
return nil
}
func (c *Config) GetEntry(name string) (string, error) {
e, found := ConfigEntries[name]
if !found {
return "", fmt.Errorf("unknown configuration entry %q", name)
}
return e.Get(c), nil
}
func (c *Config) SetEntry(name, value string) error {
e, found := ConfigEntries[name]
if !found {
return fmt.Errorf("unknown configuration entry %q", name)
}
if err := e.Set(c, value); err != nil {
return fmt.Errorf("invalid value: %v", err)
}
return nil
}