diff --git a/bin/hammer b/bin/hammer index 41126696..ec8a1d1b 100755 --- a/bin/hammer +++ b/bin/hammer @@ -13,6 +13,7 @@ require 'hammer_cli/options/normalizers' # Option descriptions are never displayed and thus do not require translation. class PreParser < Clamp::Command option ['-v', '--[no-]verbose'], :flag, _('Be verbose (or not). True by default') + option ['--[no-]use-defaults'], :flag, _('Enable/disable stored defaults. Enabled by default') option ['-q', '--quiet'], :flag, _('Completely silent') option ["-d", "--debug"], :flag, "show debugging output" option ["-c", "--config"], "CFG_FILE", "path to custom config file" do |path| @@ -92,6 +93,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? + if HammerCLI::Settings.get(:ui, :mark_translated) include HammerCLI::I18n::Debug end diff --git a/config/cli_config.template.yml b/config/cli_config.template.yml index f6c00cb5..405c2731 100644 --- a/config/cli_config.template.yml +++ b/config/cli_config.template.yml @@ -32,6 +32,9 @@ # 2 - data, messages, interactive I/O, progress bars. Equals to --verbose. :verbosity: 2 +# Enable/disable defaults. Preset default values for the options won't be used. +#:use_defaults: true + #:log_owner: 'foreman' #:log_group: 'foreman' diff --git a/lib/hammer_cli/abstract.rb b/lib/hammer_cli/abstract.rb index eb43d61b..7e66c208 100644 --- a/lib/hammer_cli/abstract.rb +++ b/lib/hammer_cli/abstract.rb @@ -245,10 +245,9 @@ def option_collector def option_sources - [ - HammerCLI::Options::Sources::CommandLine.new(self), - HammerCLI::Options::Sources::SavedDefaults.new(context[:defaults], logger) - ] + sources = [HammerCLI::Options::Sources::CommandLine.new(self)] + sources << HammerCLI::Options::Sources::SavedDefaults.new(context[:defaults], logger) if context[:use_defaults] + sources end private diff --git a/lib/hammer_cli/context.rb b/lib/hammer_cli/context.rb index dcd6569f..c8e6ffc5 100644 --- a/lib/hammer_cli/context.rb +++ b/lib/hammer_cli/context.rb @@ -7,6 +7,7 @@ def self.context :is_tty? => HammerCLI.tty?, :api_connection => HammerCLI::Connection.new(Logging.logger['Connection']), :no_headers => HammerCLI::Settings.get(:ui, :no_headers), + :use_defaults => HammerCLI::Settings.get(:use_defaults), :capitalization => HammerCLI.capitalization, :verbosity => (HammerCLI::Settings.get(:verbosity) || HammerCLI::V_VERBOSE).to_i } diff --git a/lib/hammer_cli/defaults.rb b/lib/hammer_cli/defaults.rb index f41d8264..c8de8fb8 100644 --- a/lib/hammer_cli/defaults.rb +++ b/lib/hammer_cli/defaults.rb @@ -9,7 +9,6 @@ class DefaultsPathError < DefaultsError; end attr_reader :defaults_settings def initialize(settings, file_path = nil) - @defaults_settings = settings || {} @path = file_path || DEFAULT_FILE end @@ -100,8 +99,7 @@ def switch_to_name(opt) end def self.defaults - @defaults ||= Defaults.new(HammerCLI::Settings.settings[:defaults]) - + @defaults ||= Defaults.new(HammerCLI::Settings.get(:defaults)) end HammerCLI::MainCommand.subcommand "defaults", _("Defaults management"), HammerCLI::DefaultsCommand diff --git a/lib/hammer_cli/main.rb b/lib/hammer_cli/main.rb index ce45fe51..5868ce09 100644 --- a/lib/hammer_cli/main.rb +++ b/lib/hammer_cli/main.rb @@ -10,6 +10,7 @@ class MainCommand < AbstractCommand option ['-q', '--quiet'], :flag, _('Completely silent') do context[:verbosity] = HammerCLI::V_QUIET end + option ['--[no-]use-defaults'], :flag, _('Enable/disable stored defaults. Enabled by default') option ["-d", "--debug"], :flag, _("Show debugging output"), :context_target => :debug option ["-r", "--reload-cache"], :flag, _("Force reload of Apipie cache") diff --git a/lib/hammer_cli/settings.rb b/lib/hammer_cli/settings.rb index 73739f73..dec1bb3c 100644 --- a/lib/hammer_cli/settings.rb +++ b/lib/hammer_cli/settings.rb @@ -46,6 +46,11 @@ def self.load(settings_hash) end def self.clear + empty + load(default_settings) + end + + def self.empty settings.clear path_history.clear end @@ -59,9 +64,16 @@ def self.path_history @path_history end + def self.default_settings + { + :use_defaults => true + } + end + private + def self.settings - @settings_hash ||= {} + @settings_hash ||= default_settings @settings_hash end diff --git a/test/unit/abstract_test.rb b/test/unit/abstract_test.rb index a66a154f..e767b6a8 100644 --- a/test/unit/abstract_test.rb +++ b/test/unit/abstract_test.rb @@ -373,7 +373,8 @@ def options before do @defaults = mock() @defaults.stubs(:get_defaults).returns(nil) - @cmd = TestDefaultsCmd.new("", { :defaults => @defaults }) + @cmd = TestDefaultsCmd.new("", { :defaults => @defaults, :use_defaults => true }) + @cmd_no_defaults = TestDefaultsCmd.new("", { :defaults => @defaults, :use_defaults => false }) end it 'provides default value for an option flag' do @@ -381,6 +382,11 @@ def options assert_equal({'different_attr_name' => 2}, @cmd.options) end + it 'does not set default value if the defaults are disabled' do + @defaults.expects(:get_defaults).never() + assert_equal({}, @cmd_no_defaults.options) + end + it 'prefers values from command line' do @defaults.stubs(:get_defaults).with('--test').returns(2) @cmd.run(['--test=1']) diff --git a/test/unit/settings_test.rb b/test/unit/settings_test.rb index 3ef389dc..d8de3af1 100644 --- a/test/unit/settings_test.rb +++ b/test/unit/settings_test.rb @@ -44,6 +44,7 @@ end it "dumps all settings" do + settings.empty data = {:a => 1, :b => 2} settings.load(data) settings.dump.must_equal data @@ -69,11 +70,23 @@ settings.get(:x).size.must_equal 4 end - it "clear wipes all settings" do + it "clear wipes all settings but default values" do settings.load({:a => 1, :b => 2}) settings.clear settings.get(:a).must_be_nil settings.get(:b).must_be_nil + settings.default_settings.each { |key, val| settings.get(key).must_equal val } + end + + it "empty wipes all settings including default values" do + settings.load({:a => 1, :b => 2}) + settings.empty + settings.dump.must_equal({}) + end + + it "initializes settings with default settings" do + settings.instance_variable_set(:@settings_hash, nil) + settings.dump.must_equal settings.default_settings end context "load from paths" do