forked from phayes/deadci
/
config.go
112 lines (99 loc) · 2.91 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
package main
import (
"flag"
"fmt"
"github.com/dlintw/goconf"
"log"
"os"
"strings"
)
var Config struct {
DataDir string
TempDir string
Command []string
Port int
Host string
Github struct {
Enabled bool
Token string
Secret string
}
HttpsClone bool
}
func init() {
flag.StringVar(&Config.DataDir, "data-dir", "", "Data directory where config is stored. Must be writable.")
}
func InitConfig() {
flag.Parse()
// Parse data directory
if Config.DataDir == "" {
fmt.Println(" --data-dir option not specified. Please create a writable data directory and invoke deadci command with --data-dir=/path/to/data/dir")
flag.CommandLine.PrintDefaults()
os.Exit(2)
}
Config.DataDir = strings.TrimRight(Config.DataDir, "/ ")
// Read the config file
c, err := goconf.ReadConfigFile(Config.DataDir + "/deadci.ini")
if err != nil {
log.Fatal(err.Error() + ". Please ensure that your deadci.ini file is readable and in place at " + Config.DataDir + "/deadci.ini")
}
// Parse command
cmd, err := c.GetString("", "command")
if err != nil {
log.Fatal(err)
}
cmd = strings.Trim(cmd, " ")
if cmd == "" {
log.Fatal("Missing command in deadci.ini. Please specify a command to run to build / test your repositories.")
}
Config.Command = strings.Split(cmd, " ")
if len(Config.Command) == 0 {
log.Fatal("Missing command in deadci.ini. Please specify a command to run to build / test your repositories.")
}
// Parse Port
Config.Port, err = c.GetInt("", "port")
if err != nil {
log.Fatal(err)
}
// Parse Host
Config.Host, err = c.GetString("", "host")
if (err != nil && err.(goconf.GetError).Reason == goconf.OptionNotFound) || Config.Host == "" {
Config.Host, err = os.Hostname()
if err != nil {
log.Fatal("Unable to determine hostname. Please specify a hostname in deadci.ini")
}
} else if err != nil {
log.Fatal(err)
}
// Parse Temp Dir
Config.TempDir, err = c.GetString("", "tempdir")
if (err != nil && err.(goconf.GetError).Reason == goconf.OptionNotFound) || Config.TempDir == "" {
Config.TempDir = os.TempDir()
} else if err != nil {
log.Fatal(err)
}
// Normalize tempdir string
Config.TempDir = strings.TrimRight(Config.TempDir, "/")
// Parse Github settings
if c.HasSection("github") {
Config.Github.Enabled, err = c.GetBool("github", "enabled")
if err != nil && err.(goconf.GetError).Reason != goconf.OptionNotFound {
log.Fatal(err)
}
if Config.Github.Enabled {
Config.Github.Token, err = c.GetString("github", "token")
if err != nil && err.(goconf.GetError).Reason != goconf.OptionNotFound {
log.Fatal(err)
}
Config.Github.Secret, err = c.GetString("github", "secret")
if err != nil && err.(goconf.GetError).Reason != goconf.OptionNotFound {
log.Fatal(err)
}
}
}
// Parse clone style (git or https)
Config.HttpsClone, err = c.GetBool("", "httpsclone")
if err != nil && err.(goconf.GetError).Reason != goconf.OptionNotFound {
log.Fatal(err)
}
}