class ChefCLI::PolicyfileServices::UpdateAttributes

def assert_policy_and_lock_present!

def assert_policy_and_lock_present!
  unless File.exist?(policyfile_expanded_path)
    raise PolicyfileNotFound, "Policyfile not found at path #{policyfile_expanded_path}"
  end
  unless File.exist?(policyfile_lock_expanded_path)
    raise LockfileNotFound, "Policyfile lock not found at path #{policyfile_lock_expanded_path}"
  end
end

def initialize(policyfile: nil, ui: nil, root_dir: nil, chef_config: nil)

def initialize(policyfile: nil, ui: nil, root_dir: nil, chef_config: nil)
  @ui = ui
  policyfile_rel_path = policyfile || "Policyfile.rb"
  policyfile_full_path = File.expand_path(policyfile_rel_path, root_dir)
  @storage_config = Policyfile::StorageConfig.new.use_policyfile(policyfile_full_path)
  @updated = false
  @chef_config = chef_config
end

def policyfile_compiler

def policyfile_compiler
  @policyfile_compiler ||= ChefCLI::PolicyfileCompiler.evaluate(policyfile_content, policyfile_expanded_path, ui: ui, chef_config: chef_config)
end

def policyfile_content

def policyfile_content
  @policyfile_content ||= IO.read(policyfile_expanded_path)
end

def policyfile_lock

def policyfile_lock
  @policyfile_lock ||= begin
    lock_data = FFI_Yajl::Parser.new.parse(policyfile_lock_content)
    PolicyfileLock.new(storage_config, ui: ui).build_from_lock_data(lock_data)
  end
end

def policyfile_lock_content

def policyfile_lock_content
  @policyfile_lock_content ||= IO.read(policyfile_lock_expanded_path)
end

def prepare_constraints

def prepare_constraints
  # Ensure we recompute policies from their (possibly updated) source
  Policyfile::LockApplier.new(policyfile_lock, policyfile_compiler)
    .with_unlocked_policies(:all)
    .apply!
end

def run

def run
  assert_policy_and_lock_present!
  prepare_constraints
  if policyfile_compiler.default_attributes != policyfile_lock.default_attributes
    policyfile_lock.default_attributes = policyfile_compiler.default_attributes
    @updated = true
  end
  if policyfile_compiler.override_attributes != policyfile_lock.override_attributes
    policyfile_lock.override_attributes = policyfile_compiler.override_attributes
    @updated = true
  end
  if updated_lock?
    with_file(policyfile_lock_expanded_path) do |f|
      f.print(FFI_Yajl::Encoder.encode(policyfile_lock.to_lock, pretty: true ))
    end
    ui.msg("Updated attributes in #{policyfile_lock_expanded_path}")
  else
    ui.msg("Attributes already up to date")
  end
rescue => error
  raise PolicyfileUpdateError.new("Failed to update Policyfile lock", error)
end

def updated_lock?

def updated_lock?
  @updated
end