Skip to content

Commit

Permalink
Use cobra for flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Aug 22, 2018
1 parent c5b3928 commit bb9c9cc
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 135 deletions.
20 changes: 10 additions & 10 deletions builder/files/dkron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# backend: etcd
# backend_machine: 127.0.0.1:2379
# server: false
# log_level: debug
# log-level: debug
# tags:
# role: web
# datacenter: east
Expand All @@ -12,12 +12,12 @@
# - 10.0.0.1
# - 10.0.0.2
# - 10.0.0.3
# webhook_url: https://hooks.slack.com/services/XXXXXX/XXXXXXX/XXXXXXXXXXXXXXXXXXXX
# webhook_payload: "payload={\"text\": \"{{.Report}}\", \"channel\": \"#foo\"}"
# webhook_headers: Content-Type:application/x-www-form-urlencoded
# mail_host: email-smtp.eu-west-1.amazonaws.com
# mail_port: 25
# mail_username": mailuser
# mail_password": mailpassword
# mail_from": cron@example.com
# mail_subject_prefix: [Dkron]
# webhook-url: https://hooks.slack.com/services/XXXXXX/XXXXXXX/XXXXXXXXXXXXXXXXXXXX
# webhook-payload: "payload={\"text\": \"{{.Report}}\", \"channel\": \"#foo\"}"
# webhook-headers: Content-Type:application/x-www-form-urlencoded
# mail-host: email-smtp.eu-west-1.amazonaws.com
# mail-port: 25
# mail-username": mailuser
# mail-password": mailpassword
# mail-from": cron@example.com
# mail-subject-prefix: [Dkron]
188 changes: 187 additions & 1 deletion cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ It also runs a web UI.`,
// The returned value is the exit code.
// protoc -I proto/ proto/executor.proto --go_out=plugins=grpc:dkron/
RunE: func(cmd *cobra.Command, args []string) error {
legacyConfig()

// Make sure we clean up any managed plugins at the end of this
p := &Plugins{}
if err := p.DiscoverPlugins(); err != nil {
Expand Down Expand Up @@ -61,7 +63,7 @@ It also runs a web UI.`,
func init() {
dkronCmd.AddCommand(agentCmd)

agentCmd.Flags().AddGoFlagSet(dkron.ConfigFlagSet())
agentCmd.Flags().AddFlagSet(dkron.ConfigFlagSet())
viper.BindPFlags(agentCmd.Flags())
}

Expand Down Expand Up @@ -134,3 +136,187 @@ func handleReload() {
}
//Config reloading will also reload Notification settings
}

// Suport legacy config files for some time
func legacyConfig() {
s := viper.GetString("node_name")
if s != "" && viper.GetString("node-name") == "" {
log.WithField("param", "node_name").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.NodeName = s
}

s = viper.GetString("bind_addr")
if s != "" && viper.GetString("bind-addr") == "" {
log.WithField("param", "bind_addr").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.BindAddr = s
}

s = viper.GetString("http_addr")
if s != "" && viper.GetString("http-addr") == "" {
log.WithField("param", "http_addr").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.HTTPAddr = s
}

ss := viper.GetStringSlice("backend_machine")
if ss != nil && viper.GetStringSlice("backend_machine") == nil {
log.WithField("param", "backend_machine").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.BackendMachines = ss
}

s = viper.GetString("advertise_addr")
if s != "" && viper.GetString("advertise-addr") == "" {
log.WithField("param", "advertise_addr").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.AdvertiseAddr = s
}

s = viper.GetString("snapshot_path")
if s != "" && viper.GetString("snapshot-path") == "" {
log.WithField("param", "snapshot_path").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.SnapshotPath = s
}

d := viper.GetDuration("reconnect_interval")
if d != 0 && viper.GetDuration("reconnect-interval") == 0 {
log.WithField("param", "reconnect_interval").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.ReconnectInterval = d
}

d = viper.GetDuration("reconnect_timeout")
if d != 0 && viper.GetDuration("reconnect-timeout") == 0 {
log.WithField("param", "reconnect_timeout").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.ReconnectTimeout = d
}

d = viper.GetDuration("tombstone_timeout")
if d != 0 && viper.GetDuration("tombstone-timeout") == 0 {
log.WithField("param", "tombstone_timeout").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.TombstoneTimeout = d
}

b := viper.GetBool("disable_name_resolution")
if b != false && viper.GetBool("disable-name-resolution") == false {
log.WithField("param", "disable_name_resolution").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.DisableNameResolution = b
}

s = viper.GetString("keyring_file")
if s != "" && viper.GetString("keyring-file") == "" {
log.WithField("param", "keyring_file").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.KeyringFile = s
}

b = viper.GetBool("rejoin_after_leave")
if b != false && viper.GetBool("rejoin-after-leave") == false {
log.WithField("param", "rejoin_after_leave").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.RejoinAfterLeave = b
}

s = viper.GetString("encrypt_key")
if s != "" && viper.GetString("encrypt-key") == "" {
log.WithField("param", "encrypt_key").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.EncryptKey = s
}

ss = viper.GetStringSlice("start_join")
if ss != nil && viper.GetStringSlice("start-join") == nil {
log.WithField("param", "start_join").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.StartJoin = ss
}

i := viper.GetInt("rpc_port")
if i != 0 && viper.GetInt("rpc-port") == 0 {
log.WithField("param", "rpc_port").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.RPCPort = i
}

i = viper.GetInt("advertise_rpc_port")
if i != 0 && viper.GetInt("advertise-rpc-port") == 0 {
log.WithField("param", "advertise_rpc_port").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.AdvertiseRPCPort = i
}

s = viper.GetString("log_level")
if s != "" && viper.GetString("log-level") == "" {
log.WithField("param", "log_level").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.LogLevel = s
}

s = viper.GetString("mail_host")
if s != "" && viper.GetString("mail-host") == "" {
log.WithField("param", "mail_host").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailHost = s
}

i = viper.GetInt("mail_port")
if i != 0 && viper.GetInt("mail-port") == 0 {
log.WithField("param", "mail_port").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailPort = uint16(i)
}

s = viper.GetString("mail_username")
if s != "" && viper.GetString("mail-username") == "" {
log.WithField("param", "mail_username").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailUsername = s
}

s = viper.GetString("mail_password")
if s != "" && viper.GetString("mail-password") == "" {
log.WithField("param", "mail_password").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailPassword = s
}

s = viper.GetString("mail_from")
if s != "" && viper.GetString("mail-from") == "" {
log.WithField("param", "mail_from").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailFrom = s
}

s = viper.GetString("mail_payload")
if s != "" && viper.GetString("mail-payload") == "" {
log.WithField("param", "mail_payload").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailPayload = s
}

s = viper.GetString("mail_subject_prefix")
if s != "" && viper.GetString("mail-subject-prefix") == "" {
log.WithField("param", "mail_subject_prefix").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.MailSubjectPrefix = s
}

s = viper.GetString("webhook_url")
if s != "" && viper.GetString("webhook-url") == "" {
log.WithField("param", "webhook_url").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.WebhookURL = s
}

s = viper.GetString("webhook_payload")
if s != "" && viper.GetString("webhook-payload") == "" {
log.WithField("param", "webhook_payload").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.WebhookPayload = s
}

ss = viper.GetStringSlice("webhook_headers")
if ss != nil && viper.GetStringSlice("webhook-headers") == nil {
log.WithField("param", "webhook_headers").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.WebhookHeaders = ss
}

s = viper.GetString("dog_statsd_addr")
if s != "" && viper.GetString("dog-statsd-addr") == "" {
log.WithField("param", "dog_statsd_add").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.DogStatsdAddr = s
}

ss = viper.GetStringSlice("dog_statsd_tags")
if ss != nil && viper.GetStringSlice("dog-statsd-tags") == nil {
log.WithField("param", "dog_statsd_tags").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.DogStatsdTags = ss
}

s = viper.GetString("statsd_addr")
if s != "" && viper.GetString("statsd-addr") == "" {
log.WithField("param", "statsd_addr").Warn("Deprecation warning: Config param name is deprecated and will be removed in future versions.")
config.StatsdAddr = s
}

}
53 changes: 22 additions & 31 deletions cmd/dkron.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/victorcoder/dkron/dkron"
)
Expand Down Expand Up @@ -37,23 +36,33 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

dkronCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/dkron/dkron.yml)")
dkronCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigName("dkron") // name of config file (without extension)
viper.AddConfigPath("/etc/dkron") // call multiple times to add many search paths
viper.AddConfigPath("$HOME/.dkron") // call multiple times to add many search paths
viper.AddConfigPath("./config") // call multiple times to add many search paths
viper.SetEnvPrefix("dkron") // will be uppercased automatically
viper.AutomaticEnv()
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName("dkron") // name of config file (without extension)
viper.AddConfigPath("/etc/dkron") // call multiple times to add many search paths
viper.AddConfigPath("$HOME/.dkron") // call multiple times to add many search paths
viper.AddConfigPath("./config") // call multiple times to add many search paths
}

viper.SetEnvPrefix("dkron")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv() // read in environment variables that match

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logrus.WithError(err).Info("No valid config found: Applying default values.")
}

viper.Unmarshal(config)

cliTags := viper.GetStringSlice("tag")
var tags map[string]string

Expand All @@ -64,32 +73,14 @@ func initConfig() {
}
} else {
tags = viper.GetStringMapString("tags")
fmt.Println(tags)
}

server := viper.GetBool("server")
nodeName := viper.GetString("node_name")

if server {
tags["dkron_server"] = "true"
} else {
tags["dkron_server"] = "false"
}
tags["dkron_server"] = strconv.FormatBool(config.Server)
tags["dkron_version"] = dkron.Version

dkron.InitLogger(viper.GetString("log_level"), nodeName)
config.Tags = tags

dkronCmd.Flags().VisitAll(func(f *pflag.Flag) {
fmt.Println(f.Value.String())
v := strings.Replace(f.Name, "-", "_", -1)
if f.Value.String() != f.DefValue {
viper.Set(v, f.Value.String())
} else {
viper.SetDefault(v, f.Value.String())
}
})

viper.Unmarshal(config)
spew.Dump(config)
dkron.InitLogger(viper.GetString("log_level"), config.NodeName)
}

// unmarshalTags is a utility function which takes a slice of strings in
Expand Down
Loading

0 comments on commit bb9c9cc

Please sign in to comment.