pdk 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/README.md +3 -9
  4. data/exe/pdk +1 -1
  5. data/lib/pdk.rb +5 -4
  6. data/lib/pdk/cli.rb +62 -59
  7. data/lib/pdk/cli/errors.rb +1 -1
  8. data/lib/pdk/cli/exec.rb +154 -29
  9. data/lib/pdk/cli/input.rb +2 -2
  10. data/lib/pdk/cli/new.rb +12 -27
  11. data/lib/pdk/cli/new/class.rb +28 -41
  12. data/lib/pdk/cli/new/module.rb +30 -41
  13. data/lib/pdk/cli/test.rb +9 -20
  14. data/lib/pdk/cli/test/unit.rb +38 -0
  15. data/lib/pdk/cli/util/option_normalizer.rb +45 -19
  16. data/lib/pdk/cli/util/option_validator.rb +24 -20
  17. data/lib/pdk/cli/validate.rb +65 -65
  18. data/lib/pdk/generate.rb +5 -0
  19. data/lib/pdk/generators/module.rb +37 -33
  20. data/lib/pdk/generators/puppet_class.rb +1 -1
  21. data/lib/pdk/generators/puppet_object.rb +19 -20
  22. data/lib/pdk/logger.rb +1 -1
  23. data/lib/pdk/module/metadata.rb +35 -18
  24. data/lib/pdk/module/templatedir.rb +40 -33
  25. data/lib/pdk/report.rb +76 -19
  26. data/lib/pdk/report/event.rb +276 -0
  27. data/lib/pdk/template_file.rb +8 -6
  28. data/lib/pdk/tests/unit.rb +8 -3
  29. data/lib/pdk/util.rb +65 -0
  30. data/lib/pdk/util/bundler.rb +167 -0
  31. data/lib/pdk/util/version.rb +34 -0
  32. data/lib/pdk/validate.rb +3 -4
  33. data/lib/pdk/validators/base_validator.rb +60 -4
  34. data/lib/pdk/validators/metadata.rb +29 -0
  35. data/lib/pdk/validators/puppet/puppet_lint.rb +47 -0
  36. data/lib/pdk/validators/puppet/puppet_parser.rb +34 -0
  37. data/lib/pdk/validators/puppet_validator.rb +30 -0
  38. data/lib/pdk/validators/ruby/rubocop.rb +59 -0
  39. data/lib/pdk/validators/ruby_validator.rb +29 -0
  40. data/lib/pdk/version.rb +1 -1
  41. data/lib/puppet/util/windows.rb +14 -0
  42. data/lib/puppet/util/windows/api_types.rb +278 -0
  43. data/lib/puppet/util/windows/file.rb +488 -0
  44. data/lib/puppet/util/windows/string.rb +16 -0
  45. data/locales/de/pdk.po +263 -78
  46. data/locales/pdk.pot +224 -65
  47. metadata +60 -8
  48. data/lib/pdk/cli/tests/unit.rb +0 -52
  49. data/lib/pdk/validators/puppet_lint.rb +0 -17
  50. data/lib/pdk/validators/puppet_parser.rb +0 -17
  51. data/lib/pdk/validators/ruby_lint.rb +0 -17
data/lib/pdk/cli/input.rb CHANGED
@@ -10,10 +10,10 @@ module PDK
10
10
  #
11
11
  # @return [String] The value provided by the user (or the supplied
12
12
  # default value).
13
- def self.get(message, default=nil)
13
+ def self.get(message, default = nil)
14
14
  print message
15
15
  if default.nil?
16
- print " [(none)]"
16
+ print ' [(none)]'
17
17
  else
18
18
  print " [#{default}]"
19
19
  end
