puppet-debugger 0.15.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.gitlab-ci.yml +21 -45
  3. data/.rubocop.yml +64 -233
  4. data/.rubocop_todo.yml +89 -147
  5. data/.ruby-version +1 -1
  6. data/.vscode/launch.json +15 -0
  7. data/CHANGELOG.md +34 -2
  8. data/Gemfile +7 -5
  9. data/README.md +29 -261
  10. data/Rakefile +11 -12
  11. data/bin/pdb +1 -1
  12. data/lib/awesome_print/ext/awesome_puppet.rb +22 -8
  13. data/lib/plugins/puppet-debugger/input_responders/benchmark.rb +5 -4
  14. data/lib/plugins/puppet-debugger/input_responders/classes.rb +14 -2
  15. data/lib/plugins/puppet-debugger/input_responders/classification.rb +4 -2
  16. data/lib/plugins/puppet-debugger/input_responders/commands.rb +18 -18
  17. data/lib/plugins/puppet-debugger/input_responders/datatypes.rb +22 -6
  18. data/lib/plugins/puppet-debugger/input_responders/environment.rb +4 -2
  19. data/lib/plugins/puppet-debugger/input_responders/exit.rb +4 -2
  20. data/lib/plugins/puppet-debugger/input_responders/facterdb_filter.rb +4 -2
  21. data/lib/plugins/puppet-debugger/input_responders/facts.rb +4 -2
  22. data/lib/plugins/puppet-debugger/input_responders/functions.rb +34 -32
  23. data/lib/plugins/puppet-debugger/input_responders/help.rb +4 -2
  24. data/lib/plugins/puppet-debugger/input_responders/krt.rb +4 -2
  25. data/lib/plugins/puppet-debugger/input_responders/play.rb +22 -24
  26. data/lib/plugins/puppet-debugger/input_responders/reset.rb +5 -3
  27. data/lib/plugins/puppet-debugger/input_responders/resources.rb +16 -7
  28. data/lib/plugins/puppet-debugger/input_responders/set.rb +34 -32
  29. data/lib/plugins/puppet-debugger/input_responders/stacktrace.rb +23 -0
  30. data/lib/plugins/puppet-debugger/input_responders/types.rb +6 -2
  31. data/lib/plugins/puppet-debugger/input_responders/vars.rb +8 -7
  32. data/lib/plugins/puppet-debugger/input_responders/whereami.rb +5 -3
  33. data/lib/puppet-debugger.rb +1 -45
  34. data/lib/puppet-debugger/cli.rb +120 -92
  35. data/lib/puppet-debugger/code/code_file.rb +13 -14
  36. data/lib/puppet-debugger/code/code_range.rb +5 -3
  37. data/lib/puppet-debugger/code/loc.rb +1 -1
  38. data/lib/puppet-debugger/debugger_code.rb +2 -0
  39. data/lib/puppet-debugger/hooks.rb +15 -16
  40. data/lib/puppet-debugger/input_responder_plugin.rb +54 -52
  41. data/lib/puppet-debugger/monkey_patches.rb +57 -0
  42. data/lib/puppet-debugger/plugin_test_helper.rb +9 -8
  43. data/lib/puppet-debugger/support.rb +27 -17
  44. data/lib/puppet-debugger/support/environment.rb +10 -3
  45. data/lib/puppet-debugger/support/errors.rb +25 -27
  46. data/lib/puppet-debugger/support/facts.rb +5 -5
  47. data/lib/puppet-debugger/support/node.rb +4 -7
  48. data/lib/puppet-debugger/support/scope.rb +29 -0
  49. data/lib/puppet-debugger/trollop.rb +38 -31
  50. data/lib/puppet-debugger/version.rb +1 -1
  51. data/lib/puppet/application/debugger.rb +151 -126
  52. data/output.json +1 -0
  53. data/puppet-debugger.gemspec +17 -15
  54. data/spec/awesome_print/ext/awesome_puppet_spec.rb +30 -30
  55. data/spec/fixtures/pe-xl-core-0.puppet.vm.json +1 -0
  56. data/spec/fixtures/sample_start_debugger.pp +3 -2
  57. data/spec/hooks_spec.rb +33 -35
  58. data/spec/input_responder_plugin_spec.rb +7 -6
  59. data/spec/input_responders/benchmark_spec.rb +3 -1
  60. data/spec/input_responders/classes_spec.rb +12 -13
  61. data/spec/input_responders/classification_spec.rb +4 -2
  62. data/spec/input_responders/commands_spec.rb +2 -0
  63. data/spec/input_responders/datatypes_spec.rb +8 -2
  64. data/spec/input_responders/environment_spec.rb +2 -0
  65. data/spec/input_responders/exit_spec.rb +9 -11
  66. data/spec/input_responders/facterdb_filter_spec.rb +2 -0
  67. data/spec/input_responders/facts_spec.rb +2 -0
  68. data/spec/input_responders/functions_spec.rb +30 -28
  69. data/spec/input_responders/help_spec.rb +5 -3
  70. data/spec/input_responders/krt_spec.rb +3 -1
  71. data/spec/input_responders/play_spec.rb +10 -20
  72. data/spec/input_responders/reset_spec.rb +2 -0
  73. data/spec/input_responders/resources_spec.rb +7 -1
  74. data/spec/input_responders/set_spec.rb +3 -1
  75. data/spec/input_responders/stacktrace_spec.rb +15 -0
  76. data/spec/input_responders/types_spec.rb +2 -0
  77. data/spec/input_responders/vars_spec.rb +4 -4
  78. data/spec/input_responders/whereami_spec.rb +2 -0
  79. data/spec/pdb_spec.rb +0 -9
  80. data/spec/puppet/application/debugger_spec.rb +35 -17
  81. data/spec/puppet_debugger_spec.rb +81 -83
  82. data/spec/remote_node_spec.rb +1 -5
  83. data/spec/spec_helper.rb +22 -18
  84. data/spec/support_spec.rb +3 -5
  85. data/test_matrix.rb +1 -1
  86. metadata +53 -19
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Help < InputResponderPlugin
5
- COMMAND_WORDS = %w(help)
7
+ COMMAND_WORDS = %w[help].freeze
6
8
  SUMMARY = 'Show the help screen with version information.'
