Skip to content

[amtool] use kingpin.v2#1330

Merged
stuartnelson3 merged 6 commits intoprometheus:masterfrom
simonpasquier:kingpin-v2
Apr 24, 2018
Merged

[amtool] use kingpin.v2#1330
stuartnelson3 merged 6 commits intoprometheus:masterfrom
simonpasquier:kingpin-v2

Conversation

@simonpasquier
Copy link
Member

This is an attempt to fix #1328. To workaround the lack of configuration file support in kingpin v2, it injects environment variables after reading the flags from the YAML files. This isn't pretty but at least seems to work (this would need more testing obviously).

As noted in #976, there's no way to have longer help descriptions when using the --help-long flag. I've tried several approaches but failed. I don't think it is even possible. So switching to v2 would mean to get rid of either the short helps or the long helps.

Waiting for @stuartnelson3 & @thetincho's comments...

)

resolver := configResolver{}
// loadConfig loads flags from YAML files and injects them as environment variables.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could any of these be secrets? The environment is not a safe place for secrets.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see your concern. Right now amtool doesn't expose any secret flags or arguments. Which isn't saying it won't.

Copy link
Contributor

Choose a reason for hiding this comment

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

An idea would be to store this in a public map at init time and use it to populate defaults for flags.. but maybe it is too convoluted

Copy link
Contributor

Choose a reason for hiding this comment

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

The alertmanager.url can potentially contain basic auth credentials.

Any ideas on how else to implement this? @thetincho's map idea might work. Parse the file and populate a struct/map, and set those as the default values for their appropriate flags?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've just tried and it doesn't work because of alertmanager.url (at least) which is a required flag . As stated in the kingpin documentation: You can not provide a Default() value to a Required() flag.

I've tried the suggestion from alecthomas/kingpin#208 (comment) but it doesn't work either because some flags are only valid for some commands and you can't have a flag present both in the configuration file and in the program's arguments (unless the flag can be repeated).

Copy link
Contributor

@stuartnelson3 stuartnelson3 Apr 19, 2018

Choose a reason for hiding this comment

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

Could we move it from being a Required flag, provide the value in the config file, and validate after the fact ourselves?

EDIT:
The flag will still attempt to create a URL var, so maybe it'll be as simple as doing a nil check and erroring

Copy link
Member Author

Choose a reason for hiding this comment

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

@stuartnelson3 good idea, it seems to work...

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@NightTsarina
Copy link
Contributor

Thanks for this!

I have no comments on the PR, except my small suggestion.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@stuartnelson3
Copy link
Contributor

This has the fantastic side-effect of allowing check-configto no longer require --alertmanager.url

#1199

Copy link
Contributor

@stuartnelson3 stuartnelson3 left a comment

Choose a reason for hiding this comment

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

a question and a small request, but looks awesome!

cli/root.go Outdated
continue
}
name := f.Model().Name
if name == "help" || name == "help-long" || name == "help(man" {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's help(man?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops typo.

return nil, err
}
for k, v := range m {
if new, ok := legacyFlags[k]; ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

it appears to be compiling here, but new is a keyword in golang and should be renamed

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the catch-up!

@stuartnelson3 stuartnelson3 changed the title [WIP] cli: use kingpin.v2 [amtool] use kingpin.v2 Apr 20, 2018
@simonpasquier
Copy link
Member Author

Thanks @stuartnelson3, from my testing, the precedence between command line flags and configuration files works as expected but I plan to increase the coverage in the unit tests.

There's still a question around short vs. long help messages though.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@stuartnelson3
Copy link
Contributor

As noted in #976, there's no way to have longer help descriptions when using the --help-long flag. I've tried several approaches but failed. I don't think it is even possible. So switching to v2 would mean to get rid of either the short helps or the long helps.

I would prefer to keep the longer help message. The documentation isn't too long, and it covers the use cases. We would have to figure out where to add the long help information on prometheus.io if we were to get rid of it.

@simonpasquier
Copy link
Member Author

I would prefer to keep the longer help message.

This is my feeling too: having it buried under --help-long isn't very user-friendly. I'll update the PR in this direction then.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Copy link
Contributor

@stuartnelson3 stuartnelson3 left a comment

Choose a reason for hiding this comment

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

one final nit!

var (
longHelpText = map[string]string{}
app = kingpin.New("amtool", "Alertmanager CLI").DefaultEnvars()
app = kingpin.New("amtool", helpRoot).DefaultEnvars()
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 DefaultEnvars() can be removed, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

It could be deleted although the current code already has DefaultEnvars() which is why I didn't change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

k. I doubt anyone is relying on it, but I suppose if someone is we can leave it for now.

@stuartnelson3 stuartnelson3 merged commit dc5fc02 into prometheus:master Apr 24, 2018
@simonpasquier simonpasquier deleted the kingpin-v2 branch April 24, 2018 08:32
mxinden pushed a commit to mxinden/alertmanager that referenced this pull request May 5, 2018
* Use default values to store values from config
* fix typo and reserved keywork
* move to long help texts
* add one more unit test for resolver
* update comments

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
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.

amtool doesn't depend on a stable version of kingpin

4 participants