From e019f3811bc1f5ad22d34419f3601358d997910b Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Sun, 16 Jun 2024 07:39:27 +0200 Subject: [PATCH] Helpers --- cli/packages/util/helper.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 81fbcaeff..36d8abda1 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -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 +}