This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
112 lines (88 loc) · 2.67 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
// Copyright 2019-2023 Charles Korn.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// and the Commons Clause License Condition v1.0 (the "Condition");
// you may not use this file except in compliance with both the License and Condition.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// You may obtain a copy of the Condition at
//
// https://commonsclause.com/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License and the Condition is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See both the License and the Condition for the specific language governing permissions and
// limitations under the License and the Condition.
package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
)
type serviceConfig struct {
ServiceName string
ServiceVersion string
Port string
ProjectID string
HoneycombAPIKey string
}
func getConfig() (*serviceConfig, error) {
port, err := getPort()
if err != nil {
return nil, fmt.Errorf("could not get port for service to listen to: %w", err)
}
projectID, err := getProjectID()
if err != nil {
return nil, fmt.Errorf("could not get project ID: %w", err)
}
honeycombAPIKey, err := getHoneycombAPIKey()
if err != nil {
return nil, fmt.Errorf("could not get Honeycomb API key: %w", err)
}
return &serviceConfig{
ServiceName: getServiceName(),
ServiceVersion: getServiceVersion(),
Port: port,
ProjectID: projectID,
HoneycombAPIKey: honeycombAPIKey,
}, nil
}
func getServiceName() string {
return getEnvOrDefault("K_SERVICE", "abacus")
}
func getServiceVersion() string {
return getEnvOrDefault("K_REVISION", "local")
}
func getEnvOrDefault(name string, fallback string) string {
if value, ok := os.LookupEnv(name); ok {
return value
}
return fallback
}
func getPort() (string, error) {
return getEnv("PORT")
}
func getProjectID() (string, error) {
return getEnv("GOOGLE_PROJECT")
}
func getHoneycombAPIKey() (string, error) {
return getEnv("HONEYCOMB_API_KEY")
}
func getCredentialsFilePath() string {
variableName := "GOOGLE_APPLICATION_CREDENTIALS"
value := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if value == "" {
logrus.WithField("variable", variableName).Info("Credentials file environment variable is not set, will fallback to default credential sources for GCP connections.")
}
return value
}
func getEnv(name string) (string, error) {
value := os.Getenv(name)
if value == "" {
return "", fmt.Errorf("environment variable '%v' is not set", name)
}
return value, nil
}