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

add workaround/option to select Sensu config file from within tool #17

Merged
merged 4 commits into from
Nov 29, 2016

Conversation

bparli
Copy link
Contributor

@bparli bparli commented Nov 29, 2016

I am using your sensu-client-go package to extend one of our tools to also run as a standalone sensu check. Since the tool already has command line flags I wasn't able to set the Sensu configuration file from the command line as the NewConfigFromFlagSet function expects.

  • Return error in NewConfigFromFlagSet if flagset.configFile is not set
  • Add exported function to set the Sensu config file from within the context of the tool to allow for setting the Sensu config file location

@@ -61,9 +76,10 @@ func NewConfigFromFlagSet(flagset *configFlagSet) (*Config, error) {
if err := json.Unmarshal(buf, &cfg.config); err != nil {
return nil, err
}
return &cfg, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to replace the logic inside this if block with a call to NewConfigFromFile in order to avoid code duplication?

}
return nil, errors.New("No config file is set")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want to return an error here, because the client is able to subscribe to checks from the Sensu server via the SENSU_CLIENT_SUBSCRIPTIONS environment variable and you can use RABBITMQ_URI to specify the URI of the Sensu server, which I believe lets you run the client without a config file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, internally we only use the environment variables to configure the clients. It's a desired behavour.

@bparli
Copy link
Contributor Author

bparli commented Nov 29, 2016

I've cleaned up the duplicate code and also removed the error return to realign with your conventions.

You've obviously thought about this more than me, but it seems to me using Environment variables like this deviates from the main sensu project conventions. More importantly though, from what I can tell you could hose the sensu server if the SENSU_CLIENT_NAME environment variable is fat-fingered or not set.

In any case, thanks for the PR review

if err := json.Unmarshal(buf, &cfg.config); err != nil {
func NewConfigFromFlagSet(flagset *configFlagSet) (*Config, error) {
if flagset != nil && flagset.configFile != "" {
cfg, err := NewConfigFromFile(flagset, flagset.configFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just do: return NewConfigFromFile(flagset, flagset.configFile)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thats much better

Copy link
Member

@AlexisMontagne AlexisMontagne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bparli i gave you some syntax feedbacks, you can use the golang linter and govet to catch these smells easily

return cfg, nil
} else {
var cfg = Config{flagset, &configPayload{}}
return &cfg, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to assign a variable, you can directlydo return &Config{flagset, &configPayload{}}, nil

return nil, err
}
return cfg, nil
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this else is not really needed, you can just drop it

Copy link
Member

@AlexisMontagne AlexisMontagne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AlexisMontagne AlexisMontagne merged commit 783414b into upfluence:master Nov 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants