Add environment variable configuration

This commit is contained in:
Elara 2023-06-09 11:19:07 -07:00
parent a9a3a2094e
commit fed9e6527b
4 changed files with 30 additions and 18 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"`
}

View File

@ -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,6 +73,12 @@ func main() {
}
cfg := &config.Config{}
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()
@ -79,6 +87,7 @@ func main() {
if err != nil {
log.Fatal("Error decoding config file").Err(err).Send()
}
}
if _, err := os.Stat(cfg.Git.RepoDir); os.IsNotExist(err) {
err = os.MkdirAll(cfg.Git.RepoDir, 0o755)