Class: ChefDK::Command::Export

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(*args) ⇒ Export



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

def initialize(*args)
  super
  @push = nil
  @ui = nil
  @policyfile_relative_path = nil
  @export_dir = nil
  @chef_config = nil
  @ui = UI.new
end

Instance Attribute Details

#export_dirObject (readonly)

Returns the value of attribute export_dir.



65
66
67
# File 'lib/chef-dk/command/export.rb', line 65

def export_dir
  @export_dir
end

#policyfile_relative_pathObject (readonly)

Returns the value of attribute policyfile_relative_path.



64
65
66
# File 'lib/chef-dk/command/export.rb', line 64

def policyfile_relative_path
  @policyfile_relative_path
end

#uiObject

Returns the value of attribute ui.



67
68
69
# File 'lib/chef-dk/command/export.rb', line 67

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef-dk/command/export.rb', line 114

def apply_params!(params)
  remaining_args = parse_options(params)
  case remaining_args.size
  when 1
    @export_dir = remaining_args[0]
  when 2
    @policyfile_relative_path, @export_dir = remaining_args
  else
    ui.err(banner)
    return false
  end
  true
end

#debug?Boolean



93
94
95
# File 'lib/chef-dk/command/export.rb', line 93

def debug?
  !!config[:debug]
end

#export_serviceObject



97
98
99
100
101
102
# File 'lib/chef-dk/command/export.rb', line 97

def export_service
  @export_service ||= PolicyfileServices::ExportRepo.new(policyfile: policyfile_relative_path,
                                                 export_dir: export_dir,
                                                 root_dir: Dir.pwd,
                                                 force: config[:force])
end

#handle_error(error) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/chef-dk/command/export.rb', line 104

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

#run(params = []) ⇒ Object



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

def run(params = [])
  return 1 unless apply_params!(params)
  export_service.run
  ui.msg("Exported policy '#{export_service.policyfile_lock.name}' to #{export_dir}")
  0
rescue ExportDirNotEmpty => e
  ui.err("ERROR: " + e.message)
  ui.err("Use --force to force export")
  1
rescue PolicyfileServiceError => e
  handle_error(e)
  1
end