add support for config settings as environment variables

This commit is contained in:
Travis Nielsen
2016-09-14 14:08:32 -07:00
parent 316bd1283d
commit 38992afec5
2 changed files with 27 additions and 3 deletions
Generated
+4 -3
View File
@@ -1,5 +1,5 @@
hash: cb31830bb94bc7f841afb8023472f541aa1a4bf2297da8c05007c26900b8f7ce
updated: 2016-09-13T12:37:36.929062201-07:00
hash: b6857a2bb14d137d9d8a067bf9f3fa4519520ddfddc2105cd4dd99914d9ff1f9
updated: 2016-09-13T14:08:54.366031279-07:00
imports:
- name: bitbucket.org/ww/goautoneg
version: 75cd24fc2f2c2a2088577d12123ddee5f54e0675
@@ -38,6 +38,7 @@ imports:
- pkg/cors
- pkg/crc
- pkg/fileutil
- pkg/flags
- pkg/httputil
- pkg/idutil
- pkg/ioutil
@@ -119,7 +120,7 @@ imports:
- name: github.com/prometheus/procfs
version: 454a56f35412459b5e684fd5ec0f9211b94f002a
- name: github.com/quantum/clusterd
version: a19ae6e0d43166b5e8cec10424cb2dcc9c13c7f3
version: 14dec27c9b5e2463555ac74674daf53c625fd619
subpackages:
- pkg/inventory
- pkg/orchestrator
+23
View File
@@ -4,9 +4,11 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/quantum/castle/pkg/castled"
"github.com/quantum/castle/pkg/util"
@@ -28,6 +30,10 @@ var rootCmd = &cobra.Command{
Long: `https://github.com/quantum/castle`,
}
// Initialize the configuration parameters. The precedence from lowest to highest is:
// 1) default value (at compilation)
// 2) environment variables (upper case, replace - with _, and castle prefix. For example, discovery-url is CASTLE_DISCOVERY_URL)
// 3) command line parameter
func init() {
rootCmd.Flags().StringVar(&discoveryURL, "discovery-url", "", "etcd discovery URL. Example: http://discovery.castle.com/26bd83c92e7145e6b103f623263f61df")
rootCmd.Flags().StringVar(&etcdMembers, "etcd-members", "", "etcd members to connect to. Overrides the discovery URL. Example: http://10.23.45.56:2379")
@@ -36,6 +42,9 @@ func init() {
rootCmd.Flags().BoolVar(&forceFormat, "force-format", false,
"true to force the format of any specified devices, even if they already have a filesystem. BE CAREFUL!")
// load the environment variables
setFlagsFromEnv(rootCmd.Flags())
rootCmd.MarkFlagRequired("private-ipv4")
rootCmd.RunE = joinCluster
@@ -52,6 +61,7 @@ func addCommands() {
}
func joinCluster(cmd *cobra.Command, args []string) error {
if err := util.VerifyRequiredFlags(cmd, []string{"private-ipv4"}); err != nil {
return err
}
@@ -80,3 +90,16 @@ func joinCluster(cmd *cobra.Command, args []string) error {
return nil
}
func setFlagsFromEnv(flags *pflag.FlagSet) error {
flags.VisitAll(func(f *pflag.Flag) {
envVar := "CASTLE_" + strings.Replace(strings.ToUpper(f.Name), "-", "_", -1)
value := os.Getenv(envVar)
if value != "" {
// Set the environment variable. Will override default values, but be overridden by command line parameters.
flags.Set(f.Name, value)
}
})
return nil
}