7
9
  COMMAND_GROUP = :help
8
10
 
9
- def run(args = [])
11
+ def run(_args = [])
10
12
  PuppetDebugger::Cli.print_repl_desc
11
13
  end
12
14
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Krt < InputResponderPlugin
5
- COMMAND_WORDS = %w(krt)
7
+ COMMAND_WORDS = %w[krt].freeze
6
8
  SUMMARY = 'List all the known resource types.'
7
9
  COMMAND_GROUP = :scope
8
10
 
9
- def run(args = [])
11
+ def run(_args = [])
10
12
  debugger.known_resource_types.ai(sort_keys: true, indent: -1)
11
13
  end
12
14
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Play < InputResponderPlugin
5
- COMMAND_WORDS = %w(play)
7
+ COMMAND_WORDS = %w[play].freeze
6
8
  SUMMARY = 'Playback a file or URL as input.'
7
9
  COMMAND_GROUP = :editing
8
10
 
@@ -20,7 +22,7 @@ module PuppetDebugger
20
22
  elsif File.exist? config[:play]
21
23
  play_back_string(File.read(config[:play]))
22
24
  else config[:play]
23
- debugger.out_buffer.puts "puppet-debugger can't play #{config[:play]}'"
25
+ debugger.out_buffer.puts "puppet-debugger can't play #{config[:play]}'"
24
26
  end
25
27
  end
26
28
  end
@@ -29,29 +31,29 @@ module PuppetDebugger
29
31
  require 'uri'
30
32
  url_data = URI(url)
31
33
  case url_data.host
32
- when /^gist\.github*/
33
- url = url += '.txt' unless url_data.path =~ /raw/
34
- url
35
- when /^github.com/
36
- url.gsub('blob', 'raw') if url_data.path =~ /blob/
37
- when /^gist.github.com/
38
- url = url += '.txt' unless url_data.path =~ /raw/
34
+ when /^gist\.github*/
35
+ url = url += '.txt' unless url_data.path =~ /raw/
36
+ url
37
+ when /^github.com/
38
+ url.gsub('blob', 'raw') if url_data.path =~ /blob/
39
+ when /^gist.github.com/
40
+ url = url += '.txt' unless url_data.path =~ /raw/
41
+ url
42
+ when /^gitlab.com/
43
+ if url_data.path =~ /snippets/
44
+ url += '/raw' unless url_data.path =~ /raw/
39
45
  url
