Skip to content

Commit

Permalink
command line flag overrides for addresses, secrets, config dump
Browse files Browse the repository at this point in the history
  • Loading branch information
doublerebel committed Mar 2, 2016
1 parent 15d6bb5 commit 04fe165
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
50 changes: 38 additions & 12 deletions config/load.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"encoding/json"
"fmt"
"log"
"path"
Expand All @@ -10,10 +9,46 @@ import (
"time"

"github.com/spf13/viper"
flag "github.com/spf13/pflag"
)

func Load(filename string) (*Config, error) {
v := viper.New()
func Load(args []string) (cfg *Config, showVersion bool) {
showVersion, filename, configdebug, v := FromFlags(args)
if showVersion {
return nil, true
}

cfg, err := FromFile(v, filename)
if err != nil {
log.Fatal("[FATAL] ", err)
}
if configdebug {
v.Debug()
}
return cfg, false
}

func FromFlags(args []string) (showVersion bool, cfgPath string, configdebug bool, v *viper.Viper) {
v = viper.New()
fs := flag.NewFlagSet("default", flag.ExitOnError)

fs.BoolVar(&showVersion, "version", false, "show version")
fs.StringVar(&cfgPath, "cfg", "", "path to config file")
fs.BoolVar(&configdebug, "configdebug", false, "print config to console for debugging")

fs.String("registry.consul.addr", "", "Consul address")
fs.String("registry.consul.token", "", "Consul token")
fs.String("proxy.addr", "", "proxy address")
fs.String("proxy.localip", "", "proxy local IP")
fs.String("proxy.header.clientip", "", "proxy header client IP")
fs.String("ui.addr", "", "UI address")

v.BindPFlags(fs)
fs.Parse(args)
return showVersion, cfgPath, configdebug, v
}

func FromFile(v *viper.Viper, filename string) (*Config, error) {
base := path.Base(filename)
ext := path.Ext(filename)
name := strings.TrimSuffix(base, ext)
Expand Down Expand Up @@ -84,18 +119,9 @@ func FromViper(v *viper.Viper) (cfg *Config, err error) {
cfg.Runtime.GOMAXPROCS = runtime.NumCPU()
}

dump(cfg)
return cfg, nil
}

func dump(cfg *Config) {
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
log.Fatal("[FATAL] Cannot dump runtime config. ", err)
}
log.Println("[INFO] Runtime config\n" + string(data))
}

func parseMetrics(target, prefix, graphiteAddr string, interval time.Duration) []Metrics {
m := Metrics{Target: target, Prefix: prefix, Interval: interval}
if target == "graphite" {
Expand Down
12 changes: 1 addition & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -32,23 +31,14 @@ import (
var version = "1.1.1"

func main() {
var filename string
var v bool
flag.StringVar(&filename, "cfg", "", "path to config file")
flag.BoolVar(&v, "v", false, "show version")
flag.Parse()
cfg, v := config.Load(os.Args[1:])

if v {
fmt.Println(version)
return
}
log.Printf("[INFO] Version %s starting", version)

cfg, err := config.Load(filename)
if err != nil {
log.Fatal("[FATAL] ", err)
}

initRuntime(cfg)
initMetrics(cfg)
initBackend(cfg)
Expand Down

0 comments on commit 04fe165

Please sign in to comment.