From fed9e6527be3349d54ed644f16640bccf4c423c3 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Fri, 9 Jun 2023 11:19:07 -0700 Subject: [PATCH] Add environment variable configuration --- go.mod | 1 + go.sum | 2 ++ internal/config/config.go | 22 +++++++++++----------- main.go | 23 ++++++++++++++++------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 59ea948..ae52875 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module go.elara.ws/lure-updater go 1.20 require ( + github.com/caarlos0/env/v8 v8.0.0 github.com/go-git/go-git/v5 v5.7.0 github.com/pelletier/go-toml/v2 v2.0.8 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index b774976..0246f36 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/caarlos0/env/v8 v8.0.0 h1:POhxHhSpuxrLMIdvTGARuZqR4Jjm8AYmoi/JKlcScs0= +github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= diff --git a/internal/config/config.go b/internal/config/config.go index a029eda..61b2b37 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,27 +19,27 @@ package config type Config struct { - Git Git `toml:"git"` - Webhook Webhook `toml:"webhook"` + Git Git `toml:"git" envPrefix:"GIT_"` + Webhook Webhook `toml:"webhook" envPrefix:"WEBHOOK_"` } type Git struct { - RepoDir string `toml:"repoDir"` - RepoURL string `toml:"repoURL"` - Commit Commit `toml:"commit"` - Credentials Credentials `toml:"credentials"` + RepoDir string `toml:"repoDir" env:"REPO_DIR"` + RepoURL string `toml:"repoURL" env:"REPO_URL"` + Commit Commit `toml:"commit" envPrefix:"COMMIT_"` + Credentials Credentials `toml:"credentials" envPrefix:"CREDENTIALS_"` } type Credentials struct { - Username string - Password string + Username string `toml:"username" env:"USERNAME"` + Password string `toml:"password" env:"PASSWORD"` } type Commit struct { - Name string `toml:"name"` - Email string `toml:"email"` + Name string `toml:"name" env:"NAME"` + Email string `toml:"email" env:"EMAIL"` } type Webhook struct { - PasswordHash string `toml:"pwd_hash"` + PasswordHash string `toml:"pwd_hash" env:"PASSWORD_HASH"` } diff --git a/main.go b/main.go index 081c7c7..789a68b 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ import ( "path/filepath" "strings" + "github.com/caarlos0/env/v8" "github.com/go-git/go-git/v5" "github.com/pelletier/go-toml/v2" "github.com/spf13/pflag" @@ -49,6 +50,7 @@ func main() { pluginDir := pflag.StringP("plugin-dir", "p", "/etc/lure-updater/plugins", "Path to plugin directory") serverAddr := pflag.StringP("address", "a", ":8080", "Webhook server address") genHash := pflag.BoolP("gen-hash", "g", false, "Generate a password hash for webhooks") + useEnv := pflag.BoolP("use-env", "E", false, "Use environment variables for configuration") pflag.Parse() if *genHash { @@ -71,13 +73,20 @@ func main() { } cfg := &config.Config{} - fl, err := os.Open(*configPath) - if err != nil { - log.Fatal("Error opening config file").Err(err).Send() - } - err = toml.NewDecoder(fl).Decode(cfg) - if err != nil { - log.Fatal("Error decoding config file").Err(err).Send() + if *useEnv { + err = env.Parse(cfg) + if err != nil { + log.Fatal("Error parsing environment variables").Err(err).Send() + } + } else { + fl, err := os.Open(*configPath) + if err != nil { + log.Fatal("Error opening config file").Err(err).Send() + } + err = toml.NewDecoder(fl).Decode(cfg) + if err != nil { + log.Fatal("Error decoding config file").Err(err).Send() + } } if _, err := os.Stat(cfg.Git.RepoDir); os.IsNotExist(err) {