data/lib/pdk/cli/new.rb CHANGED
@@ -1,30 +1,15 @@
1
- require 'cri'
2
- require 'pdk/cli/new/module'
3
- require 'pdk/cli/new/class'
4
-
5
- module PDK
6
- module CLI
7
- module New
8
- def self.command
9
- @new ||= Cri::Command.new.tap do |cmd|
10
- cmd.modify do
11
- name 'new'
12
- usage _("new <type> [options]")
13
- summary _("create a new module, etc.")
14
- description _("Creates a new instance of <type> using the options relevant to that type of thing")
15
-
16
- # print the help text for the 'new' sub command if no type has been
17
- # provided.
18
- run do |opts, args, cmd|
19
- puts command.help
20
- exit 1
21
- end
22
- end
23
1
 
24
- cmd.add_command(PDK::CLI::New::Module.command)
25
- cmd.add_command(PDK::CLI::New::PuppetClass.command)
26
- end
27
- end
28
- end
2
+ module PDK::CLI
3
+ @new_cmd = @base_cmd.define_command do
4
+ name 'new'
5
+ usage _('new <type> [options]')
6
+ summary _('create a new module, etc.')
7
+ description _('Creates a new instance of <type> using the options relevant to that type of thing')
8
+ default_subcommand 'help'
29
9
  end
10
+
11
+ @new_cmd.add_command Cri::Command.new_basic_help
30
12
  end
13
+
14
+ require 'pdk/cli/new/class'
15
+ require 'pdk/cli/new/module'
@@ -1,45 +1,32 @@
1
- require 'cri'
2
- require 'pdk/cli/util/option_validator'
3
-
4
- require 'pdk/generators/module'
5
- require 'pdk/generators/puppet_class'
6
-
7
- module PDK
8
- module CLI
9
- module New
10
- class PuppetClass
11
- include PDK::CLI::Util
12
-
13
- def self.command
14
- @puppet_class ||= Cri::Command.define do
15
- name 'class'
16
- usage _("class [options] <class_name> [parameter[:type]] [parameter[:type]] ...")
17
- summary _("Create a new class named <class_name> using given options")
18
-
19
- option nil, 'template-url', _("Specifies the URL to the template to use when creating the module. Defaults to the template used to create the module, otherwise %{default}") % {:default => PDK::Generate::Module::DEFAULT_TEMPLATE}, argument: :required
20
-
21
- run do |opts, args, cmd|
22
- class_name = args[0]
23
- module_dir = Dir.pwd
24
-
25
- if class_name.nil? || class_name.empty?
26
- puts command.help
27
- exit 1
28
- end
29
-
30
- unless PDK::CLI::Util::OptionValidator.is_valid_class_name?(class_name)
31
- raise PDK::CLI::FatalError, _("'%{name}' is not a valid class name") % {name: class_name}
32
- end
33
-
34
- if args.length > 1
35
- opts[:params] = args[1..-1].map { |r| PDK::CLI::Util::OptionNormalizer.parameter_specification(r) }
36
- end
37
-
38
- PDK::Generate::PuppetClass.new(module_dir, class_name, opts).run
39
- end
40
- end
41
- end
1
+
2
+ module PDK::CLI
3
+ @new_class_cmd = @new_cmd.define_command do
4
+ name 'class'
5
+ usage _('class [options] <class_name> [parameter[:type]] [parameter[:type]] ...')
6
+ summary _('Create a new class named <class_name> using given options')
7
+
8
+ PDK::CLI.template_url_option(self)
9
+
10
+ run do |opts, args, _cmd|
11
+ require 'pdk/generators/puppet_class'
12
+
13
+ class_name = args[0]
14
+ module_dir = Dir.pwd
15
+
16
+ if class_name.nil? || class_name.empty?
17
+ puts command.help
18
+ exit 1
42
19
  end
20
+
21
+ unless Util::OptionValidator.valid_class_name?(class_name)
22
+ raise PDK::CLI::FatalError, _("'%{name}' is not a valid class name") % { name: class_name }
23
+ end
24
+
25
+ if args.length > 1
26
+ opts[:params] = args[1..-1].map { |r| Util::OptionNormalizer.parameter_specification(r) }
27
+ end
28
+
29
+ PDK::Generate::PuppetClass.new(module_dir, class_name, opts).run
43
30
  end
