Class: ChefDK::Command::DeletePolicyGroup

Inherits:
Base
  • Object
show all
Includes:
ChefDK::Configurable
Defined in:
lib/chef-dk/command/delete_policy_group.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) ⇒ DeletePolicyGroup

Returns a new instance of DeletePolicyGroup.



61
62
63
64
65
66
# File 'lib/chef-dk/command/delete_policy_group.rb', line 61

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

Instance Attribute Details

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



59
60
61
# File 'lib/chef-dk/command/delete_policy_group.rb', line 59

def policy_group
  @policy_group
end

#uiObject

Returns the value of attribute ui.



57
58
59
# File 'lib/chef-dk/command/delete_policy_group.rb', line 57

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef-dk/command/delete_policy_group.rb', line 99

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

  if remaining_args.size == 1
    @policy_group = remaining_args.first
    true
  elsif remaining_args.empty?
    ui.err("You must specify the POLICY_GROUP to delete.")
    ui.err("")
    ui.err(opt_parser)
    false
  else
    ui.err("Too many arguments")
    ui.err("")
    ui.err(opt_parser)
    false
  end
end

#debug?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/chef-dk/command/delete_policy_group.rb', line 85

def debug?
  !!config[:debug]
end

#handle_error(error) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/chef-dk/command/delete_policy_group.rb', line 89

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

#rm_policy_group_serviceObject



78
79
80
81
82
83
# File 'lib/chef-dk/command/delete_policy_group.rb', line 78

def rm_policy_group_service
  @rm_policy_group_service ||=
    PolicyfileServices::RmPolicyGroup.new(config: chef_config,
                                          ui: ui,
                                          policy_group: policy_group)
end

#run(params) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/chef-dk/command/delete_policy_group.rb', line 68

def run(params)
  return 1 unless apply_params!(params)
  rm_policy_group_service.run
  ui.msg("This operation can be reversed by running `chef undelete --last`.")
  0
rescue PolicyfileServiceError => e
  handle_error(e)
  1
end