Class: Textus::CLI::Action

Inherits:
Verb
  • Object
show all
Includes:
DeprecatedAliasMixin
Defined in:
lib/textus/cli/action.rb

Instance Attribute Summary

Attributes included from DeprecatedAliasMixin

#deprecated_alias

Attributes inherited from Verb

#positional

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeprecatedAliasMixin

prepended

Methods inherited from Verb

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

Constructor Details

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

Class Method Details

.deprecated_nameObject



6
# File 'lib/textus/cli/action.rb', line 6

def self.deprecated_name = "action"

.replacement_pathObject



7
# File 'lib/textus/cli/action.rb', line 7

def self.replacement_path = "extension run"

Instance Method Details

#call(store) ⇒ Object

Raises:



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
45
46
47
48
# File 'lib/textus/cli/action.rb', line 13

def call(store)
  name = @raw_argv.shift
  raise UsageError.new("action 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 'action #{name}': #{tok}")
    end
  end

  role = Role.resolve(flag: as_flag, env: ENV, root: store.root)
  callable = store.registry.action(name)
  view = StoreView.new(store, writable: true, as: role)

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

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

#parse(argv) ⇒ Object



9
10
11
# File 'lib/textus/cli/action.rb', line 9

def parse(argv)
  @raw_argv = argv
end