-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.go
More file actions
47 lines (37 loc) · 1.02 KB
/
environment.go
File metadata and controls
47 lines (37 loc) · 1.02 KB
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
package main
import (
"fmt"
"os"
"strings"
)
type duplicateValueError struct {
description string
lastkey string
currentkey string
}
func (d *duplicateValueError) Error() string {
return fmt.Sprintf("value for %q already provided by %q but also found in %q", d.description, "$"+d.lastkey, "$"+d.currentkey)
}
type envValueRequired struct {
description string
keyNames []string
}
func (er *envValueRequired) Error() string {
return fmt.Sprintf("must provide a value for %s using any of the following: %s", er.description, strings.Join(er.keyNames, ", "))
}
func getAnyEnvironment(description string, envName ...string) (string, error) {
var value, lastKeyFound string
for _, v := range envName {
if s := os.Getenv(v); s != "" {
if value != "" {
return "", &duplicateValueError{description: description, lastkey: lastKeyFound, currentkey: v}
}
value = s
lastKeyFound = v
}
}
if value == "" {
return "", &envValueRequired{description: description, keyNames: envName}
}
return value, nil
}