-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathenvfile.go
More file actions
28 lines (24 loc) · 863 Bytes
/
envfile.go
File metadata and controls
28 lines (24 loc) · 863 Bytes
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
// getEnvOrFile returns the value of the environment variable if set, otherwise if a corresponding _FILE variable is set, reads the value from the file at that path.
// If both are set, the environment variable takes precedence.
//
//nolint:wsl_v5 // extensive whitespace linting would require significant refactoring
package githosts
import (
"os"
"strings"
)
// getEnvOrFile returns the value of the environment variable if set, otherwise if a corresponding _FILE variable is set, reads the value from the file at that path.
func getEnvOrFile(envVar string) string {
val := strings.TrimSpace(os.Getenv(envVar))
if val != "" {
return val
}
fileEnv := envVar + "_FILE"
filePath := strings.TrimSpace(os.Getenv(fileEnv))
if filePath != "" {
if b, err := os.ReadFile(filePath); err == nil {
return strings.TrimSpace(string(b))
}
}
return ""
}