forked from gravityblast/fresh
/
settings.go
143 lines (122 loc) · 2.89 KB
/
settings.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
135
136
137
138
139
140
141
142
143
package runner
import (
"fmt"
"github.com/pilu/config"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
const (
envSettingsPrefix = "RUNNER_"
mainSettingsSection = "Settings"
)
var settings = map[string]string{
"config_path": "./runner.conf",
"root": ".",
"tmp_path": "./tmp",
"build_name": "runner-build",
"build_log": "runner-build-errors.log",
"valid_ext": ".go, .tpl, .tmpl, .html",
"build_delay": "600",
"colors": "1",
"log_color_main": "cyan",
"log_color_build": "yellow",
"log_color_runner": "green",
"log_color_watcher": "magenta",
"log_color_app": "",
}
var colors = map[string]string{
"reset": "0",
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"white": "37",
"bold_black": "30;1",
"bold_red": "31;1",
"bold_green": "32;1",
"bold_yellow": "33;1",
"bold_blue": "34;1",
"bold_magenta": "35;1",
"bold_cyan": "36;1",
"bold_white": "37;1",
"bright_black": "30;2",
"bright_red": "31;2",
"bright_green": "32;2",
"bright_yellow": "33;2",
"bright_blue": "34;2",
"bright_magenta": "35;2",
"bright_cyan": "36;2",
"bright_white": "37;2",
}
func logColor(logName string) string {
settingsKey := fmt.Sprintf("log_color_%s", logName)
colorName := settings[settingsKey]
return colors[colorName]
}
func loadEnvSettings() {
for key, _ := range settings {
envKey := fmt.Sprintf("%s%s", envSettingsPrefix, strings.ToUpper(key))
if value := os.Getenv(envKey); value != "" {
settings[key] = value
}
}
}
func loadRunnerConfigSettings() {
if _, err := os.Stat(configPath()); err != nil {
return
}
logger.Printf("Loading settings from %s", configPath())
sections, err := config.ParseFile(configPath(), mainSettingsSection)
if err != nil {
return
}
for key, value := range sections[mainSettingsSection] {
settings[key] = value
}
}
func initSettings() {
loadEnvSettings()
loadRunnerConfigSettings()
}
func getenv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func root() string {
return settings["root"]
}
func tmpPath() string {
return settings["tmp_path"]
}
func buildName() string {
return settings["build_name"]
}
func buildPath() string {
p := filepath.Join(tmpPath(), buildName())
if runtime.GOOS == "windows" && filepath.Ext(p) != ".exe" {
p += ".exe"
}
return p
}
func buildErrorsFileName() string {
return settings["build_log"]
}
func buildErrorsFilePath() string {
return filepath.Join(tmpPath(), buildErrorsFileName())
}
func configPath() string {
return settings["config_path"]
}
func buildDelay() time.Duration {
value, _ := strconv.Atoi(settings["build_delay"])
return time.Duration(value)
}