Class: Textus::Protocol::Handlers::Ops::DataMv

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/protocol/handlers/ops/data_mv.rb

Defined Under Namespace

Classes: Plan

Instance Method Summary collapse

Instance Method Details

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

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/textus/protocol/handlers/ops/data_mv.rb', line 14

def call(from:, to:, ctx:, dry_run: false)
  raise Textus::UsageError.new("from and to required") unless from && to
  raise Textus::UsageError.new("data lane '#{from}' not declared") unless ctx.manifest.data.declared_lane_kinds.key?(from)

  dest_dir = ctx.layout.lane_path(to)
  raise Textus::UsageError.new("destination data/#{to} already exists") if File.exist?(dest_dir)

  affected_keys = ctx.manifest.data.entries.select { |e| e.lane == 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:, warnings: [])
  return plan.to_h if dry_run

  rewrite_manifest!(from:, to:, ctx:)
  FileUtils.mv(ctx.layout.lane_path(from), dest_dir)
  plan.to_h
end