-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
217 lines (168 loc) · 4.33 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// go-crypto-market-ui
// Command-line utility to track cryptocurrencies in realtime.
//
// Copyright 2020-2024, Marc S. Brooks (https://mbrooks.info)
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license.php
//
package lib
import (
"log"
"os"
"os/user"
"path"
"strconv"
"io/fs"
yaml "gopkg.in/yaml.v2"
)
// ConfigHolding declared data types.
type ConfigHolding struct {
Units float64 `yaml:"units"`
Price float64 `yaml:"price"`
}
type symbol []ConfigHolding
// ConfigSymbol declared data types.
type ConfigSymbol map[string]symbol
// ConfigOpts declared data types.
type ConfigOpts struct {
ServiceName string `yaml:"service"`
APIKey string `yaml:"apiKey"`
Currency string `yaml:"currency"`
Language string `yaml:"language"`
RefreshRate int64 `yaml:"refreshRate"`
Symbols ConfigSymbol `yaml:"symbols"`
}
// Config declared data types.
type Config struct {
options ConfigOpts
file string
}
// NewConfig create a new config instance.
func NewConfig(serviceName string) *Config {
config := &Config{}
config.file = filePath(".crypto-market-ui.yml")
config.init(serviceName)
return config
}
// Options returns ConfigOpts name/value(s).
func (config *Config) Options() ConfigOpts {
return config.options
}
// ServiceName sets/returns the option value.
func (config *Config) ServiceName(v ...string) string {
if len(v) == 1 {
config.options.ServiceName = v[0]
}
return config.options.ServiceName
}
// APIKey sets/returns the option value.
func (config *Config) APIKey(v ...string) string {
if len(v) == 1 {
config.options.APIKey = v[0]
}
return config.options.APIKey
}
// Currency sets/returns the option value.
func (config *Config) Currency(v ...string) string {
if len(v) == 1 {
config.options.Currency = v[0]
}
return config.options.Currency
}
// Language sets/returns the option value.
func (config *Config) Language(v ...string) string {
if len(v) == 1 {
config.options.Language = v[0]
}
return config.options.Language
}
// RefreshRate sets/returns the option value.
func (config *Config) RefreshRate(v ...string) int64 {
if len(v) == 1 {
i, err := strconv.ParseInt(v[0], 10, 64)
if err != nil {
i = 15
}
config.options.RefreshRate = i
}
return config.options.RefreshRate
}
// Symbols sets/returns the option value.
func (config *Config) Symbols(v ...ConfigSymbol) ConfigSymbol {
if len(v) == 1 {
config.options.Symbols = v[0]
}
return config.options.Symbols
}
// Save the config field values.
func (config *Config) Save() {
config.write(config.options)
}
// IsValid checks config values exist.
func (config *Config) IsValid() bool {
var valid bool
if len(config.ServiceName()) > 0 && len(config.APIKey()) > 0 && len(config.Currency()) > 0 && len(config.Language()) > 0 && len(config.Symbols()) > 0 && config.RefreshRate() > 0 {
valid = true
}
return valid
}
// Initialize a new config, if none exists.
func (config *Config) init(serviceName string) {
_, err := os.Stat(config.file)
if os.IsNotExist(err) {
config.create()
}
config.read(serviceName)
}
// Create a new config with default values.
func (config *Config) create() {
config.write(
ConfigOpts{
ServiceName: "CoinMarketCap",
Currency: "USD",
Language: "en",
Symbols: ConfigSymbol{"BTC": make([]ConfigHolding, 1)},
RefreshRate: 15,
},
)
}
// Read selected config YAML key/value pairs.
func (config *Config) read(serviceName string) {
data, err := os.ReadFile(config.file)
if err != nil {
log.Fatal("Cannot read ", err)
}
options := []ConfigOpts{}
err = yaml.Unmarshal(data, &options)
if err != nil {
log.Fatal("Cannot decode ", err)
}
var option ConfigOpts
for i := 0; i < len(options); i++ {
option = options[i]
if serviceName == option.ServiceName {
config.options = option
break
}
}
}
// Write the locally hosted config file.
func (config *Config) write(file ConfigOpts) {
apis := []ConfigOpts{}
// TODO: Support multiple services.
apis = append(apis, file)
data, err := yaml.Marshal(&apis)
if err != nil {
log.Fatal("Cannot encode ", err)
}
os.WriteFile(config.file, data, fs.FileMode(0644))
}
// Returns the file relative path to $HOME
func filePath(name string) string {
usr, err := user.Current()
if err != nil {
log.Fatal("Cannot open path ", err)
}
return path.Join(usr.HomeDir, name)
}