Class: Caliph::CommandLine

Inherits:
Object
  • Object
show all
Includes:
DefineOp
Defined in:
lib/caliph/command-line.rb,
lib/caliph/testing/record-commands.rb,
lib/caliph/testing/mock-command-line.rb

Direct Known Subclasses

CommandChain, ShellEscaped

Constant Summary collapse

@@commands =
[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefineOp

included

Constructor Details

#initialize(executable = nil, *options) {|_self| ... } ⇒ CommandLine

Returns a new instance of CommandLine.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
# File 'lib/caliph/command-line.rb', line 12

def initialize(executable = nil, *options)
  @output_stream = self.class.output_stream || $stderr
  @executable = executable.to_s unless executable.nil?
  @options = options
  @redirections = []
  @env = {}
  @verbose = false
  yield self if block_given?
end

Class Attribute Details

.command_recording_pathObject

Returns the value of attribute command_recording_path.



15
16
17
# File 'lib/caliph/testing/record-commands.rb', line 15

def command_recording_path
  @command_recording_path
end

.output_streamObject

Returns the value of attribute output_stream.



9
10
11
# File 'lib/caliph/command-line.rb', line 9

def output_stream
  @output_stream
end

Instance Attribute Details

#definition_watcherObject

Returns the value of attribute definition_watcher.



23
24
25
# File 'lib/caliph/command-line.rb', line 23

def definition_watcher
  @definition_watcher
end

#envObject Also known as: command_environment

Returns the value of attribute env.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def env
  @env
end

#executableObject

Returns the value of attribute executable.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def executable
  @executable
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def name
  @name
end

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def options
  @options
end

#output_streamObject

Returns the value of attribute output_stream.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def output_stream
  @output_stream
end

#redirectionsObject (readonly)

Returns the value of attribute redirections.



24
25
26
# File 'lib/caliph/command-line.rb', line 24

def redirections
  @redirections
end

#verboseObject

Returns the value of attribute verbose.



22
23
24
# File 'lib/caliph/command-line.rb', line 22

def verbose
  @verbose
end

Class Method Details

.emit_recordingObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/caliph/testing/record-commands.rb', line 21

def emit_recording
  io = $stderr
  if command_recording_path
    io = File.open(command_recording_path, "w")
  else
    io.puts "Set CALIPH_CMDREC to write to a path"
  end
  @@commands.each do |pair|
    io.puts "[/#{pair[0]}/, #{[pair[1].exit_code, pair[1].streams].inspect}]"
  end
end

.execute(*args) ⇒ Object



28
29
30
# File 'lib/caliph/testing/mock-command-line.rb', line 28

def self.execute(*args)
  fail "Command line executed in specs without 'expect_command' or 'expect_some_commands'"
end

Instance Method Details

#commandObject

The command as a string, including arguments and options



42
43
44
# File 'lib/caliph/command-line.rb', line 42

def command
  ([executable] + options_composition + @redirections).join(" ")
end

#copy_stream_to(from, to) ⇒ Object



67
68
69
# File 'lib/caliph/command-line.rb', line 67

def copy_stream_to(from, to)
  @redirections << "#{from}>&#{to}"
end

#executeObject

Deprecated.

(see: Shell)



106
107
108
# File 'lib/caliph/command-line.rb', line 106

def execute
  Caliph.new.execute(self)
end

#must_succeed!Object

Deprecated.


122
123
124
# File 'lib/caliph/command-line.rb', line 122

def must_succeed!
  run.must_succeed!
end

#options_compositionObject



54
55
56
# File 'lib/caliph/command-line.rb', line 54

def options_composition
  options
end

#original_executeObject

Deprecated.

(see: Shell)



6
7
8
# File 'lib/caliph/testing/record-commands.rb', line 6

def execute
  Caliph.new.execute(self)
end

#redirect_both(path) ⇒ Object



83
84
85
# File 'lib/caliph/command-line.rb', line 83

def redirect_both(path)
  redirect_stdout(path).redirect_stderr(path)
end

#redirect_from(path, stream) ⇒ Object



63
64
65
# File 'lib/caliph/command-line.rb', line 63

def redirect_from(path, stream)
  @redirections << "#{stream}<#{path}"
end

#redirect_stderr(path) ⇒ Object



75
76
77
# File 'lib/caliph/command-line.rb', line 75

def redirect_stderr(path)
  redirect_to(2, path)
end

#redirect_stdin(path) ⇒ Object



79
80
81
# File 'lib/caliph/command-line.rb', line 79

def redirect_stdin(path)
  redirect_from(path, 0)
end

#redirect_stdout(path) ⇒ Object



71
72
73
# File 'lib/caliph/command-line.rb', line 71

def redirect_stdout(path)
  redirect_to(1, path)
end

#redirect_to(stream, path) ⇒ Object



58
59
60
61
# File 'lib/caliph/command-line.rb', line 58

def redirect_to(stream, path)
  @redirections << "#{stream}>#{path}"
  self
end

#runObject

Deprecated.

(see: Shell)

:nocov:



89
90
91
# File 'lib/caliph/command-line.rb', line 89

def run
  Caliph.new.run(self)
end

#run_as_replacementObject Also known as: replace_us

Deprecated.

(see: Shell)



94
95
96
# File 'lib/caliph/command-line.rb', line 94

def run_as_replacement
  Caliph.new.run_as_replacement(self)
end

#run_detachedObject Also known as: spin_off

Deprecated.

(see: Shell)



100
101
102
# File 'lib/caliph/command-line.rb', line 100

def run_detached
  Caliph.new.run_detached(self)
end

#run_in_backgroundObject Also known as: background

Deprecated.

(see: Shell)



111
112
113
# File 'lib/caliph/command-line.rb', line 111

def run_in_background
  Caliph.new.run_in_background(self)
end

#set_env(name, value) ⇒ Object



32
33
34
35
# File 'lib/caliph/command-line.rb', line 32

def set_env(name, value)
  command_environment[name] = value
  return self
end

#string_formatObject

The command as a string, including arguments and options, plus prefixed environment variables.



48
49
50
51
52
# File 'lib/caliph/command-line.rb', line 48

def string_format
  (command_environment.map do |key, value|
    [key, value].join("=")
  end + [command]).join(" ")
end

#succeeds?Boolean

Deprecated.

Returns:

  • (Boolean)


117
118
119
# File 'lib/caliph/command-line.rb', line 117

def succeeds?
  run.succeeded?
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/caliph/command-line.rb', line 28

def valid?
  !@executable.nil?
end