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

#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.



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

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



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

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

#copy_stream_to(from, to) ⇒ Object



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

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

#executeObject

Deprecated.

(see: Shell)



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

def execute
  Caliph.new.execute(self)
end

#must_succeed!Object

Deprecated.


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

def must_succeed!
  run.must_succeed!
end

#options_compositionObject



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

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



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

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

#redirect_from(path, stream) ⇒ Object

Waits for the process to complete. If this takes longer that consume_timeout,



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

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

#redirect_stderr(path) ⇒ Object



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

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

#redirect_stdin(path) ⇒ Object



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

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

#redirect_stdout(path) ⇒ Object



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

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

#redirect_to(stream, path) ⇒ Object



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

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

#runObject

Deprecated.

(see: Shell)

:nocov:



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

def run
  Caliph.new.run(self)
end

#run_as_replacementObject Also known as: replace_us

Deprecated.

(see: Shell)



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

def run_as_replacement
  Caliph.new.run_as_replacement(self)
end

#run_detachedObject Also known as: spin_off

Deprecated.

(see: Shell)



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

def run_detached
  Caliph.new.run_detached(self)
end

#run_in_backgroundObject Also known as: background

Deprecated.

(see: Shell)



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

def run_in_background
  Caliph.new.run_in_background(self)
end

#set_env(name, value) ⇒ Object



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

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.



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

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

#succeeds?Boolean

Deprecated.

Returns:

  • (Boolean)


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

def succeeds?
  run.succeeded?
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !@executable.nil?
end