Skip to content

Commit

Permalink
Move config init to agent command (#1465)
Browse files Browse the repository at this point in the history
Config init was being done on the root level command but only the agent command was using config values.

Now config init is done as pre-run of agent command only, getting rid of extra messages in other commands when the config was missing.
  • Loading branch information
vcastellm authored Feb 6, 2024
1 parent 7a2d490 commit c35d31e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var agentCmd = &cobra.Command{
Short: "Start a dkron agent",
Long: `Start a dkron agent that schedules jobs, listens for executions and runs executors.
It also runs a web UI.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return initConfig()
},
// Run will execute the main functions of the agent command.
// This includes the main eventloop and starting the server if enabled.
//
Expand All @@ -42,8 +45,9 @@ It also runs a web UI.`,
func init() {
dkronCmd.AddCommand(agentCmd)

agentCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path")
agentCmd.Flags().AddFlagSet(dkron.ConfigFlagSet())
viper.BindPFlags(agentCmd.Flags())
_ = viper.BindPFlags(agentCmd.Flags())
}

func agentRun(args ...string) error {
Expand Down
14 changes: 5 additions & 9 deletions cmd/dkron.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ func Execute() {
}
}

func init() {
cobra.OnInitialize(initConfig)

dkronCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
func initConfig() error {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
Expand All @@ -68,7 +62,7 @@ func initConfig() {
}

if err := viper.Unmarshal(config); err != nil {
logrus.WithError(err).Fatal("config: Error unmarshalling config")
return fmt.Errorf("config: Error unmarshalling config: %s", err)
}

cliTags := viper.GetStringSlice("tag")
Expand All @@ -77,7 +71,7 @@ func initConfig() {
if len(cliTags) > 0 {
tags, err = UnmarshalTags(cliTags)
if err != nil {
logrus.WithError(err).Fatal("config: Error unmarshalling cli tags")
return fmt.Errorf("config: Error unmarshalling cli tags: %s", err)
}
} else {
tags = viper.GetStringMapString("tags")
Expand All @@ -86,4 +80,6 @@ func initConfig() {
config.Tags = tags

dkron.InitLogger(viper.GetString("log-level"), config.NodeName)

return nil
}

0 comments on commit c35d31e

Please sign in to comment.