Class: Textus::CLI::Verb::HookRun

Inherits:
Textus::CLI::Verb show all
Defined in:
lib/textus/cli/verb/hook_run.rb

Instance Attribute Summary

Attributes inherited from Textus::CLI::Verb

#positional

Instance Method Summary collapse

Methods inherited from Textus::CLI::Verb

#emit, inherited, #initialize, needs_store?, option, options

Constructor Details

This class inherits a constructor from Textus::CLI::Verb

Instance Method Details

#call(store) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/textus/cli/verb/hook_run.rb', line 9

def call(store)
  name = @raw_argv.shift
  raise UsageError.new("hook run requires a name") if name.nil?

  as_flag = nil
  args = {}
  @raw_argv.each do |tok|
    case tok
    when /\A--as=(.+)\z/         then as_flag = ::Regexp.last_match(1)
    when /\A--format=/           then next
    when /\A--([\w-]+)=(.*)\z/   then args[::Regexp.last_match(1)] = ::Regexp.last_match(2)
    else
      raise UsageError.new("unknown arg to 'hook run #{name}': #{tok}")
    end
  end

  role = Role.resolve(flag: as_flag, env: ENV, root: store.root)
  callable = store.registry.rpc_callable(:fetch, name)
  view = Store::View.new(store, writable: true, as: role)

  begin
    Timeout.timeout(Textus::Refresh::FETCH_TIMEOUT_SECONDS) do
      callable.call(config: {}, store: view, args: args)
    end
  rescue Timeout::Error
    raise UsageError.new(
      "hook run '#{name}' exceeded #{Textus::Refresh::FETCH_TIMEOUT_SECONDS}s timeout",
    )
  rescue Textus::Error
    raise
  rescue StandardError => e
    raise UsageError.new("hook run '#{name}' raised: #{e.class}: #{e.message}")
  end

  emit({ "action" => name, "ok" => true })
end

#parse(argv) ⇒ Object



5
6
7
# File 'lib/textus/cli/verb/hook_run.rb', line 5

def parse(argv)
  @raw_argv = argv
end