40
- when /^gitlab.com/
41
- if url_data.path =~ /snippets/
42
- url += '/raw' unless url_data.path =~ /raw/
43
- url
44
- else
45
- url.gsub('blob', 'raw')
46
- end
47
46
  else
48
- url
47
+ url.gsub('blob', 'raw')
48
+ end
49
+ else
50
+ url
49
51
  end
50
52
  end
51
53
 
52
54
  # opens the url and reads the data
53
55
  def fetch_url_data(url)
54
- open(url).read
56
+ URI.open(url).read # open(url).read
55
57
  end
56
58
 
57
59
  def play_back_url(url)
@@ -72,12 +74,8 @@ module PuppetDebugger
72
74
  begin
73
75
  full_buffer += buf
74
76
  # unless this is puppet code, otherwise skip repl keywords
75
- if PuppetDebugger::InputResponders::Commands.command_list_regex.match(buf)
76
- debugger.out_buffer.write('>> ')
77
- else
78
- debugger.parser.parse_string(full_buffer)
79
- debugger.out_buffer.write('>> ')
80
- end
77
+ debugger.parser.parse_string(full_buffer) unless PuppetDebugger::InputResponders::Commands.command_list_regex.match(buf)
78
+ debugger.out_buffer.write('>> ')
81
79
  rescue Puppet::ParseErrorWithIssue => e
82
80
  if debugger.multiline_input?(e)
83
81
  full_buffer += "\n"
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Reset < InputResponderPlugin
5
- COMMAND_WORDS = %w(reset)
7
+ COMMAND_WORDS = %w[reset].freeze
6
8
  SUMMARY = 'Reset the debugger to a clean state.'
7
9
  COMMAND_GROUP = :context
8
10
 
9
- def run(args = [])
11
+ def run(_args = [])
10
12
  debugger.set_scope(nil)
11
13
  debugger.set_remote_node_name(nil)
12
14
  debugger.set_node(nil)
13
15
  debugger.set_facts(nil)
14
16
  debugger.set_environment(nil)
15
17
  debugger.set_compiler(nil)
16
- #debugger.handle_input(":set loglevel #{debugger.log_level}")
18
+ # debugger.handle_input(":set loglevel #{debugger.log_level}")
17
19
  nil
18
20
  end
19
21
  end
@@ -1,20 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Resources < InputResponderPlugin
5
- COMMAND_WORDS = %w(resources)
7
+ COMMAND_WORDS = %w[resources].freeze
6
8
  SUMMARY = 'List all the resources current in the catalog.'
7
9
  COMMAND_GROUP = :scope
8
10
 
9
11
  def run(args = [])
10
- res = debugger.scope.compiler.catalog.resources.map do |res|
12
+ filter = args
13
+ resources = find_resources(debugger.catalog.resources, filter)
14
+ modified = resources.map do |res|
11
15
  res.to_s.gsub(/\[/, "['").gsub(/\]/, "']") # ensure the title has quotes
12
16
  end
13
- if !args.first.nil?
14
- res[args.first.to_i].ai
15
- else
16
- output = "Resources not shown in any specific order\n".warning
17
- output += res.ai
17
+ output = "Resources not shown in any specific order\n".warning
18
+ output + modified.ai
19
+ end
20
+
21
+ def find_resources(resources, filter = [])
22
+ return resources if filter.nil? || filter.empty?
23
+
24
+ filter_string = filter.join(' ').downcase
25
+ resources.find_all do |resource|
26
+ resource.name.to_s.downcase.include?(filter_string) || resource.type.to_s.downcase.include?(filter_string)
18
27
  end
19
28
  end
20
29
  end
@@ -1,28 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Set < InputResponderPlugin
5
- COMMAND_WORDS = %w(set :set)
7
+ COMMAND_WORDS = %w[set :set].freeze
6
8
  SUMMARY = 'Set the a puppet debugger config'
7
9
  COMMAND_GROUP = :scope
8
- KEYWORDS = %w(node loglevel)
9
- LOGLEVELS = %w(debug info)
10
+ KEYWORDS = %w[node loglevel].freeze
11
+ LOGLEVELS = %w[debug info].freeze
10
12
 
11
13
  def self.command_completion(buffer_words)
12
14
  next_word = buffer_words.shift
13
15
  case next_word
