Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move config init to agent command #1465

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading