Class: Moose::Inventory::Cli::Application

Inherits:
Thor
  • Object
show all
Includes:
Helpers
Defined in:
lib/moose_inventory/cli/application.rb

Overview

Top-level Thor application for moose-inventory. rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from Helpers

Helpers::AUTOMATIC_GROUP

Instance Method Summary collapse

Instance Method Details

#consoleObject



87
88
89
# File 'lib/moose_inventory/cli/application.rb', line 87

def console
  Moose::Inventory::Cli::Console.new(context: inventory_context).run
end

#doctorObject



36
37
38
39
40
# File 'lib/moose_inventory/cli/application.rb', line 36

def doctor
  report = build_operation(Moose::Inventory::Operations::InventoryDoctor).call
  render_doctor_report(report)
  exit(1) unless report[:ok]
end

#export(file = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moose_inventory/cli/application.rb', line 43

def export(file = nil)
  snapshot = build_operation(Moose::Inventory::Operations::InventorySnapshot).export
  output = serialize_snapshot(snapshot)

  if file.nil?
    puts output
  else
    File.write(file, output)
    puts "Exported inventory snapshot to #{file}."
  end
end

#import(file) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/moose_inventory/cli/application.rb', line 58

def import(file)
  snapshot = YAML.safe_load_file(file, aliases: false)
  operation = build_operation(Moose::Inventory::Operations::ImportInventorySnapshot)
  return render_import_preview(operation.preview(snapshot: snapshot)) if options[:preview]

  abort('ERROR: --preview-format requires --preview.') if options[:preview_format]

  result = operation.call(snapshot: snapshot)
  record_audit({ command: 'import', action: 'import', entity_type: 'inventory',
                 entity_names: file }, result: result)
  puts "Imported inventory snapshot from #{file}."
  puts "Created hosts: #{result.created_hosts}"
  puts "Created groups: #{result.created_groups}"
  puts "Variables changed: #{result.updated_variables}"
  puts "Associations added: #{result.associations}"
rescue Psych::SyntaxError => e
  abort("ERROR: Could not parse inventory snapshot '#{file}': #{e.message}")
rescue db.exceptions[:moose] => e
  abort("ERROR: #{e.message}")
end

#versionObject



30
31
32
# File 'lib/moose_inventory/cli/application.rb', line 30

def version
  puts "Version #{Moose::Inventory::VERSION}"
end