14
- when 'loglevel'
15
- if buffer_words.count > 0
16
- LOGLEVELS.grep(/^#{Regexp.escape(buffer_words.first)}/)
17
- else
18
- LOGLEVELS
19
- end
20
- when 'debug', 'info','node'
21
- []
22
- when nil
23
- %w(node loglevel)
16
+ when 'loglevel'
17
+ if buffer_words.count.positive?
18
+ LOGLEVELS.grep(/^#{Regexp.escape(buffer_words.first)}/)
24
19
  else
25
- KEYWORDS.grep(/^#{Regexp.escape(next_word)}/)
20
+ LOGLEVELS
21
+ end
22
+ when 'debug', 'info', 'node'
23
+ []
24
+ when nil
25
+ %w[node loglevel]
26
+ else
27
+ KEYWORDS.grep(/^#{Regexp.escape(next_word)}/)
26
28
  end
27
29
  end
28
30
 
@@ -37,24 +39,24 @@ module PuppetDebugger
37
39
  # args = input.split(' ')
38
40
  # args.shift # throw away the set
39
41
  case input.shift
40
- when /node/
41
- if name = input.shift
42
- output = "Resetting to use node #{name}"
43
- debugger.set_scope(nil)
44
- debugger.set_node(nil)
45
- debugger.set_facts(nil)
46
- debugger.set_environment(nil)
47
- debugger.set_compiler(nil)
48
- set_log_level(debugger.log_level)
49
- debugger.set_remote_node_name(name)
50
- else
51
- debugger.out_buffer.puts 'Must supply a valid node name'
52
- end
53
- when /loglevel/
54
- if level = input.shift
55
- set_log_level(level)
56
- output = "loglevel #{Puppet::Util::Log.level} is set"
57
- end
42
+ when /node/
43
+ if name = input.shift
44
+ output = "Resetting to use node #{name}"
45
+ debugger.set_scope(nil)
46
+ debugger.set_node(nil)
47
+ debugger.set_facts(nil)
48
+ debugger.set_environment(nil)
49
+ debugger.set_compiler(nil)
50
+ set_log_level(debugger.log_level)
51
+ debugger.set_remote_node_name(name)
52
+ else
53
+ debugger.out_buffer.puts 'Must supply a valid node name'
54
+ end
55
+ when /loglevel/
56
+ if level = input.shift
57
+ set_log_level(level)
58
+ output = "loglevel #{Puppet::Util::Log.level} is set"
59
+ end
58
60
  end
59
61
  output
60
62
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'puppet-debugger/input_responder_plugin'
4
+ module PuppetDebugger
5
+ module InputResponders
6
+ class Stacktrace < InputResponderPlugin
7
+ COMMAND_WORDS = %w[stacktrace].freeze
8
+ SUMMARY = 'Show the stacktrace for how we got here'
9
+ COMMAND_GROUP = :tools
10
+
11
+ # @return [Array]- returns a array of pp files that are involved in the stacktrace
12
+ def run(_args = [])
13
+ s = stacktrace
14
+ s.empty? ? 'stacktrace not available'.warning : s.ai
15
+ end
16
+
17
+ # @return [Array] - an array of files
18
+ def stacktrace
19
+ Puppet::Pops::PuppetStack.stacktrace.find_all { |line| !line.include?('unknown') }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Types < InputResponderPlugin
5
- COMMAND_WORDS = %w(types)
7
+ COMMAND_WORDS = %w[types].freeze
6
8
  SUMMARY = 'List all the types available in the environment.'
7
9
  COMMAND_GROUP = :environment
8
10
 
9
11
  # @return - returns a list of types available to the environment
10
12
  # if a error occurs we we run the types function again
11
- def run(args = [])
13
+ def run(_args = [])
12
14
  types
13
15
  end
14
16
 
@@ -19,6 +21,7 @@ module PuppetDebugger
19
21
  Puppet::Type.loadall
20
22
  Puppet::Type.eachtype do |t|
21
23
  next if t.name == :component
24
+
22
25
  loaded_types << t.name.to_s
23
26
  end
24
27
  loaded_types.ai
@@ -27,6 +30,7 @@ module PuppetDebugger
27
30
  Puppet.info(e.message)
28
31
  # prevent more than two calls and recursive loop
29
32
  return if caller_locations(1, 10).find_all { |f| f.label == 'types' }.count > 2
33
+
30
34
  types
31
35
  end
32
36
  end
@@ -1,26 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Vars < InputResponderPlugin
5
- COMMAND_WORDS = %w(vars ls)
7
+ COMMAND_WORDS = %w[vars ls].freeze
6
8
  SUMMARY = 'List all the variables in the current scopes.'
7
9
  COMMAND_GROUP = :scope
8
10
 
9
11
  def run(args = [])
10
12
  filter = args
11
13
  unless filter.empty?
12
- parameters = resource_parameters(debugger.scope.catalog.resources, filter)
14
+ parameters = resource_parameters(debugger.catalog.resources, filter)
13
15
  return parameters.ai(sort_keys: true, indent: -1)
14
16
  end
15
17
  # remove duplicate variables that are also in the facts hash
16
18
  variables = debugger.scope.to_hash.delete_if { |key, _value| debugger.node.facts.values.key?(key) }
17
19
  variables['facts'] = 'removed by the puppet-debugger' if variables.key?('facts')
18
20
  output = 'Facts were removed for easier viewing'.ai + "\n"
19
- output += variables.ai(sort_keys: true, indent: -1)
21
+ output + variables.ai(sort_keys: true, indent: -1)
20
22
  end
21
23
 
22
24
  def resource_parameters(resources, filter = [])
23
- find_resources(resources, filter).each_with_object({}) do | resource, acc|
25
+ find_resources(resources, filter).each_with_object({}) do |resource, acc|
24
26
  name = "#{resource.type}[#{resource.name}]"
25
27
  acc[name] = parameters_to_h(resource)
26
28
  acc
@@ -28,9 +30,9 @@ module PuppetDebugger
28
30
  end
29
31
 
30
32
  def parameters_to_h(resource)
31
- resource.parameters.each_with_object({}) do | param, params |
33
+ resource.parameters.each_with_object({}) do |param, params|
32
34
  name = param.first.to_s
33
- params[name] = param.last.value
35
+ params[name] = param.last.respond_to?(:value) ? param.last.value : param.last
34
36
  params
35
37
  end
36
38
  end
@@ -41,7 +43,6 @@ module PuppetDebugger
41
43
  resource.name.to_s.downcase.include?(filter_string) || resource.type.to_s.downcase.include?(filter_string)
42
44
  end
43
45
  end
44
-
45
46
  end
46
47
  end
47
48
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-debugger/input_responder_plugin'
2
4
  module PuppetDebugger
3
5
  module InputResponders
4
6
  class Whereami < InputResponderPlugin
5
- COMMAND_WORDS = %w(whereami)
7
+ COMMAND_WORDS = %w[whereami].freeze
6
8
  SUMMARY = 'Show code surrounding the current context.'
7
9
  COMMAND_GROUP = :context
8
10
 
@@ -21,8 +23,8 @@ module PuppetDebugger
21
23
  else
22
24
  code = DebuggerCode.from_file(file, :puppet)
23
25
  end
24
- return code.with_marker(line_num).around(line_num, num_lines)
25
- .with_line_numbers.with_indentation(5).with_file_reference.to_s
26
+ code.with_marker(line_num).around(line_num, num_lines)
27
+ .with_line_numbers.with_indentation(5).with_file_reference.to_s
26
28
  end
27
29
  end
28
30
 
@@ -9,51 +9,7 @@ require 'puppet/util/log'
9
9
  require 'puppet-debugger/debugger_code'
10
10
  require 'puppet-debugger/support/errors'
11
11
  require 'plugins/puppet-debugger/input_responders/commands'
12
-
13
-
14
- # monkey patch in some color effects string methods
15
- class String
16
- def red
17
- "\033[31m#{self}\033[0m"
18
- end
19
-
20
- def bold
21
- "\033[1m#{self}\033[22m"
22
- end
23
-
24
- def black
25
- "\033[30m#{self}\033[0m"
26
- end
27
-
28
- def green
29
- "\033[32m#{self}\033[0m"
30
- end
31
-
32
- def cyan
33
- "\033[36m#{self}\033[0m"
34
- end
35
-
36
- def yellow
37
- "\033[33m#{self}\033[0m"
38
- end
39
-
40
- def warning
41
- yellow
42
- end
43
-
44
- def fatal
45
- red
46
- end
47
-
48
- def info
49
- green
50
- end
51
-
52
- def camel_case
53
- return self if self !~ /_/ && self =~ /[A-Z]+.*/
54
- split('_').map(&:capitalize).join
55
- end
56
- end
12
+ require 'puppet-debugger/monkey_patches'
57
13
 
58
14
  Puppet::Util::Log.newdesttype :buffer do
59
15
  require 'puppet/util/colors'