Class: ChefCLI::Command::Undelete

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefCLI::Configurable

#chef_config, #chefcli_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

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(*args) ⇒ Undelete

Returns a new instance of Undelete.



72
73
74
75
76
77
# File 'lib/chef-cli/command/undelete.rb', line 72

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.



68
69
70
# File 'lib/chef-cli/command/undelete.rb', line 68

def ui
  @ui
end

#undo_record_idObject (readonly)

Returns the value of attribute undo_record_id.



70
71
72
# File 'lib/chef-cli/command/undelete.rb', line 70

def undo_record_id
  @undo_record_id
end

Instance Method Details

#apply_params!(params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef-cli/command/undelete.rb', line 118

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)


100
101
102
# File 'lib/chef-cli/command/undelete.rb', line 100

def debug?
  !!config[:debug]
end

#handle_error(error) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/chef-cli/command/undelete.rb', line 108

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)


104
105
106
# File 'lib/chef-cli/command/undelete.rb', line 104

def list_undo_records?
  @list_undo_records
end

#run(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef-cli/command/undelete.rb', line 79

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



93
94
95
96
97
98
# File 'lib/chef-cli/command/undelete.rb', line 93

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