-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
69 lines (61 loc) · 1.46 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
package mantil
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"os/user"
"strings"
)
// Configuration environment variables:
const (
EnvConfig = "MANTIL_GO_CONFIG"
EnvKVTableName = "MANTIL_KV_TABLE"
)
type cfg struct {
ResourceTags map[string]string
WsForwarderName string
NamingTemplate string
}
func (c *cfg) load() error {
encoded, err := ensureEnv(EnvConfig, "mantil.go config not found")
if err != nil {
return err
}
buf, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return err
}
if err := json.Unmarshal(buf, &c); err != nil {
return err
}
return nil
}
func (c cfg) kvTableName() (string, error) {
val, _ := ensureEnv(EnvKVTableName, "kv table name not found")
if val != "" {
return val, nil
}
if c.isUnitTestEnv() {
return fmt.Sprintf("mantil-go-%s-unit", c.username()), nil
}
return fmt.Sprintf(c.NamingTemplate, "kv"), nil
}
// hackery trick to know if I'm running in `go test`
// ref: https://stackoverflow.com/questions/14249217/how-do-i-know-im-running-within-go-test
func (c cfg) isUnitTestEnv() bool {
return strings.HasSuffix(os.Args[0], ".test")
}
func ensureEnv(envVarName, errPrefix string) (string, error) {
if val, ok := os.LookupEnv(envVarName); ok {
return val, nil
}
return "", fmt.Errorf("%s, please set environment variable %s", errPrefix, envVarName)
}
func (c cfg) username() string {
user, err := user.Current()
if err != nil {
return "anonymous"
}
return user.Username
}