-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseConfig.go
More file actions
56 lines (52 loc) · 1.24 KB
/
parseConfig.go
File metadata and controls
56 lines (52 loc) · 1.24 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
func parseConfig(configFile string) (ConfigJSON, error) {
var config ConfigJSON
if configFile == "" {
// default to the normal location
homeDir, err := os.UserHomeDir()
if err != nil {
return config, err
}
configFile = filepath.Join(homeDir, ".hitec.conf")
}
// Validate if the file exists
_, err := os.Stat(configFile)
if err != nil {
// If the error is due to the file not existing
if os.IsNotExist(err) {
// Create the file
config.InfluxBucket = "Bucket Name"
config.InfluxOrg = "Org Name"
config.InfluxToken = "Security token"
config.ServerAddress = "http://serveraddress:8086"
outData, err := json.MarshalIndent(config, "", " ")
if err != nil {
return config, err
}
err = os.WriteFile(configFile, outData, 0644)
if err != nil {
return config, err
}
// Return explanation
return config, fmt.Errorf("config file not found, generated at %s", configFile)
}
return config, err
}
// File existed, read and parse it
bytes, err := os.ReadFile(configFile)
if err != nil {
return config, err
}
err = json.Unmarshal(bytes, &config)
if err != nil {
return config, err
}
// Return the config
return config, nil
}