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

Fixes #25307 - Add ability to disable defaults #293

Merged
merged 4 commits into from
Nov 2, 2018

Conversation

mbacovsky
Copy link
Member

Presence of Defaults can be set in cli_config.yml with
:use_defaults: true/false or on CLI with --use-defaults
--no-use-defaults respectively. both ways can be combined.
The value is propagated in the context as :use_defaults.
The defaults are enabled by default.

Hammer settings class was chenged so that it can be the single
source of default values which has three benefits

  • we don't have to fallback to default with every use of the value
  • we can keep the values commented out in config which makes it easier
    to changelater
  • there is single place to look for the default setting

Copy link
Member

@tstrachota tstrachota left a comment

Choose a reason for hiding this comment

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

Looks about right. I added some refactoring suggestions for better code readability.

@@ -59,9 +64,16 @@ def self.path_history
@path_history
end

def self.default_values
Copy link
Member

Choose a reason for hiding this comment

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

How about renaming it to default_settings ?
It's probably just because of this PR's context, but the name associated me different functionality.

end

HammerCLI::MainCommand.subcommand "defaults", _("Defaults management"), HammerCLI::DefaultsCommand
if HammerCLI::Settings.get(:use_defaults)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep the command in. I find it confusing that a command would disappear when you pass --no-defaults

Copy link
Member Author

Choose a reason for hiding this comment

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

@tstrachota Removing commands was easy way to prevent the defaults.yaml from rewriting when user stores new value with defaults disabled. I can make it fail on attempt to store the defaults. Would that be better?

Copy link
Member

Choose a reason for hiding this comment

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

I'd vote for fail on attempt and showing user a some kind of warning?

Copy link
Member

Choose a reason for hiding this comment

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

IMHO it's totally fine to save and list defaults even --no-use-defaults is passed. The option should just deactivate using the defaults in requests.

return @defaults if @defaults
settings = HammerCLI::Settings
default_values = settings.get(:use_defaults) ? settings.get(:defaults) : {}
@defaults = Defaults.new(default_values)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's just me, but this seems difficult to read.
How about:

@defaults ||= Defaults.new(default_values_from_settings)

private
def default_values_from_settings
  HammerCLI::Settings.get(:use_defaults) ? HammerCLI::Settings.get(:defaults) : {}
end

Copy link
Member Author

Choose a reason for hiding this comment

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

@tstrachota it's a way better! You were awarded by the rubocop of the day trophy. 🤖

@@ -92,6 +94,8 @@ HammerCLI::Settings.load({
:ssl_with_basic_auth => preparser.ssl_with_basic_auth?
}})

HammerCLI::Settings.load({:use_defaults => preparser.use_defaults?}) unless preparser.use_defaults?.nil?
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it a duplication of https://github.com/theforeman/hammer-cli/pull/293/files#diff-aca6a835822ab9b99782be0a5f01f8ddR87 ? If it's a shorcut for Settings.get(:_params, :use_defaults), why keep it under _params then?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ofedoren Exactly. I had a reason to have it there but it was bad idea indeed. Removed.

Processing of defaults can be altered in cli_config.yml with
:use_defaults: true/false or on CLI with --use-defaults
--no-use-defaults respectively. both ways can be combined.
The value is propagated in the context as :use_defaults.
The defaults are enabled by default.

Hammer settings class was changed so that it can be the single
source of default values which has three benefits
 - we don't have to fallback to default with every use of the value
 - we can keep the values commented out in config which makes it easier
to changelater
 - there is single place to look for the default setting
@mbacovsky
Copy link
Member Author

@tstrachota, @ofedoren: comments addressed in separate commit. Is it better?
Please squash before merging ;)

@tstrachota
Copy link
Member

Looks ok now. Just one question regarding the design @mbacovsky:
Have you considered just adding equivalent of unless context[:use_defaults] here https://github.com/theforeman/hammer-cli/blob/master/lib/hammer_cli/abstract.rb#L250 ? It seems as the most straightforward solution to me. It would probably also allow keeping the defaults commands' functionality on even with disabled defaults.

@mbacovsky
Copy link
Member Author

@tstrachota updated. I removed the restriction to save the defaults when disabled. I also moved the logic to the option source.

@mbacovsky
Copy link
Member Author

@tstrachota updated. Last version?

@tstrachota
Copy link
Member

Looks good and works as expected now. Thanks @mbacovsky

@tstrachota tstrachota merged commit f9b27dc into theforeman:master Nov 2, 2018
mbacovsky added a commit that referenced this pull request Nov 6, 2018
Fixes #25307 - Add ability to disable defaults

Processing of defaults can be altered in cli_config.yml with
:use_defaults: true/false or on CLI with --use-defaults
--no-use-defaults respectively. both ways can be combined.
The value is propagated in the context as :use_defaults.
The defaults are enabled by default.

Hammer settings class was changed so that it can be the single
source of default values which has three benefits
 - we don't have to fallback to default with every use of the value
 - we can keep the values commented out in config which makes it easier
to change later
 - there is single place to look for the default setting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants