This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
forked from elgs/filesync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars.go
96 lines (69 loc) · 1.67 KB
/
vars.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
package vars
import (
"os"
"fmt"
"log"
"strconv"
"io/ioutil"
simplejson "github.com/bitly/go-simplejson"
)
type ConfigVars struct{
Mode string
Ip string
Port int
Monitors map[string]interface{}
}
var config ConfigVars
func Init() {
var configFile string
input := Args()
if len(input) >= 1 {
configFile = input[0]
}
if(configFile != ""){
fmt.Println("HERE ", configFile)
b, err := ioutil.ReadFile(configFile)
if err != nil {
fmt.Println(configFile, " not found")
//return
} else {
json, _ := simplejson.NewJson(b)
config.Mode = json.Get("mode").MustString()
config.Ip = json.Get("ip").MustString()
config.Port = json.Get("port").MustInt()
config.Monitors = json.Get("monitors").MustMap()
}
} else {
config.Mode = os.Getenv("FILESYNC_MODE")
config.Ip = os.Getenv("FILESYNC_IP")
config.Port, _ = strconv.Atoi(os.Getenv("FILESYNC_PORT"))
config.Monitors = make(map[string]interface{})
config.Monitors["default"] = os.Getenv("FILESYNC_PATH")
}
if(config.Mode == ""){
config.Mode = "server"
}
if(config.Ip == ""){
config.Ip = "0.0.0.0"
}
if(config.Port <= 0){
config.Port = 6776
}
if (len(config.Monitors) == 1) && (config.Monitors["default"] == ""){
log.Println("No paths to monitor - defaulting to /share")
config.Monitors = make(map[string]interface{})
config.Monitors["default"] = "/share"
}
}
func GetConfig() ConfigVars{
return config
}
func Args() []string {
ret := []string{}
if len(os.Args) >= 1 {
for i := 1; i < len(os.Args); i++ {
ret = append(ret, os.Args[i])
}
}
return ret
}