-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
153 lines (120 loc) · 3.28 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/json"
"fmt"
. "github.com/ahmetalpbalkan/go-linq"
"github.com/bsideup/configo/exec"
"github.com/bsideup/configo/flatmap"
"github.com/bsideup/configo/sources"
"github.com/op/go-logging"
"os"
"strconv"
"strings"
)
const envVariablePrefix = "CONFIGO_SOURCE_"
type env struct {
key string
value string
}
type sourceWithPriority struct {
priority int
value string
}
var log = logging.MustGetLogger("configo")
var loggingBackend = logging.NewLogBackend(os.Stdout, "", 0)
func main() {
pattern := os.Getenv("CONFIGO_LOG_PATTERN")
if len(pattern) == 0 {
pattern = `%{color}%{time:15:04:05.999} [%{level:.1s}] %{message}%{color:reset}`
}
format := logging.MustStringFormatter(pattern)
logging.SetBackend(logging.NewBackendFormatter(loggingBackend, format))
levelString := os.Getenv("CONFIGO_LOG_LEVEL")
var level logging.Level
if len(levelString) > 0 {
var err error
level, err = logging.LogLevel(levelString)
if err != nil {
log.Warningf("%s", err)
}
} else {
level = logging.WARNING
}
logging.SetLevel(level, "configo")
defer func() {
if r := recover(); r != nil {
log.Errorf("%s", r)
os.Exit(1)
}
}()
if len(os.Args) < 2 {
panic("the required argument `command` was not provided")
}
environ := os.Environ()
log.Debugf("Environment variables at start:\n\t%s", strings.Join(environ, "\n\t"))
if err := resolveAll(environ); err != nil {
panic(err)
}
if err := processTemplatedEnvs(environ); err != nil {
panic(err)
}
exec.Execute(strings.Join(os.Args[1:], " "))
}
func resolveAll(environ []string) error {
rawTSources, err := fromEnviron(environ).
Where(func(kv T) (bool, error) { return strings.HasPrefix(kv.(env).key, envVariablePrefix), nil }).
Select(func(kv T) (T, error) {
priority, err := strconv.Atoi(strings.TrimLeft(kv.(env).key, envVariablePrefix))
if err != nil {
return nil, err
}
return sourceWithPriority{priority, kv.(env).value}, nil
}).
OrderBy(func(a T, b T) bool { return a.(sourceWithPriority).priority <= b.(sourceWithPriority).priority }).
Select(func(it T) (T, error) {
sourceBytes := []byte(it.(sourceWithPriority).value)
rawSource := make(map[string]interface{})
err := json.Unmarshal(sourceBytes, &rawSource)
return rawSource, err
}).
Results()
if err != nil {
return err
}
rawSources := make([]map[string]interface{}, len(rawTSources))
for k, v := range rawTSources {
rawSources[k] = v.(map[string]interface{})
}
if len(rawSources) == 0 {
log.Warning("No sources provided")
return nil
}
if os.Getenv("CONFIGO_UPPERCASE_KEYS") == "0" {
flatmap.UppercaseKeys = false
}
loader := sources.CompositeSource{
Sources: rawSources,
}
resultEnv, err := loader.Get()
if err != nil {
return err
}
if len(resultEnv) == 0 {
log.Info("No new env variables were added.")
return nil
}
for key, value := range resultEnv {
log.Infof("Setting %s to %v", key, value)
os.Setenv(key, fmt.Sprintf("%v", value))
}
if log.IsEnabledFor(logging.DEBUG) {
log.Debugf("Environment variables after resolve:\n\t%s", strings.Join(os.Environ(), "\n\t"))
}
return nil
}
func fromEnviron(environ []string) Query {
return From(environ).Select(func(kv T) (T, error) {
pair := strings.SplitN(kv.(string), "=", 2)
return env{pair[0], pair[1]}, nil
})
}