44
31
  end
45
32
  end
@@ -1,55 +1,44 @@
1
- require 'cri'
2
- require 'pdk/cli/util/option_validator'
3
1
 
4
- require 'pdk/generators/module'
2
+ module PDK::CLI
3
+ @new_module_cmd = @new_cmd.define_command do
4
+ name 'module'
5
+ usage _('module [options] <module_name> [target_dir]')
6
+ summary _('Create a new module named <module_name> using given options')
5
7
 
6
- module PDK
7
- module CLI
8
- module New
9
- class Module
10
- include PDK::CLI::Util
8
+ PDK::CLI.template_url_option(self)
11
9
 
12
- def self.command
13
- @module ||= Cri::Command.define do
14
- name 'module'
15
- usage _("module [options] <module_name> [target_dir]")
16
- summary _("Create a new module named <module_name> using given options")
10
+ option nil, 'license', _('Specifies the license this module is written under. ' \
11
+ "This should be a identifier from https://spdx.org/licenses/. Common values are 'Apache-2.0', 'MIT', or 'proprietary'."), argument: :required
17
12
 
18
- option nil, 'template-url', _("Specifies the URL to the template to use when creating the module. Defaults to %{default}") % {:default => PDK::Generate::Module::DEFAULT_TEMPLATE}, argument: :required
13
+ option nil, 'vcs', _("Specifies the version control driver. Valid values: 'git', 'none'. Default: 'git'."), argument: :required
19
14
 
20
- option nil, 'license', _("Specifies the license this module is written under. This should be a identifier from https://spdx.org/licenses/. Common values are 'Apache-2.0', 'MIT', or 'proprietary'."), argument: :required
15
+ flag nil, 'skip-interview', _('When specified, skips interactive querying of metadata.')
21
16
 
22
- option nil, 'vcs', _("Specifies the version control driver. Valid values: 'git', 'none'. Default: 'git'."), argument: :required
17
+ run do |opts, args, _cmd|
18
+ require 'pdk/generators/module'
23
19
 
24
- flag nil, 'skip-interview', _("When specified, skips interactive querying of metadata.")
20
+ module_name = args[0]
21
+ target_dir = args[1]
25
22
 
26
- run do |opts, args, cmd|
27
- module_name = args[0]
28
- target_dir = args[1]
29
-
30
- if module_name.nil? || module_name.empty?
31
- puts command.help
32
- exit 1
33
- end
23
+ if module_name.nil? || module_name.empty?
24
+ puts command.help
25
+ exit 1
26
+ end
34
27
 
35
- unless OptionValidator.is_valid_module_name?(module_name)
36
- error_msg = _(
37
- "'%{module_name}' is not a valid module name.\n" +
38
- "Module names must begin with a lowercase letter and can only include lowercase letters, digits, and underscores."
39
- ) % {:module_name => module_name}
40
- raise PDK::CLI::FatalError.new(error_msg)
41
- end
28
+ unless Util::OptionValidator.valid_module_name?(module_name)
29
+ error_msg = _(
30
+ "'%{module_name}' is not a valid module name.\n" \
31
+ 'Module names must begin with a lowercase letter and can only include lowercase letters, digits, and underscores.',
32
+ ) % { module_name: module_name }
33
+ raise PDK::CLI::FatalError, error_msg
34
+ end
42
35
 
43
- opts[:name] = module_name
44
- opts[:target_dir] = target_dir.nil? ? module_name : target_dir
45
- opts[:vcs] ||= 'git'
36
+ opts[:name] = module_name
37
+ opts[:target_dir] = target_dir.nil? ? module_name : target_dir
38
+ opts[:vcs] ||= 'git'
46
39
 
