Class: Caliph::CommandLine

Inherits:
Object
  • Object
show all
Includes:
DefineOp
Defined in:
lib/caliph/command-line.rb,
lib/caliph/testing/record-commands.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(*args) {|_self| ... } ⇒ CommandLine

Returns a new instance of CommandLine.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(*args)
  @output_stream = self.class.output_stream || $stderr
  from(*args)
  @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.



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

def definition_watcher
  @definition_watcher
end

#envObject Also known as: command_environment

Returns the value of attribute env.



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

def env
  @env
end

#executableObject

Returns the value of attribute executable.



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

def executable
  @executable
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#output_streamObject

Returns the value of attribute output_stream.



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

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.



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

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

Instance Method Details

#commandObject

The command as a string, including arguments and options



46
47
48
# File 'lib/caliph/command-line.rb', line 46

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

#copy_stream_to(from, to) ⇒ Object



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

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

#executeObject

Deprecated.

(see: Shell)



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

def execute
  Caliph.new.execute(self)
end

#from(executable = nil, *options) ⇒ Object



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

def from(executable = nil, *options)
  @executable = executable.to_s unless executable.nil?
  @options = options
end

#must_succeed!Object

Deprecated.


126
127
128
# File 'lib/caliph/command-line.rb', line 126

def must_succeed!
  run.must_succeed!
end

#options_compositionObject



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

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



87
88
89
# File 'lib/caliph/command-line.rb', line 87

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

#redirect_from(path, stream) ⇒ Object



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

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

#redirect_stderr(path) ⇒ Object



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

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

#redirect_stdin(path) ⇒ Object



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

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

#redirect_stdout(path) ⇒ Object



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

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

#redirect_to(stream, path) ⇒ Object



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

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

#runObject

Deprecated.

(see: Shell)

:nocov:



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

def run
  Caliph.new.run(self)
end

#run_as_replacementObject Also known as: replace_us

Deprecated.

(see: Shell)



98
99
100
# File 'lib/caliph/command-line.rb', line 98

def run_as_replacement
  Caliph.new.run_as_replacement(self)
end

#run_detachedObject Also known as: spin_off

Deprecated.

(see: Shell)



104
105
106
# File 'lib/caliph/command-line.rb', line 104

def run_detached
  Caliph.new.run_detached(self)
end

#run_in_backgroundObject Also known as: background

Deprecated.

(see: Shell)



115
116
117
# File 'lib/caliph/command-line.rb', line 115

def run_in_background
  Caliph.new.run_in_background(self)
end

#set_env(name, value) ⇒ Object



36
37
38
39
# File 'lib/caliph/command-line.rb', line 36

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.



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

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

#succeeds?Boolean

Deprecated.

Returns:

  • (Boolean)


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

def succeeds?
  run.succeeded?
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !@executable.nil?
end