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

Returns a new instance of Undelete.



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

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.



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

def ui
  @ui
end

#undo_record_idObject (readonly)

Returns the value of attribute undo_record_id.



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

def undo_record_id
  @undo_record_id
end

Instance Method Details

#apply_params!(params) ⇒ Object



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

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)


109
110
111
# File 'lib/chef-dk/command/undelete.rb', line 109

def debug?
  !!config[:debug]
end

#handle_error(error) ⇒ Object



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

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)


113
114
115
# File 'lib/chef-dk/command/undelete.rb', line 113

def list_undo_records?
  @list_undo_records
end

#run(params) ⇒ Object



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

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



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

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