Class: Whodunit::Chronicles::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/whodunit/chronicles/cli.rb

Overview

Small operational command-line interface for ledger lifecycle tasks.

The CLI owns book preparation, migration-like operations, index creation, verification, and status checks. The runtime chronicler only appends entries into a ledger it has been handed.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv:, out:, err:) ⇒ CLI

Create a CLI instance.

Parameters:

  • argv (Array<String>)

    command-line arguments

  • out (#puts)

    output stream

  • err (#puts)

    error stream



31
32
33
34
35
# File 'lib/whodunit/chronicles/cli.rb', line 31

def initialize(argv:, out:, err:)
  @argv = argv.dup
  @out = out
  @err = err
end

Class Method Details

.run(argv, out: $stdout, err: $stderr) ⇒ Integer

Run a command with argv-style arguments.

Parameters:

  • argv (Array<String>)

    command-line arguments

  • out (#puts) (defaults to: $stdout)

    output stream

  • err (#puts) (defaults to: $stderr)

    error stream

Returns:

  • (Integer)

    process-style exit code



22
23
24
# File 'lib/whodunit/chronicles/cli.rb', line 22

def self.run(argv, out: $stdout, err: $stderr)
  new(argv: argv, out: out, err: err).run
end

Instance Method Details

#runInteger

Execute the requested command.

Returns:

  • (Integer)

    process-style exit code



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/whodunit/chronicles/cli.rb', line 40

def run
  return usage(0) if @argv.empty? || @argv.first == 'help'

  namespace = @argv.shift
  command = @argv.shift
  config_path = @argv.shift
  options = @argv.dup
  return usage(1) unless namespace == 'ledger' && command && config_path

  ledger = LedgerFactory.build(load_config(config_path))
  execute_ledger_command(ledger, command, options)
rescue StandardError => e
  @err.puts(e.message)
  1
end