Class: Textus::Maintenance::ZoneMv

Inherits:
Object
  • Object
show all
Extended by:
Contract::DSL
Defined in:
lib/textus/maintenance/zone_mv.rb

Overview

Rename a zone — rewrites the manifest’s zones[] entry, rewrites the ‘zone:` field on every entry under the old zone, and moves every file from zones/<old>/ to zones/<new>/.

Instance Method Summary collapse

Methods included from Contract::DSL

arg, contract, contract?, response, summary, surfaces, verb

Constructor Details

#initialize(container:, call:) ⇒ ZoneMv

Returns a new instance of ZoneMv.



19
20
21
22
23
24
# File 'lib/textus/maintenance/zone_mv.rb', line 19

def initialize(container:, call:)
  @container = container
  @call      = call
  @manifest  = container.manifest
  @root      = container.root
end

Instance Method Details

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

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/textus/maintenance/zone_mv.rb', line 26

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.declared_zone_kinds.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