Class: Subcheat::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/subcheat/runner.rb

Overview

the Runner handles this program’s input and output, and selects and invokes the commands to run.

Program flow

  1. The user invokes subcheat via the command line: subcheat foo

  2. The CLI creates a new runner.

  3. The Runner finds a custom Command by the name foo and invokes it.

  4. The Command returns a Subversion command to be executed.

  5. The Runner executes the command and exits.

If no custom command for the given subcommand name was found, it will be passed along to svn itself. This way, subcheat is a transparent wrapper around svn.

Testing

You can control where output is sent by overriding output, which defaults to $stdin. You can also prevent the actual execution of commands by setting perform_run to false.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Create a new runner by passing it all the arguments that the command-line client received.

The first argument is the name of the subcommand, and defaults to ‘help’. The rest are arguments passed to the subcommand.

Creating a new runner will immediately run the given subcommand.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/subcheat/runner.rb', line 51

def initialize(*args)
  # Default to $stdout
  self.class.output = $stdout if self.class.output.nil?

  # Gather subcommand and arguments
  subcommand, *arguments = args
  subcommand ||= 'help'
  arguments  ||= []

  begin
    self.class.run Command.on(subcommand).call(Svn.new(arguments))
  rescue NotAWorkingCopy
    # ...
  rescue NoSuchCommand
    self.class.run "svn #{subcommand} #{arguments.join(' ')}".strip
  rescue CommandException
    puts $!
  end
end

Class Attribute Details

.outputObject

Usually $stdin, but might be overridden



25
26
27
# File 'lib/subcheat/runner.rb', line 25

def output
  @output
end

.perform_runObject

Switch that controls whether commands are executed in the system, or simply sent to the output stream.



29
30
31
# File 'lib/subcheat/runner.rb', line 29

def perform_run
  @perform_run
end

Class Method Details

.run(command) ⇒ Object

Run a command in the system. Setting perform_run to false will make it just output the command, rather than executing it.



39
40
41
# File 'lib/subcheat/runner.rb', line 39

def run(command)
  (perform_run.nil? || perform_run) ? exec(command) : self.write(command)
end

.write(msg) ⇒ Object

Print something to the output stream



32
33
34
# File 'lib/subcheat/runner.rb', line 32

def write(msg)
  self.output.puts(msg)
end