Class: ChefCLI::Command::Export

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChefCLI::Configurable

#chef_config, #chefcli_config, #config_loader, #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) ⇒ Export

Returns a new instance of Export.



80
81
82
83
84
85
86
87
88
# File 'lib/chef-cli/command/export.rb', line 80

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.



76
77
78
# File 'lib/chef-cli/command/export.rb', line 76

def export_dir
  @export_dir
end

#policyfile_relative_pathObject (readonly)

Returns the value of attribute policyfile_relative_path.



75
76
77
# File 'lib/chef-cli/command/export.rb', line 75

def policyfile_relative_path
  @policyfile_relative_path
end

#uiObject

Returns the value of attribute ui.



78
79
80
# File 'lib/chef-cli/command/export.rb', line 78

def ui
  @ui
end

Instance Method Details

#apply_params!(params) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/chef-cli/command/export.rb', line 148

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(opt_parser)
    ui.err("\n")
    return false
  end
  true
end

#archive?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/chef-cli/command/export.rb', line 123

def archive?
  !!config[:archive]
end

#debug?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/chef-cli/command/export.rb', line 119

def debug?
  !!config[:debug]
end

#export_serviceObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/chef-cli/command/export.rb', line 127

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

#export_targetObject



111
112
113
114
115
116
117
# File 'lib/chef-cli/command/export.rb', line 111

def export_target
  if archive?
    export_service.archive_file_location
  else
    export_dir
  end
end

#handle_error(error) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/chef-cli/command/export.rb', line 138

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef-cli/command/export.rb', line 90

def run(params = [])
  return 1 unless apply_params!(params)

  export_service.run
  ui.msg("Exported policy '#{export_service.policyfile_lock.name}' to #{export_target}")
  unless archive?
    ui.msg("")
    ui.msg("To converge this system with the exported policy, run:")
    ui.msg("  cd #{export_dir}")
    ui.msg("  #{ChefCLI::Dist::INFRA_CLIENT_CLI} -z")
  end
  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