47
- PDK.logger.info(_("Creating new module: %{modname}") % {:modname => module_name})
48
- PDK::Generate::Module.invoke(opts)
49
- end
50
- end
51
- end
52
- end
40
+ PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
41
+ PDK::Generate::Module.invoke(opts)
53
42
  end
54
43
  end
55
44
  end
data/lib/pdk/cli/test.rb CHANGED
@@ -1,23 +1,12 @@
1
- require 'cri'
2
- require 'pdk/cli/tests/unit'
3
- require 'pdk/report'
4
1
 
5
-
6
- module PDK
7
- module CLI
8
- module Test
9
-
10
- def self.command
11
- @test ||= Cri::Command.new.tap do |cmd|
12
- cmd.modify do
13
- name 'test'
14
- usage _("test [type] [options]")
15
- summary _("Run tests.")
16
- end
17
-
18
- cmd.add_command(PDK::CLI::Test::Unit.command)
19
- end
20
- end
21
- end
2
+ module PDK::CLI
3
+ @test_cmd = @base_cmd.define_command do
4
+ name 'test'
5
+ usage _('test [type] [options]')
6
+ summary _('Run tests.')
7
+ default_subcommand 'help'
22
8
  end
9
+ @test_cmd.add_command Cri::Command.new_basic_help
23
10
  end
11
+
12
+ require 'pdk/cli/test/unit'
@@ -0,0 +1,38 @@
1
+
2
+ module PDK::CLI
3
+ @test_unit_cmd = @test_cmd.define_command do
4
+ name 'unit'
5
+ usage _('unit [options]')
6
+ summary _('Run unit tests.')
7
+
8
+ flag nil, :list, _('list all available unit tests and their descriptions')
9
+
10
+ option nil, :tests, _('a comma-separated list of tests to run'), argument: :required do |values|
11
+ OptionValidator.list(values)
12
+ end
13
+
14
+ option nil, :runner_options, _('options to pass through to the actual test-runner'), argument: :required
15
+
16
+ run do |opts, _args, _cmd|
17
+ require 'pdk/tests/unit'
18
+
19
+ report = nil
20
+
21
+ if opts[:list]
22
+ puts _('List of all available unit tests: (TODO)')
23
+ end
24
+
25
+ if opts[:tests]
26
+ tests = opts.fetch(:tests)
27
+ end
28
+
29
+ # Note: Reporting may be delegated to the validation tool itself.
30
+ if opts[:'report-file']
31
+ format = opts.fetch(:'report-format', PDK::Report.default_format)
32
+ report = Report.new(opts.fetch(:'report-file'), format)
33
+ end
34
+
35
+ PDK::Test::Unit.invoke(tests, report)
36
+ end
37
+ end
38
+ end
@@ -2,41 +2,67 @@ module PDK
2
2
  module CLI
3
3
  module Util
4
4
  class OptionNormalizer
5
- def self.comma_separated_list_to_array(list, options = {})
6
- raise _("Error: expected comma separated list") unless OptionValidator.is_comma_separated_list?(list)
5
+ def self.comma_separated_list_to_array(list, _options = {})
6
+ raise _('Error: expected comma separated list') unless OptionValidator.comma_separated_list?(list)
7
7
  list.split(',').compact
8
8
  end
9
9
 
