class ChefCLI::Command::Export

def apply_params!(params)

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

def archive?

def archive?
  !!config[:archive]
end

def debug?

def debug?
  !!config[:debug]
end

def export_service

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

def export_target

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

def handle_error(error)

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

def initialize(*args)

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

def run(params = [])

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