This commit is contained in:
Daniel Hougaard
2024-06-16 07:39:27 +02:00
parent c119f506fd
commit e019f3811b

View File

@@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
@@ -246,3 +247,30 @@ func AppendAPIEndpoint(address string) string {
}
return address + "/api"
}
func ReadFileAsString(filePath string) (string, error) {
fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(fileBytes), nil
}
func GetEnvVarOrFileContent(envName string, filePath string) (string, error) {
// First check if the environment variable is set
if envVarValue := os.Getenv(envName); envVarValue != "" {
return envVarValue, nil
}
// If it's not set, try to read the file
fileContent, err := ReadFileAsString(filePath)
if err != nil {
return "", fmt.Errorf("unable to read file content from file path '%s' [err=%v]", filePath, err)
}
return fileContent, nil
}