This repository was archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
87 lines (74 loc) · 2.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
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/spf13/viper"
)
type Config struct {
Client HTTPClient
Port string
GocdUrl string
GocdApiKey string
JiraUrl string
JiraUser string
JiraApiKey string
ConfluenceSpaceKey string
}
func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
func NewDefaultConfig() *Config {
gocdApiKey, err := getAPISecret("gocdapikey")
if err != nil {
log.Fatalf("failed to read secret %s: %v", "gocdapikey", err)
}
jiraApiKey, err := getAPISecret("jiraapikey")
if err != nil {
log.Fatalf("failed to read secret %s: %v", "jiraapikey", err)
}
// NOTE: we could import a proper cert,
// e.g. for GoCD our own corp.ca.pem cert
// but that would add extra 20 lines I don't want to maintain now
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return &Config{
Client: &http.Client{Transport: transport},
Port: viper.GetString("port"),
GocdUrl: viper.GetString("gocdUrl"),
GocdApiKey: gocdApiKey, // NOTE: create your own GoCD personal access token
JiraUrl: viper.GetString("jiraUrl"),
JiraUser: viper.GetString("jiraUser"), // NOTE: change to your email address for development
JiraApiKey: jiraApiKey, // NOTE: change to your JIRA password during development
ConfluenceSpaceKey: viper.GetString("confluenceSpaceKey"),
}
}
func getAPISecret(secretName string) (string, error) {
rtn := ""
// first try the k8s/FaaS secrets
secretBytes, err := ioutil.ReadFile("/var/openfaas/secrets/" + secretName)
if err != nil {
secretBytes, err = ioutil.ReadFile("/run/secrets/" + secretName)
}
if err == nil {
rtn = strings.TrimSpace(string(secretBytes))
return rtn, nil
}
val := viper.GetString(secretName)
if val != "" {
return val, nil
}
fmt.Printf("using default for '%s' - use for testing only\n", secretName)
return "notset", nil
}