Class: Rundoc::CodeCommand::Bash
- Inherits:
-
Rundoc::CodeCommand
- Object
- Rundoc::CodeCommand
- Rundoc::CodeCommand::Bash
- Defined in:
- lib/rundoc/code_command/bash.rb,
lib/rundoc/code_command/bash/cd.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Cd
Instance Attribute Summary
Attributes inherited from Rundoc::CodeCommand
#command, #contents, #keyword, #original_args, #render_command, #render_result
Instance Method Summary collapse
- #call(env = {}) ⇒ Object
-
#initialize(line) ⇒ Bash
constructor
line = “cd ..”“ line = ”pwd“ line = ”ls“.
-
#sanitize_escape_chars(input) ⇒ Object
markdown doesn’t understand bash color codes.
- #shell(cmd, stdin = nil) ⇒ Object
- #to_md(env = {}) ⇒ Object
Methods inherited from Rundoc::CodeCommand
Constructor Details
Instance Method Details
#call(env = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/rundoc/code_command/bash.rb', line 23 def call(env = {}) return @delegate.call(env) if @delegate shell(@line, @contents) end |
#sanitize_escape_chars(input) ⇒ Object
markdown doesn’t understand bash color codes
30 31 32 |
# File 'lib/rundoc/code_command/bash.rb', line 30 def sanitize_escape_chars(input) input.gsub(/\e\[(\d+)m/, '') end |
#shell(cmd, stdin = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rundoc/code_command/bash.rb', line 34 def shell(cmd, stdin = nil) msg = "Running: $ '#{cmd}'" msg << " with stdin: '#{stdin.inspect}'" if stdin && !stdin.empty? puts msg result = "" IO.popen("#{cmd} 2>&1", "w+") do |io| io << stdin if stdin io.close_write result = sanitize_escape_chars io.read end unless $?.success? raise "Command `#{@line}` exited with non zero status: #{result}" unless keyword.to_s.include?("fail") end return result end |
#to_md(env = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/rundoc/code_command/bash.rb', line 17 def to_md(env = {}) return @delegate.to_md(env) if @delegate "$ #{@line}" end |