class Dip::InteractionTree

Attributes

entries[R]

Public Class Methods

new(entries) click to toggle source
# File lib/dip/interaction_tree.rb, line 10
def initialize(entries)
  @entries = entries
end

Public Instance Methods

find(name, *argv) click to toggle source
# File lib/dip/interaction_tree.rb, line 14
def find(name, *argv)
  entry = entries[name.to_sym]
  return unless entry

  commands = expand(name.to_s, entry)

  keys = [name, *argv]
  rest = []

  keys.size.times do
    if (command = commands[keys.join(" ")])
      return {command: command, argv: rest.reverse!}
    else
      rest << keys.pop
    end
  end

  nil
end
list() click to toggle source
# File lib/dip/interaction_tree.rb, line 34
def list
  entries.each_with_object({}) do |(name, entry), memo|
    expand(name.to_s, entry, tree: memo)
  end
end

Private Instance Methods

build_command(entry) click to toggle source
# File lib/dip/interaction_tree.rb, line 58
def build_command(entry)
  {
    description: entry[:description],
    service: entry[:service],
    command: entry[:command].to_s.strip,
    shell: entry.fetch(:shell, true),
    default_args: entry[:default_args].to_s.strip,
    environment: entry[:environment] || {},
    compose: {
      method: entry.dig(:compose, :method) || entry[:compose_method] || "run",
      run_options: compose_run_options(entry.dig(:compose, :run_options) || entry[:compose_run_options])
    }
  }
end
compose_run_options(value) click to toggle source
# File lib/dip/interaction_tree.rb, line 80
def compose_run_options(value)
  return [] unless value

  value.map do |o|
    o = o.start_with?("-") ? o : "--#{o}"
    o.shellsplit
  end.flatten
end
expand(name, entry, tree: {}) click to toggle source
# File lib/dip/interaction_tree.rb, line 44
def expand(name, entry, tree: {})
  cmd = build_command(entry)

  tree[name] = cmd

  entry[:subcommands]&.each do |sub_name, sub_entry|
    sub_command_defaults!(sub_entry)

    expand("#{name} #{sub_name}", entry.deep_merge(sub_entry), tree: tree)
  end

  tree
end
sub_command_defaults!(entry) click to toggle source
# File lib/dip/interaction_tree.rb, line 73
def sub_command_defaults!(entry)
  entry[:command] ||= nil
  entry[:default_args] ||= nil
  entry[:subcommands] ||= nil
  entry[:description] ||= nil
end