/
config.go
134 lines (111 loc) 路 2.74 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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
sdk "github.com/lorenzodisidoro/avocado-sdk"
)
// Config define fields of JSON file of root configurations
type Config struct {
Storage string `json:"storage"`
Bolt Bolt `json:"bolt"`
Redis Redis `json:"redis"`
Public string `json:"publicPath"`
}
// Bolt define bolt fields
type Bolt struct {
Path string `json:"path"`
Bucket string `json:"bucket"`
}
// Redis define redis fields
type Redis struct {
Address string `json:"address"`
Password string `json:"password"`
Db int `json:"db"`
}
// parseConfigJSON unmarshal JSON configuration
func parseConfigJSON(configPath string) (*Config, error) {
var configuration Config
// open JSON file
jsonFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
byteJSON, _ := ioutil.ReadAll(jsonFile)
defer jsonFile.Close()
json.Unmarshal(byteJSON, &configuration)
return &configuration, nil
}
// createConfigFolder creates a new folder
func createConfigFolder(path string) error {
err := os.Mkdir(path, 0755)
return err
}
// exists check if the directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func initConfiguration(configDir, configFile string) error {
config := Config{
Storage: "bolt",
Public: configDir + "/" + defaultPublicKeyName,
Bolt: Bolt{
Path: configDir + "/" + defaultBoltDB,
Bucket: defaultBoltBucker,
},
Redis: Redis{
Address: defaultRedisAddress,
Password: defaultRedisPassword,
Db: defaultRedisDB,
},
}
configDirectoryExists, err := exists(cfgDir)
if err != nil {
return err
}
if !configDirectoryExists {
createConfigFolder(cfgDir)
}
file, err := json.MarshalIndent(config, "", " ")
err = ioutil.WriteFile(configFile, file, 0644)
return err
}
func getStorageAndConfig(configFilePath string) (*sdk.StorageClient, *Config, error) {
config, err := parseConfigJSON(configFilePath)
if err != nil {
return nil, nil, err
}
storage, err := createStorageClient(config)
return storage, config, err
}
func createStorageClient(config *Config) (*sdk.StorageClient, error) {
var storage *sdk.StorageClient
switch config.Storage {
case "bolt":
storage = &sdk.StorageClient{
Bbolt: &sdk.BboltStorage{
SotoragePath: config.Bolt.Path,
BucketName: config.Bolt.Bucket,
},
}
case "redis":
storage = &sdk.StorageClient{
Redis: &sdk.RedisStorage{
Address: config.Redis.Address,
Password: config.Redis.Password,
DB: config.Redis.Db,
},
}
default:
return storage, fmt.Errorf("Storage" + config.Storage + " not supported. Storage can be 'bolt' or 'redis'.")
}
return storage, nil
}