Skip to content

Commit

Permalink
feat: add flag configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
rruizt committed Apr 21, 2024
1 parent f63bc27 commit 02571fa
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"flag"
"strings"
)

Expand All @@ -27,15 +28,34 @@ func InitConfig() {
}

func getOmnivoreToken() (string, error) {
secretEnv := os.Getenv("OMNIVORE_AUTH_TOKEN")
var omnivoreTokenFlag string
var secretFilePathFlag string

flag.StringVar(&omnivoreTokenFlag, "t", "" , "the Omnivore API token")
flag.StringVar(&secretFilePathFlag, "tf", "" , "the path to the file with the Omnivore API Token")

flag.Parse()

omnivoreToken := os.Getenv("OMNIVORE_AUTH_TOKEN")
if omnivoreToken == "" {
omnivoreToken = omnivoreTokenFlag
}

secretFilePath := os.Getenv("OMNIVORE_AUTH_TOKEN_FILEPATH")
if secretFilePath == "" {
secretFilePath = secretFilePathFlag
}

if secretFilePath != "" {
if omnivoreToken != "" {
log.Println("Reading secret from env var")
return omnivoreToken, nil
} else if secretFilePath != "" {
log.Println("Reading secret from file")

dat, err := os.ReadFile(secretFilePath)
if err != nil {
log.Println(err)
return "", errors.New("unable to read Omnivore secret file")
return "", errors.New("unable to get Omnivore API Key: filepath was provided but couldn't read it")
}

token := string(dat)
Expand All @@ -45,10 +65,7 @@ func getOmnivoreToken() (string, error) {
token = strings.Replace(token, "\n", "", -1)

return token, nil
} else if secretEnv != "" {
log.Println("Reading secret from env var")
return secretEnv, nil
}
return "", errors.New("omnivore authentication is needed for this service to work, please set environment variable or secret file")

return "", errors.New("omnivore API key is needed for this application to work, please set environment variable or flag")
}

0 comments on commit 02571fa

Please sign in to comment.