10
- # Parse one or more format:target pairs.
11
- # @return [Array<Report>] An array of one or more Reports.
12
- def self.report_formats(formats, options = {})
13
- reports = []
14
- formats.each do |f|
15
- if f.include?(':')
16
- format, target = f.split(':')
17
- else
18
- format, target = f, PDK::Report.default_target
10
+ # Parse one or more format:target pairs into report format
11
+ # specifications.
12
+ #
13
+ # Each specification is a Hash with two values:
14
+ # :method => The name of the method to call on the PDK::Report object
15
+ # to render the report.
16
+ # :target => The target to write the report to. This can be either an
17
+ # IO object that implements #write, or a String filename
18
+ # that will be opened for writing.
19
+ #
20
+ # If the target given is "stdout" or "stderr", this will convert those
21
+ # strings into the appropriate IO object.
22
+ #
23
+ # @return [Array<Hash{Symbol=>Object}>] An array of one or more report
24
+ # format specifications
25
+ def self.report_formats(formats)
26
+ formats.map do |f|
27
+ format, target = f.split(':', 2)
28
+
29
+ begin
30
+ OptionValidator.enum(format, PDK::Report.formats)
31
+ rescue
32
+ raise PDK::CLI::FatalError, _("'%{name}' is not a valid report format (%{valid})") % {
33
+ name: format,
34
+ valid: PDK::Report.formats.join(', '),
35
+ }
19
36
  end
20
37
 
21
- reports << Report.new(target, format)
22
- end
38
+ case target
39
+ when 'stdout'
40
+ target = $stdout
41
+ when 'stderr'
42
+ target = $stderr
43
+ end
23
44
 
24
- reports
45
+ { method: "to_#{format}".to_sym, target: target }
46
+ end
25
47
  end
26
48
 
27
49
  def self.parameter_specification(value)
28
50
  param_name, param_type = value.split(':', 2)
29
51
  param_type = 'String' if param_type.nil?
30
52
 
31
- unless PDK::CLI::Util::OptionValidator.is_valid_param_name?(param_name)
32
- raise PDK::CLI::FatalError, _("'%{name}' is not a valid parameter name") % {name: param_name}
53
+ unless PDK::CLI::Util::OptionValidator.valid_param_name?(param_name)
54
+ raise PDK::CLI::FatalError, _("'%{name}' is not a valid parameter name") % {
55
+ name: param_name,
56
+ }
33
57
  end
34
58
 
35
- unless PDK::CLI::Util::OptionValidator.is_valid_data_type?(param_type)
36
- raise PDK::CLI::FatalError, _("'%{type}' is not a valid data type") % {type: param_type}
59
+ unless PDK::CLI::Util::OptionValidator.valid_data_type?(param_type)
60
+ raise PDK::CLI::FatalError, _("'%{type}' is not a valid data type") % {
61
+ type: param_type,
62
+ }
37
63
  end
38
64
 
39
- {name: param_name, type: param_type}
65
+ { name: param_name, type: param_type }
40
66
  end
41
67
  end
42
68
  end
@@ -2,16 +2,16 @@ module PDK
2
2
  module CLI
3
3
  module Util
4
4
  class OptionValidator
5
- def self.is_comma_separated_list?(list, options = {})
6
- list =~ /^[\w\-]+(?:,[\w\-]+)+$/ ? true : false
5
+ def self.comma_separated_list?(list, _options = {})
6
+ (list =~ %r{^[\w\-]+(?:,[\w\-]+)+$}) ? true : false
7
7
  end
8
8
 
9
- def self.enum(val, valid_entries, options = {})
9
+ def self.enum(val, valid_entries, _options = {})
10
10
  vals = val.is_a?(Array) ? val : [val]
11
- invalid_entries = vals.find_all { |e| !valid_entries.include?(e) }
11
+ invalid_entries = vals.reject { |e| valid_entries.include?(e) }
12
12
 
13
13
  unless invalid_entries.empty?
14
- raise _("Error: the following values are invalid: %{invalid_entries}") % {invalid_entries: invalid_entries}
14
+ raise _('Error: the following values are invalid: %{invalid_entries}') % { invalid_entries: invalid_entries }
15
15
  end
16
16
 
17
17
  val
@@ -19,31 +19,31 @@ module PDK
19
19
 
20
20
  # Validate the module name against the regular expression in the
21
21
  # documentation: https://docs.puppet.com/puppet/4.10/modules_fundamentals.html#allowed-module-names
