This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.go
142 lines (116 loc) · 3.19 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
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
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"fmt"
"os"
"path/filepath"
"time"
)
var (
defaultGrimQueueName = "grim-queue"
defaultConfigRoot = "/etc/grim"
defaultResultRoot = "/var/log/grim"
defaultWorkspaceRoot = "/var/tmp/grim"
defaultTimeout = 5 * time.Minute
configFileName = "config.json"
buildScriptName = "build.sh"
repoBuildScriptName = "grim_build.sh"
repoHiddenBuildScriptName = ".grim_build.sh"
defaultTemplateForStart = templateForStart()
defaultTemplateForError = templateForFailureandError("Error during")
defaultTemplateForSuccess = templateForSuccess()
defaultColorForSuccess = colorForSuccess()
defaultColorForFailure = colorForFailure()
defaultColorForError = colorForError()
defaultColorForPending = colorForPending()
defaultTemplateForFailure = templateForFailureandError("Failure during")
defaultHipChatVersion = 1
)
type configMap map[string]interface{}
func getEffectiveConfigRoot(configRootPtr *string) string {
if configRootPtr == nil || *configRootPtr == "" {
return defaultConfigRoot
}
return *configRootPtr
}
type repo struct {
owner, name string
}
func getAllConfiguredRepos(configRoot string) []repo {
var repos []repo
repoPattern := filepath.Join(configRoot, "*/*")
matches, err := filepath.Glob(repoPattern)
if err != nil {
return repos
}
for _, match := range matches {
fi, fiErr := os.Stat(match)
if fiErr != nil || !fi.Mode().IsDir() {
continue
}
rel, relErr := filepath.Rel(configRoot, match)
if relErr != nil {
continue
}
owner, name := filepath.Split(rel)
if owner != "" && name != "" {
repos = append(repos, repo{filepath.Clean(owner), filepath.Clean(name)})
}
}
return repos
}
func templateForStart() *string {
s := fmt.Sprintf("Starting build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}}")
return &s
}
func templateForSuccess() *string {
s := fmt.Sprintf("Success after build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}} ({{.Workspace}})")
return &s
}
func templateForFailureandError(preamble string) *string {
s := fmt.Sprintf("%s build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}} ({{.LogDir}})", preamble)
return &s
}
func colorForSuccess() *string {
c := string(ColorGreen)
return &c
}
func colorForFailure() *string {
c := string(ColorRed)
return &c
}
func colorForError() *string {
c := string(ColorGray)
return &c
}
func colorForPending() *string {
c := string(ColorYellow)
return &c
}
func readStringWithDefaults(m map[string]interface{}, key string, strs ...string) string {
val, _ := m[key]
str, _ := val.(string)
if str == "" {
for _, str = range strs {
if str != "" {
break
}
}
}
return str
}
func readIntWithDefaults(m map[string]interface{}, key string, ints ...int) int {
val, _ := m[key]
f, _ := val.(float64)
i := int(f)
if i == 0 {
for _, i = range ints {
if i != 0 {
break
}
}
}
return i
}