From 29743cd4117510eb3b512e4481559b8a9d4e13f6 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 4 May 2017 12:06:46 +0200 Subject: [PATCH] Allow defining default config on disk Signed-off-by: Knut Ahlers --- main.go | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 00c1276..9697a09 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "text/template" "time" + yaml "gopkg.in/yaml.v2" + "github.com/Luzifer/rconfig" log "github.com/Sirupsen/logrus" "github.com/hashicorp/vault/api" @@ -28,7 +30,8 @@ const ( actionRevoke = "revoke" actionRevokeSerial = "revoke-serial" - dateFormat = "2006-01-02 15:04:05" + dateFormat = "2006-01-02 15:04:05" + defaultsFile = "~/.config/vault-openvpn.yaml" ) var ( @@ -36,16 +39,24 @@ var ( VaultAddress string `flag:"vault-addr" env:"VAULT_ADDR" default:"https://127.0.0.1:8200" description:"Vault API address"` VaultToken string `flag:"vault-token" env:"VAULT_TOKEN" vardefault:"vault-token" description:"Specify a token to use instead of app-id auth"` - PKIMountPoint string `flag:"pki-mountpoint" default:"/pki" description:"Path the PKI provider is mounted to"` - PKIRole string `flag:"pki-role" default:"openvpn" description:"Role defined in the PKI usable by the token and able to write the specified FQDN"` + PKIMountPoint string `flag:"pki-mountpoint" vardefault:"pki-mountpoint" description:"Path the PKI provider is mounted to"` + PKIRole string `flag:"pki-role" vardefault:"pki-role" description:"Role defined in the PKI usable by the token and able to write the specified FQDN"` - AutoRevoke bool `flag:"auto-revoke" default:"true" description:"Automatically revoke older certificates for this FQDN"` - CertTTL time.Duration `flag:"ttl" default:"8760h" description:"Set the TTL for this certificate"` + AutoRevoke bool `flag:"auto-revoke" vardefault:"auto-revoke" description:"Automatically revoke older certificates for this FQDN"` + CertTTL time.Duration `flag:"ttl" vardefault:"ttl" description:"Set the TTL for this certificate"` - LogLevel string `flag:"log-level" default:"info" description:"Log level to use (debug, info, warning, error)"` + LogLevel string `flag:"log-level" vardefault:"log-level" description:"Log level to use (debug, info, warning, error)"` VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{} + defaultConfig = map[string]string{ + "pki-mountpoint": "/pki", + "pki-role": "openvpn", + "auto-revoke": "true", + "ttl": "8760h", + "log-level": "info", + } + version = "dev" client *api.Client @@ -87,10 +98,29 @@ func vaultTokenFromDisk() string { return string(data) } +func defualtsFromDisk() map[string]string { + res := defaultConfig + + df, err := homedir.Expand(defaultsFile) + if err != nil { + return res + } + + yamlSource, err := ioutil.ReadFile(df) + if err != nil { + return res + } + + if err := yaml.Unmarshal(yamlSource, &res); err != nil { + log.Errorf("Unable to parse defaults file %q: %s", defaultsFile, err) + } + return res +} + func init() { - rconfig.SetVariableDefaults(map[string]string{ - "vault-token": vaultTokenFromDisk(), - }) + defaults := defualtsFromDisk() + defaults["vault-token"] = vaultTokenFromDisk() + rconfig.SetVariableDefaults(defaults) if err := rconfig.Parse(&cfg); err != nil { log.Fatalf("Unable to parse commandline options: %s", err)