Class: ChefDK::Command::ShowPolicy

Inherits:
Base
  • Object
show all
Includes:
ChefDK::Configurable
Defined in:
lib/chef-dk/command/show_policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader, #generator_config, #knife_config

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(*args) ⇒ ShowPolicy

Returns a new instance of ShowPolicy.



79
80
81
82
83
84
85
# File 'lib/chef-dk/command/show_policy.rb', line 79

def initialize(*args)
  super
  @show_all_policies = nil
  @policy_name = nil
  @policy_group = nil
  @ui = UI.new
end

Instance Attribute Details

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



77
78
79
# File 'lib/chef-dk/command/show_policy.rb', line 77

def policy_group
  @policy_group
end

#policy_nameObject (readonly)

Returns the value of attribute policy_name.



75
76
77
# File 'lib/chef-dk/command/show_policy.rb', line 75

def policy_name
  @policy_name
end

#uiObject

Returns the value of attribute ui.



73
74
75
# File 'lib/chef-dk/command/show_policy.rb', line 73

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef-dk/command/show_policy.rb', line 133

def apply_params!(params)
  remaining_args = parse_options(params)

  if remaining_args.empty? && show_summary_diff?
    ui.err("The --summary-diff option can only be used when showing a single policy")
    ui.err("")
    ui.err(opt_parser)
    false
  elsif remaining_args.empty?
    @show_all_policies = true
    true
  elsif remaining_args.size == 1
    @policy_name = remaining_args.first
    @show_all_policies = false
    true
  elsif remaining_args.size == 2
    @policy_name = remaining_args[0]
    @policy_group = remaining_args[1]
    @show_all_policies = false
    true
  else
    ui.err("Too many arguments")
    ui.err("")
    ui.err(opt_parser)
    false
  end
end

#debug?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/chef-dk/command/show_policy.rb', line 107

def debug?
  !!config[:debug]
end

#enable_pager?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/chef-dk/command/show_policy.rb', line 119

def enable_pager?
  config[:pager]
end

#handle_error(error) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/chef-dk/command/show_policy.rb', line 123

def handle_error(error)
  ui.err("Error: #{error.message}")
  if error.respond_to?(:reason)
    ui.err("Reason: #{error.reason}")
    ui.err("")
    ui.err(error.extended_error_info) if debug?
    ui.err(error.cause.backtrace.join("\n")) if debug?
  end
end

#run(params) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/chef-dk/command/show_policy.rb', line 87

def run(params)
  return 1 unless apply_params!(params)
  show_policy_service.run
  0
rescue PolicyfileServiceError => e
  handle_error(e)
  1
end

#show_orphans?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/chef-dk/command/show_policy.rb', line 115

def show_orphans?
  config[:show_orphans]
end

#show_policy_serviceObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/chef-dk/command/show_policy.rb', line 96

def show_policy_service
  @policy_list_service ||=
    PolicyfileServices::ShowPolicy.new(config: chef_config,
                                       ui: ui,
                                       policy_name: policy_name,
                                       policy_group: policy_group,
                                       show_orphans: show_orphans?,
                                       summary_diff: show_summary_diff?,
                                       pager: enable_pager?)
end

#show_summary_diff?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/chef-dk/command/show_policy.rb', line 111

def show_summary_diff?
  false
end