Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Add paging support when showing a specific policy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Jun 12, 2015
1 parent 432ce16 commit 046a2e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/chef-dk/command/show_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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?
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions lib/chef-dk/policyfile_services/show_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

require 'chef-dk/policyfile/comparison_base'
require 'chef-dk/policyfile/lister'
require 'chef-dk/pager'

module ChefDK
module PolicyfileServices
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -107,6 +109,10 @@ def show_summary_diff?
@summary_diff
end

def enable_pager?
@enable_pager
end

def report
@report ||= ReportPrinter.new(ui)
end
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/unit/policyfile_services/show_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 046a2e7

Please sign in to comment.