-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
68 lines (55 loc) · 1.23 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
package main
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
"runtime"
)
type Config struct {
PrivateKey string `json:"privateKey"`
}
func InitConfig() {
configPath := getConfigPath()
err := os.MkdirAll(configPath, 0700)
if err != nil {
log.Fatalf("error creating config: %v", err)
}
}
func getConfigPath() string {
var configPath string
var err error
// os.UserConfigDir() returns $HOME/Library/Application Support for darwin
// set config path manually to $HOME/.config
if runtime.GOOS == "darwin" {
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
configPath = filepath.Join(home, ".config")
} else {
configPath, err = os.UserConfigDir()
if err != nil {
log.Fatal(err)
}
}
configPath = filepath.Join(configPath, "nosh")
return configPath
}
func getConfig() *Config {
var config *Config
configPath := getConfigPath()
configFilePath := filepath.Join(configPath, "config.json")
configFile, err := os.Open(configFilePath)
if err != nil {
log.Fatalf("error reading config: %v", err)
}
defer configFile.Close()
dec := json.NewDecoder(configFile)
err = dec.Decode(&config)
if err != nil && err != io.EOF {
log.Fatalf("error reading config: %v", err)
}
return config
}