-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (54 loc) · 1.85 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
package main
import (
"io/ioutil"
"log"
"github.com/spf13/viper"
)
// 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 mirrors-os"
[global]
server = "127.0.0.1:52111"
logdir = "./log"
[archlinux]
name = "archlinux"
srcroot = "rsync://mirrors.tuna.tsinghua.edu.cn/archlinux/"
src = "rsync://mirrors.tuna.tsinghua.edu.cn/archlinux/"
exclusion = ["iso/","testing/"]
cookie = "TEAMBITION_SESSIONID=xxx; TEAMBITION_SESSIONID.sig=xxx"
base = "mirrors/"
dbpath = "archlinux.db"
cron = "0 0 0,12 * * *"
[archlinuxcn]
name = "archlinuxcn"
srcroot = "rsync://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/"
src = "rsync://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/"
exclusion = []
cookie = "TEAMBITION_SESSIONID=xxx; TEAMBITION_SESSIONID.sig=xxx"
base = "mirrors/"
dbpath = "archlinuxcn.db"
cron = "0 0 6,18 * * *"
`)
if ioutil.WriteFile("config.toml", confSample, 0666) != nil {
log.Fatalln("Can't create a sample of config")
}
}