Class: PuppetDebugger::InputResponders::Play

Inherits:
PuppetDebugger::InputResponderPlugin show all
Defined in:
lib/plugins/puppet-debugger/input_responders/play.rb

Constant Summary collapse

COMMAND_WORDS =
%w(play)
SUMMARY =
'Playback a file or URL as input.'
COMMAND_GROUP =
:editing

Instance Attribute Summary

Attributes inherited from PuppetDebugger::InputResponderPlugin

#debugger

Instance Method Summary collapse

Methods inherited from PuppetDebugger::InputResponderPlugin

command_completion, command_group, command_words, details, execute, #modules_paths, #puppet_debugger_lib_dir, summary

Instance Method Details

#convert_to_text(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 28

def convert_to_text(url)
  require 'uri'
  url_data = URI(url)
  case url_data.host
    when /^gist\.github*/
      url = url += '.txt' unless url_data.path =~ /raw/
      url
    when /^github.com/
      url.gsub('blob', 'raw') if url_data.path =~ /blob/
    when /^gist.github.com/
      url = url += '.txt' unless url_data.path =~ /raw/
      url
    when /^gitlab.com/
      if url_data.path =~ /snippets/
        url += '/raw' unless url_data.path =~ /raw/
        url
      else
        url.gsub('blob', 'raw')
      end
    else
      url
  end
end

#fetch_url_data(url) ⇒ Object

opens the url and reads the data



53
54
55
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 53

def fetch_url_data(url)
  open(url).read
end

#play_back(config = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 16

def play_back(config = {})
  if config[:play]
    if config[:play] =~ /^http/
      play_back_url(config[:play])
    elsif File.exist? config[:play]
      play_back_string(File.read(config[:play]))
    else config[:play]
    debugger.out_buffer.puts "puppet-debugger can't play #{config[:play]}'"
    end
  end
end

#play_back_string(str) ⇒ Object

plays back the string to the output stream echos the input and the produced output



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 69

def play_back_string(str)
  full_buffer = ''
  str.split("\n").each do |buf|
    begin
      full_buffer += buf
      # unless this is puppet code, otherwise skip repl keywords
      if PuppetDebugger::InputResponders::Commands.command_list_regex.match(buf)
        debugger.out_buffer.write('>> ')
      else
        debugger.parser.parse_string(full_buffer)
        debugger.out_buffer.write('>> ')
      end
    rescue Puppet::ParseErrorWithIssue => e
      if debugger.multiline_input?(e)
        full_buffer += "\n"
        next
      end
    end
    debugger.out_buffer.puts(full_buffer)
    debugger.handle_input(full_buffer)
    full_buffer = ''
  end
end

#play_back_url(url) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 57

def play_back_url(url)
  require 'open-uri'
  require 'net/http'
  converted_url = convert_to_text(url)
  str = fetch_url_data(converted_url)
  play_back_string(str)
rescue SocketError
  abort "puppet-debugger can't play `#{converted_url}'"
end

#run(args = []) ⇒ Object



9
10
11
12
13
14
# File 'lib/plugins/puppet-debugger/input_responders/play.rb', line 9

def run(args = [])
  config = {}
  config[:play] = args.first
  play_back(config)
  nil # we don't want to return anything
end