22
- def self.is_valid_module_name?(string)
23
- !(string =~ /\A[a-z][a-z0-9_]*\Z/).nil?
22
+ def self.valid_module_name?(string)
23
+ !(string =~ %r{\A[a-z][a-z0-9_]*\Z}).nil?
24
24
  end
25
25
 
26
26
  # Validate a Puppet namespace against the regular expression in the
27
27
  # documentation: https://docs.puppet.com/puppet/4.10/lang_reserved.html#classes-and-defined-resource-types
28
- def self.is_valid_namespace?(string)
28
+ def self.valid_namespace?(string)
29
29
  return false if (string || '').split('::').last == 'init'
30
30
 
31
- !(string =~ /\A([a-z][a-z0-9_]*)(::[a-z][a-z0-9_]*)*\Z/).nil?
31
+ !(string =~ %r{\A([a-z][a-z0-9_]*)(::[a-z][a-z0-9_]*)*\Z}).nil?
32
32
  end
33
33
 
34
- singleton_class.send(:alias_method, :is_valid_class_name?, :is_valid_namespace?)
35
- singleton_class.send(:alias_method, :is_valid_defined_type_name?, :is_valid_namespace?)
34
+ singleton_class.send(:alias_method, :valid_class_name?, :valid_namespace?)
35
+ singleton_class.send(:alias_method, :valid_defined_type_name?, :valid_namespace?)
36
36
 
37
37
  # Validate that a class/defined type parameter matches the regular
38
38
  # expression in the documentation: https://docs.puppet.com/puppet/4.10/lang_reserved.html#parameters
39
39
  # The parameter should also not be a reserved word or overload
40
40
  # a metaparameter.
41
- def self.is_valid_param_name?(string)
42
- reserved_words = %w{trusted facts server_facts title name}.freeze
43
- metaparams = %w{alias audit before loglevel noop notify require schedule stage subscribe tag}.freeze
41
+ def self.valid_param_name?(string)
42
+ reserved_words = %w[trusted facts server_facts title name].freeze
43
+ metaparams = %w[alias audit before loglevel noop notify require schedule stage subscribe tag].freeze
44
44
  return false if reserved_words.include?(string) || metaparams.include?(string)
45
45
 
46
- !(string =~ /\A[a-z][a-zA-Z0-9_]*\Z/).nil?
46
+ !(string =~ %r{\A[a-z][a-zA-Z0-9_]*\Z}).nil?
47
47
  end
48
48
 
49
49
  # Naive validation of a data type declaration. Extracts all the bare
@@ -53,17 +53,21 @@ module PDK
53
53
  # but that shouldn't be a problem for the current feature set. This
54
54
  # should be replaced eventually by something better (or just call
55
55
  # Puppet::Pops::Types::TypesParser)
56
- def self.is_valid_data_type?(string)
57
- valid_types = %w{
56
+ def self.valid_data_type?(string)
57
+ valid_types = %w[
58
58
  String Integer Float Numeric Boolean Array Hash Regexp Undef
59
59
  Default Class Resource Scalar Collection Variant Data Pattern Enum
60
60
  Tuple Struct Optional Catalogentry Type Any Callable NotUndef
61
- }.freeze
61
+ ].freeze
62
62
 
63
- string.scan(/\b(([a-zA-Z]+)(,|\[|\]|\Z))/) do |result|
63
+ string.scan(%r{\b(([a-zA-Z0-9_]+)(,|\[|\]|\Z))}) do |result|
64
64
  type = result[1]
65
65
 
66
- return false unless valid_types.include?(type)
66
+ return false unless string =~ %r{\A[A-Z]}
67
+
68
+ unless valid_types.include?(type)
69
+ PDK.logger.warn(_("Non-standard data type '%{type}', make sure the type is available in your code, or dependencies") % { type: type })
70
+ end
67
71
  end
68
72
 
69
73
  true