This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
/
config.go
203 lines (169 loc) · 4.52 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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package util
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/TheThingsNetwork/api"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)
const (
appFilename = "app"
euiKey = "eui"
idKey = "id"
)
// GetConfigFile returns the location of the configuration file.
// It checks the following (in this order):
// the --config flag
// $XDG_CONFIG_HOME/ttnctl/config.yml (if $XDG_CONFIG_HOME is set)
// $HOME/.ttnctl.yml
func GetConfigFile() string {
flag := viper.GetString("config")
xdg := os.Getenv("XDG_CONFIG_HOME")
if xdg != "" {
xdg = path.Join(xdg, "ttnctl", "config.yml")
}
home := os.Getenv("HOME")
homeyml := ""
homeyaml := ""
if home != "" {
homeyml = path.Join(home, ".ttnctl.yml")
homeyaml = path.Join(home, ".ttnctl.yaml")
}
try_files := []string{
flag,
xdg,
homeyml,
homeyaml,
}
// find a file that exists, and use that
for _, file := range try_files {
if file != "" {
if _, err := os.Stat(file); err == nil {
return file
}
}
}
// no file found, set up correct fallback
if os.Getenv("XDG_CONFIG_HOME") != "" {
return xdg
} else {
return homeyml
}
}
// GetDataDir returns the location of the data directory used for
// sotring data.
// It checks the following (in this order):
// the --data flag
// $XDG_DATA_HOME/ttnctl (if $XDG_DATA_HOME is set)
// $XDG_CACHE_HOME/ttnctl (if $XDG_CACHE_HOME is set)
// $HOME/.ttnctl
func GetDataDir() string {
file := viper.GetString("data")
if file != "" {
return file
}
xdg := os.Getenv("XDG_DATA_HOME")
if xdg != "" {
return path.Join(xdg, "ttnctl")
}
xdg = os.Getenv("XDG_CACHE_HOME")
if xdg != "" {
return path.Join(xdg, "ttnctl")
}
return path.Join(os.Getenv("HOME"), ".ttnctl")
}
func readData(file string) map[string]interface{} {
fullpath := path.Join(GetDataDir(), file)
c := make(map[string]interface{})
// Read config file
data, err := ioutil.ReadFile(fullpath)
if err != nil {
return c
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return c
}
return c
}
func writeData(file string, data map[string]interface{}) error {
fullpath := path.Join(GetDataDir(), file)
// Generate yaml contents
d, err := yaml.Marshal(&data)
if err != nil {
return fmt.Errorf("Could not generate configiguration file contents: %s", err.Error())
}
// Write to file
err = ioutil.WriteFile(fullpath, d, 0644)
if err != nil {
return fmt.Errorf("Could not write configuration file: %s", err.Error())
}
return nil
}
func setData(file, key string, data interface{}) error {
config := readData(file)
config[key] = data
return writeData(file, config)
}
// GetAppEUI returns the AppEUI that must be set in the command options or config
func GetAppEUI(ctx ttnlog.Interface) types.AppEUI {
appEUIString := viper.GetString("app-eui")
if appEUIString == "" {
appData := readData(appFilename)
eui, ok := appData[euiKey].(string)
if !ok {
ctx.Fatal("Invalid AppEUI in config file")
}
appEUIString = eui
}
if appEUIString == "" {
ctx.Fatal("Missing AppEUI. You should select an application to use with \"ttnctl applications select\"")
}
eui, err := types.ParseAppEUI(appEUIString)
if err != nil {
ctx.WithError(err).Fatal("Invalid AppEUI")
}
return eui
}
// SetAppEUI stores the app EUI preference
func SetAppEUI(ctx ttnlog.Interface, appEUI types.AppEUI) {
err := setData(appFilename, euiKey, appEUI.String())
if err != nil {
ctx.WithError(err).Fatal("Could not save app EUI")
}
}
// GetAppID returns the AppID that must be set in the command options or config
func GetAppID(ctx ttnlog.Interface) string {
appID := viper.GetString("app-id")
if appID == "" {
appData := readData(appFilename)
id, ok := appData[idKey].(string)
if !ok {
ctx.Fatal("Invalid appID in config file.")
}
appID = id
}
if appID == "" {
ctx.Fatal("Missing Application ID. You should select an application to use with \"ttnctl applications select\"")
}
if err := api.NotEmptyAndValidID(appID, "Application ID"); err != nil {
ctx.Fatal(err.Error())
}
return appID
}
// SetApp stores the app ID and app EUI preferences
func SetApp(ctx ttnlog.Interface, appID string, appEUI types.AppEUI) {
config := readData(appFilename)
config[idKey] = appID
config[euiKey] = appEUI.String()
err := writeData(appFilename, config)
if err != nil {
ctx.WithError(err).Fatal("Could not save app preference")
}
}