From 046a2e7c1389e58d86d5736a2a5616f34035810e Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Fri, 12 Jun 2015 15:00:58 -0700 Subject: [PATCH] Add paging support when showing a specific policy --- lib/chef-dk/command/show_policy.rb | 13 ++++++++++++- lib/chef-dk/policyfile_services/show_policy.rb | 11 +++++++++-- spec/unit/policyfile_services/show_policy_spec.rb | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/chef-dk/command/show_policy.rb b/lib/chef-dk/command/show_policy.rb index ccf65c60b..ff967bc5d 100644 --- a/lib/chef-dk/command/show_policy.rb +++ b/lib/chef-dk/command/show_policy.rb @@ -53,6 +53,12 @@ class ShowPolicy < Base description: "Show policy revisions that are unassigned", default: false + option :pager, + long: "--[no-]pager", + description: "Enable/disable paged policyfile lock ouput (default: enabled)", + default: true, + boolean: true + option :config_file, short: "-c CONFIG_FILE", long: "--config CONFIG_FILE", @@ -96,7 +102,8 @@ def show_policy_service policy_name: policy_name, policy_group: policy_group, show_orphans: show_orphans?, - summary_diff: show_summary_diff?) + summary_diff: show_summary_diff?, + pager: enable_pager?) end def debug? @@ -111,6 +118,10 @@ def show_orphans? config[:show_orphans] end + def enable_pager? + config[:pager] + end + def handle_error(error) ui.err("Error: #{error.message}") if error.respond_to?(:reason) diff --git a/lib/chef-dk/policyfile_services/show_policy.rb b/lib/chef-dk/policyfile_services/show_policy.rb index d2a8e6513..6d824e85c 100644 --- a/lib/chef-dk/policyfile_services/show_policy.rb +++ b/lib/chef-dk/policyfile_services/show_policy.rb @@ -17,6 +17,7 @@ require 'chef-dk/policyfile/comparison_base' require 'chef-dk/policyfile/lister' +require 'chef-dk/pager' module ChefDK module PolicyfileServices @@ -67,13 +68,14 @@ def list(items) attr_reader :policy_group - def initialize(config: nil, ui: nil, policy_name: nil, policy_group: nil, show_orphans: false, summary_diff: false) + def initialize(config: nil, ui: nil, policy_name: nil, policy_group: nil, show_orphans: false, summary_diff: false, pager: false) @chef_config = config @ui = ui @policy_name = policy_name @policy_group = policy_group @show_orphans = show_orphans @summary_diff = summary_diff + @enable_pager = pager end def run @@ -107,6 +109,10 @@ def show_summary_diff? @summary_diff end + def enable_pager? + @enable_pager + end + def report @report ||= ReportPrinter.new(ui) end @@ -117,7 +123,8 @@ def policy_lister def display_policy_revision lock = Policyfile::ComparisonBase::PolicyGroup.new(policy_group, policy_name, http_client).lock - ui.msg(FFI_Yajl::Encoder.encode(lock, pretty: true)) + pager = Pager.new(enable_pager: enable_pager?) + pager.with_pager { |p| p.ui.msg(FFI_Yajl::Encoder.encode(lock, pretty: true)) } end def display_all_policies diff --git a/spec/unit/policyfile_services/show_policy_spec.rb b/spec/unit/policyfile_services/show_policy_spec.rb index c64195831..46553fbbd 100644 --- a/spec/unit/policyfile_services/show_policy_spec.rb +++ b/spec/unit/policyfile_services/show_policy_spec.rb @@ -819,7 +819,11 @@ let(:policyfile_lock_json) { FFI_Yajl::Encoder.encode(policyfile_lock_data, pretty: true) } + let(:pager) { instance_double("ChefDK::Pager", ui: ui) } + before do + allow(ChefDK::Pager).to receive(:new).and_return(pager) + allow(pager).to receive(:with_pager).and_yield(pager) allow(http_client).to receive(:get).with("policy_groups/dev/policies/appserver").and_return(policyfile_lock_data) end