Class: ChefDK::Command::Undelete

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader, #default_chef_server_http_client, #generator_config, #knife_config, #reset_config!

Methods inherited from Base

#check_license_acceptance, #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) ⇒ Undelete

Returns a new instance of Undelete.



83
84
85
86
87
88
# File 'lib/chef-dk/command/undelete.rb', line 83

def initialize(*args)
  super
  @list_undo_records = false
  @undo_record_id = nil
  @ui = UI.new
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



79
80
81
# File 'lib/chef-dk/command/undelete.rb', line 79

def ui
  @ui
end

#undo_record_idObject (readonly)

Returns the value of attribute undo_record_id.



81
82
83
# File 'lib/chef-dk/command/undelete.rb', line 81

def undo_record_id
  @undo_record_id
end

Instance Method Details

#apply_params!(params) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef-dk/command/undelete.rb', line 129

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

  if !remaining_args.empty?
    ui.err("Too many arguments")
    ui.err("")
    ui.err(opt_parser)
    false
  elsif config[:undo_record_id].nil? && config[:undo_last].nil?
    @list_undo_records = true
    true
  elsif config[:undo_record_id] && config[:undo_last]
    ui.err("Error: options --last and --id cannot both be given.")
    ui.err("")
    ui.err(opt_parser)
    false
  elsif config[:undo_record_id]
    @undo_record_id = config[:undo_record_id]
    true
  elsif config[:undo_last]
    @undo_record_id = nil
    true
  end
end

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  !!config[:debug]
end

#handle_error(error) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/chef-dk/command/undelete.rb', line 119

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

#list_undo_records?Boolean

Returns:

  • (Boolean)


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

def list_undo_records?
  @list_undo_records
end

#run(params) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef-dk/command/undelete.rb', line 90

def run(params)
  return 1 unless apply_params!(params)

  if list_undo_records?
    undelete_service.list
  else
    undelete_service.run
  end
  0
rescue PolicyfileServiceError => e
  handle_error(e)
  1
end

#undelete_serviceObject



104
105
106
107
108
109
# File 'lib/chef-dk/command/undelete.rb', line 104

def undelete_service
  @undelete_service ||=
    PolicyfileServices::Undelete.new(config: chef_config,
                                     ui: ui,
                                     undo_record_id: undo_record_id)
end