Class: Textus::CLI::Verb

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/cli/verb.rb,
lib/textus/cli/verb/mv.rb,
lib/textus/cli/verb/get.rb,
lib/textus/cli/verb/put.rb,
lib/textus/cli/verb/uid.rb,
lib/textus/cli/verb/deps.rb,
lib/textus/cli/verb/init.rb,
lib/textus/cli/verb/list.rb,
lib/textus/cli/verb/build.rb,
lib/textus/cli/verb/hooks.rb,
lib/textus/cli/verb/intro.rb,
lib/textus/cli/verb/rdeps.rb,
lib/textus/cli/verb/stale.rb,
lib/textus/cli/verb/where.rb,
lib/textus/cli/verb/accept.rb,
lib/textus/cli/verb/delete.rb,
lib/textus/cli/verb/doctor.rb,
lib/textus/cli/verb/schema.rb,
lib/textus/cli/verb/refresh.rb,
lib/textus/cli/verb/hook_run.rb,
lib/textus/cli/verb/published.rb,
lib/textus/cli/verb/schema_diff.rb,
lib/textus/cli/verb/schema_init.rb,
lib/textus/cli/verb/migrate_keys.rb,
lib/textus/cli/verb/schema_migrate.rb

Overview

Subclasses must implement #call(store) and return an integer exit code. Use #emit(obj) for normal JSON output (returns 0). Subclasses that don’t need a Textus store (e.g. Init) override ‘.needs_store?` to return false; dispatch will pass nil instead.

Defined Under Namespace

Classes: Accept, Build, Delete, Deps, Doctor, Get, HookRun, Hooks, Init, Intro, List, MigrateKeys, Mv, Published, Put, Rdeps, Refresh, Schema, SchemaDiff, SchemaInit, SchemaMigrate, Stale, Uid, Where

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin:, stdout:, stderr:, cwd: nil) ⇒ Verb

Returns a new instance of Verb.



31
32
33
34
35
36
# File 'lib/textus/cli/verb.rb', line 31

def initialize(stdin:, stdout:, stderr:, cwd: nil)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @cwd = cwd
end

Instance Attribute Details

#positionalObject (readonly)

Returns the value of attribute positional.



51
52
53
# File 'lib/textus/cli/verb.rb', line 51

def positional
  @positional
end

Class Method Details

.inherited(subclass) ⇒ Object



25
26
27
28
# File 'lib/textus/cli/verb.rb', line 25

def inherited(subclass)
  super
  subclass.instance_variable_set(:@options, [])
end

.needs_store?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/textus/cli/verb.rb', line 21

def needs_store?
  true
end

.option(name, optspec) ⇒ Object



12
13
14
15
# File 'lib/textus/cli/verb.rb', line 12

def option(name, optspec)
  options << [name, optspec]
  attr_accessor(name)
end

.optionsObject



17
18
19
# File 'lib/textus/cli/verb.rb', line 17

def options
  @options ||= []
end

Instance Method Details

#emit(obj, exit_code: 0) ⇒ Object

Hashes get “protocol” => PROTOCOL prepended unless they already carry one (Store envelopes do). Caller’s value wins on collision.



55
56
57
58
59
# File 'lib/textus/cli/verb.rb', line 55

def emit(obj, exit_code: 0)
  payload = obj.is_a?(Hash) ? { "protocol" => PROTOCOL }.merge(obj) : obj
  @stdout.puts(JSON.generate(payload))
  exit_code
end

#parse(argv) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/textus/cli/verb.rb', line 38

def parse(argv)
  fmt = "json"
  OptionParser.new do |o|
    self.class.options.each do |name, optspec|
      o.on(optspec) { |v| public_send(:"#{name}=", v) }
    end
    o.on("--format=FMT") { |v| fmt = v }
  end.permute!(argv)
  raise UsageError.new("only --format=json is supported in v1") unless fmt == "json"

  @positional = argv.dup
end