-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
48 lines (40 loc) · 1.31 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
package main
import (
"github.com/spf13/viper"
"io/ioutil"
"log"
)
// Create a default config file if not found config.toml
func loadConfigIfExists() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("toml") // REQUIRED if the config file does not have the extension in the name
//viper.AddConfigPath("/etc/rsync-os/") // path to look for the config file in
//viper.AddConfigPath("$HOME/.rsync-os") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config File not found
createSampleConfig()
log.Fatalln("Config does not exist, a sample of config was created")
} else {
// Found but got errors
log.Fatalln(err)
}
}
}
func createSampleConfig() {
confSample := []byte(
`title = "configuration of rsync-os"
# [object storage's name]
[minio]
endpoint = "127.0.0.1:9000"
keyAccess = "minioadmin"
keySecret = "minioadmin"
[minio.boltdb]
path = "test.db"
`)
if ioutil.WriteFile("config.toml", confSample, 0666) != nil {
log.Fatalln("Can't create a sample of config")
}
}