Class: Rundoc::CodeCommand::Bash

Inherits:
Rundoc::CodeCommand show all
Defined in:
lib/rundoc/code_command/bash.rb,
lib/rundoc/code_command/bash/cd.rb

Direct Known Subclasses

Cd

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

Methods inherited from Rundoc::CodeCommand

#hidden?, #not_hidden?, #push

Constructor Details

#initialize(line) ⇒ Bash

line = “cd ..”“ line = ”pwd“ line = ”ls“



6
7
8
9
10
11
12
13
14
15
# File 'lib/rundoc/code_command/bash.rb', line 6

def initialize(line)
  @line     = line
  @contents = ""
  @delegate = case @line.split(' ').first.downcase
  when 'cd'
    Cd.new(@line)
  else
    false
  end
end

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