Class: Textus::Application::Maintenance::ZoneMv::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/application/maintenance/zone_mv.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, caps:) ⇒ Impl

Returns a new instance of Impl.



15
16
17
18
19
# File 'lib/textus/application/maintenance/zone_mv.rb', line 15

def initialize(ctx:, caps:)
  @ctx      = ctx
  @manifest = caps.manifest
  @root     = caps.root
end

Instance Method Details

#call(from:, to:, dry_run: false) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/textus/application/maintenance/zone_mv.rb', line 21

def call(from:, to:, dry_run: false)
  raise UsageError.new("from and to required") if from.nil? || to.nil? || from.empty? || to.empty?
  raise UsageError.new("zone '#{from}' not declared") unless @manifest.data.zones.key?(from)

  dest_dir = File.join(@root, "zones", to)
  raise UsageError.new("destination 'zones/#{to}' already exists") if File.exist?(dest_dir)

  affected_keys = @manifest.data.entries.select { |e| e.zone == from }.map(&:key)

  steps  = [{ "op" => "rename_zone", "from" => from, "to" => to }]
  steps += affected_keys.map { |k| { "op" => "mv", "from" => k, "to" => "#{to}#{k[from.length..]}" } }

  plan = Plan.new(steps: steps, warnings: [])
  return plan if dry_run

  rewrite_manifest!(from, to)
  FileUtils.mv(File.join(@root, "zones", from), dest_dir)
  plan
end