pdk 1.15.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +168 -10
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +28 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/bolt.rb +19 -0
  7. data/lib/pdk/cli.rb +7 -6
  8. data/lib/pdk/cli/bundle.rb +5 -1
  9. data/lib/pdk/cli/config.rb +3 -1
  10. data/lib/pdk/cli/config/get.rb +3 -1
  11. data/lib/pdk/cli/convert.rb +7 -9
  12. data/lib/pdk/cli/exec/command.rb +45 -4
  13. data/lib/pdk/cli/exec_group.rb +78 -43
  14. data/lib/pdk/cli/get.rb +20 -0
  15. data/lib/pdk/cli/get/config.rb +24 -0
  16. data/lib/pdk/cli/new.rb +2 -0
  17. data/lib/pdk/cli/new/class.rb +2 -2
  18. data/lib/pdk/cli/new/defined_type.rb +2 -2
  19. data/lib/pdk/cli/new/fact.rb +29 -0
  20. data/lib/pdk/cli/new/function.rb +29 -0
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release/publish.rb +11 -1
  26. data/lib/pdk/cli/remove.rb +20 -0
  27. data/lib/pdk/cli/remove/config.rb +80 -0
  28. data/lib/pdk/cli/set.rb +20 -0
  29. data/lib/pdk/cli/set/config.rb +119 -0
  30. data/lib/pdk/cli/update.rb +6 -8
  31. data/lib/pdk/cli/util.rb +7 -3
  32. data/lib/pdk/cli/util/option_validator.rb +6 -0
  33. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  34. data/lib/pdk/cli/validate.rb +26 -44
  35. data/lib/pdk/config.rb +264 -7
  36. data/lib/pdk/config/ini_file.rb +183 -0
  37. data/lib/pdk/config/ini_file_setting.rb +39 -0
  38. data/lib/pdk/config/namespace.rb +25 -5
  39. data/lib/pdk/config/setting.rb +3 -2
  40. data/lib/pdk/context.rb +99 -0
  41. data/lib/pdk/context/control_repo.rb +60 -0
  42. data/lib/pdk/context/module.rb +28 -0
  43. data/lib/pdk/context/none.rb +22 -0
  44. data/lib/pdk/control_repo.rb +90 -0
  45. data/lib/pdk/generate/defined_type.rb +25 -32
  46. data/lib/pdk/generate/fact.rb +25 -0
  47. data/lib/pdk/generate/function.rb +48 -0
  48. data/lib/pdk/generate/module.rb +33 -20
  49. data/lib/pdk/generate/provider.rb +15 -64
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -188
  52. data/lib/pdk/generate/task.rb +28 -46
  53. data/lib/pdk/generate/transport.rb +20 -74
  54. data/lib/pdk/module.rb +1 -1
  55. data/lib/pdk/module/build.rb +23 -0
  56. data/lib/pdk/module/convert.rb +58 -34
  57. data/lib/pdk/module/metadata.rb +6 -2
  58. data/lib/pdk/module/release.rb +3 -9
  59. data/lib/pdk/module/update.rb +8 -12
  60. data/lib/pdk/module/update_manager.rb +7 -0
  61. data/lib/pdk/report.rb +3 -3
  62. data/lib/pdk/report/event.rb +8 -2
  63. data/lib/pdk/template.rb +59 -0
  64. data/lib/pdk/template/fetcher.rb +98 -0
  65. data/lib/pdk/template/fetcher/git.rb +85 -0
  66. data/lib/pdk/template/fetcher/local.rb +28 -0
  67. data/lib/pdk/template/renderer.rb +96 -0
  68. data/lib/pdk/template/renderer/v1.rb +25 -0
  69. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  70. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  71. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  72. data/lib/pdk/template/template_dir.rb +67 -0
  73. data/lib/pdk/tests/unit.rb +8 -1
  74. data/lib/pdk/util.rb +38 -39
  75. data/lib/pdk/util/bundler.rb +2 -1
  76. data/lib/pdk/util/changelog_generator.rb +11 -2
  77. data/lib/pdk/util/env.rb +28 -11
  78. data/lib/pdk/util/json_finder.rb +84 -0
  79. data/lib/pdk/util/puppet_strings.rb +3 -3
  80. data/lib/pdk/util/puppet_version.rb +2 -2
  81. data/lib/pdk/util/ruby_version.rb +11 -4
  82. data/lib/pdk/util/template_uri.rb +17 -14
  83. data/lib/pdk/util/vendored_file.rb +1 -2
  84. data/lib/pdk/validate.rb +79 -25
  85. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  86. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  87. data/lib/pdk/validate/external_command_validator.rb +208 -0
  88. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  89. data/lib/pdk/validate/invokable_validator.rb +220 -0
  90. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  91. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  92. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  93. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  94. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  95. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  96. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  97. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  98. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  99. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  100. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  101. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  102. data/lib/pdk/validate/validator.rb +118 -0
  103. data/lib/pdk/validate/validator_group.rb +104 -0
  104. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  105. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  106. data/lib/pdk/version.rb +1 -1
  107. data/locales/pdk.pot +513 -329
  108. metadata +76 -32
  109. data/lib/pdk/module/template_dir.rb +0 -115
  110. data/lib/pdk/module/template_dir/base.rb +0 -268
  111. data/lib/pdk/module/template_dir/git.rb +0 -91
  112. data/lib/pdk/module/template_dir/local.rb +0 -21
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -0,0 +1,20 @@
1
+ module PDK::CLI
2
+ @get_cmd = @base_cmd.define_command do
3
+ name 'get'
4
+ usage _('get [subcommand] [options]')
5
+ summary _('Retrieve information about the PDK or current project.')
6
+ default_subcommand 'help'
7
+
8
+ run do |_opts, args, _cmd|
9
+ if args == ['help']
10
+ PDK::CLI.run(%w[get --help])
11
+ exit 0
12
+ end
13
+
14
+ PDK::CLI.run(%w[get help]) if args.empty?
15
+ end
16
+ end
17
+ @get_cmd.add_command Cri::Command.new_basic_help
18
+ end
19
+
20
+ require 'pdk/cli/get/config'
@@ -0,0 +1,24 @@
1
+ module PDK::CLI
2
+ @get_config_cmd = @get_cmd.define_command do
3
+ name 'config'
4
+ usage _('config [name]')
5
+ summary _('Retrieve the configuration for <name>. If not specified, retrieve all configuration settings')
6
+
7
+ run do |_opts, args, _cmd|
8
+ item_name = args[0]
9
+ resolved_config = PDK.config.resolve(item_name)
10
+ # If the user wanted to know a setting but it doesn't exist, raise an error
11
+ if resolved_config.empty? && !item_name.nil?
12
+ PDK.logger.error(_("Configuration item '%{name}' does not exist") % { name: item_name })
13
+ exit 1
14
+ end
15
+ # If the user requested a setting and it's the only one resolved, then just output the value
16
+ if resolved_config.count == 1 && resolved_config.keys[0] == item_name
17
+ puts _('%{value}') % { value: resolved_config.values[0] }
18
+ exit 0
19
+ end
20
+ # Otherwise just output everything
21
+ resolved_config.keys.sort.each { |key| puts _('%{name}=%{value}') % { name: key, value: resolved_config[key] } }
22
+ end
23
+ end
24
+ end
data/lib/pdk/cli/new.rb CHANGED
@@ -17,3 +17,5 @@ require 'pdk/cli/new/provider'
17
17
  require 'pdk/cli/new/task'
18
18
  require 'pdk/cli/new/test'
19
19
  require 'pdk/cli/new/transport'
20
+ require 'pdk/cli/new/fact'
21
+ require 'pdk/cli/new/function'
@@ -13,7 +13,6 @@ module PDK::CLI
13
13
  )
14
14
 
15
15
  class_name = args[0]
16
- module_dir = Dir.pwd
17
16
 
18
17
  if class_name.nil? || class_name.empty?
19
18
  puts command.help
@@ -26,7 +25,8 @@ module PDK::CLI
26
25
 
27
26
  PDK::CLI::Util.analytics_screen_view('new_class', opts)
28
27
 
29
- PDK::Generate::PuppetClass.new(module_dir, class_name, opts).run
28
+ updates = PDK::Generate::PuppetClass.new(PDK.context, class_name, opts).run
29
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
30
30
  end
31
31
  end
32
32
  end
@@ -11,7 +11,6 @@ module PDK::CLI
11
11
  )
12
12
 
13
13
  defined_type_name = args[0]
14
- module_dir = Dir.pwd
15
14
 
16
15
  if defined_type_name.nil? || defined_type_name.empty?
17
16
  puts command.help
@@ -26,7 +25,8 @@ module PDK::CLI
26
25
 
27
26
  require 'pdk/generate/defined_type'
28
27
 
29
- PDK::Generate::DefinedType.new(module_dir, defined_type_name, opts).run
28
+ updates = PDK::Generate::DefinedType.new(PDK.context, defined_type_name, opts).run
29
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,29 @@
1
+ module PDK::CLI
2
+ @new_fact_cmd = @new_cmd.define_command do
3
+ name 'fact'
4
+ usage _('fact [options] <name>')
5
+ summary _('Create a new custom fact named <name> using given options')
6
+
7
+ run do |opts, args, _cmd|
8
+ PDK::CLI::Util.ensure_in_module!
9
+
10
+ fact_name = args[0]
11
+
12
+ if fact_name.nil? || fact_name.empty?
13
+ puts command.help
14
+ exit 1
15
+ end
16
+
17
+ unless Util::OptionValidator.valid_fact_name?(fact_name)
18
+ raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid fact name") % { name: fact_name }
19
+ end
20
+
21
+ PDK::CLI::Util.analytics_screen_view('new_fact', opts)
22
+
23
+ require 'pdk/generate/fact'
24
+
25
+ updates = PDK::Generate::Fact.new(PDK.context, fact_name, opts).run
26
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module PDK::CLI
2
+ @new_function_cmd = @new_cmd.define_command do
3
+ name 'function'
4
+ usage _('function [options] <name>')
5
+ summary _('Create a new function named <name> using given options')
6
+ option :t, :type, _('The function type, (native or v4)'), argument: :required, default: 'native'
7
+
8
+ run do |opts, args, _cmd|
9
+ PDK::CLI::Util.ensure_in_module!
10
+
11
+ function_name = args[0]
12
+
13
+ if function_name.nil? || function_name.empty?
14
+ puts command.help
15
+ exit 1
16
+ end
17
+
18
+ unless Util::OptionValidator.valid_function_name?(function_name)
19
+ raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid function name") % { name: function_name }
20
+ end
21
+
22
+ PDK::CLI::Util.analytics_screen_view('new_function', opts)
23
+
24
+ require 'pdk/generate/function'
25
+ updates = PDK::Generate::Function.new(PDK.context, function_name, opts).run
26
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
27
+ end
28
+ end
29
+ end
@@ -8,7 +8,6 @@ module PDK::CLI
8
8
  PDK::CLI::Util.ensure_in_module!
9
9
 
10
10
  provider_name = args[0]
11
- module_dir = Dir.pwd
12
11
 
13
12
  if provider_name.nil? || provider_name.empty?
14
13
  puts command.help
@@ -23,7 +22,8 @@ module PDK::CLI
23
22
 
24
23
  require 'pdk/generate/provider'
25
24
 
26
- PDK::Generate::Provider.new(module_dir, provider_name, opts).run
25
+ updates = PDK::Generate::Provider.new(PDK.context, provider_name, opts).run
26
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
27
27
  end
28
28
  end
29
29
  end
@@ -15,7 +15,6 @@ module PDK::CLI
15
15
  )
16
16
 
17
17
  task_name = args[0]
18
- module_dir = Dir.pwd
19
18
 
20
19
  if task_name.nil? || task_name.empty?
21
20
  puts command.help
@@ -28,7 +27,8 @@ module PDK::CLI
28
27
 
29
28
  PDK::CLI::Util.analytics_screen_view('new_task', opts)
30
29
 
31
- PDK::Generate::Task.new(module_dir, task_name, opts).run
30
+ updates = PDK::Generate::Task.new(PDK.context, task_name, opts).run
31
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
32
32
  end
33
33
  end
34
34
  end
@@ -18,7 +18,6 @@ module PDK::CLI
18
18
  )
19
19
 
20
20
  object_name = args[0]
21
- module_dir = Dir.pwd
22
21
 
23
22
  if object_name.nil? || object_name.empty?
24
23
  puts command.help
@@ -41,7 +40,8 @@ module PDK::CLI
41
40
 
42
41
  PDK::CLI::Util.analytics_screen_view('new_test', opts)
43
42
 
44
- generator.new(module_dir, obj['name'], opts.merge(spec_only: true)).run
43
+ updates = generator.new(PDK.context, obj['name'], opts.merge(spec_only: true)).run
44
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
45
45
  rescue PDK::Util::PuppetStrings::NoObjectError
46
46
  raise PDK::CLI::ExitWithError, _('Unable to find anything called "%{object}" to generate unit tests for.') % { object: object_name }
47
47
  rescue PDK::Util::PuppetStrings::NoGeneratorError => e
@@ -8,7 +8,6 @@ module PDK::CLI
8
8
  PDK::CLI::Util.ensure_in_module!
9
9
 
10
10
  transport_name = args[0]
11
- module_dir = Dir.pwd
12
11
 
13
12
  if transport_name.nil? || transport_name.empty?
14
13
  puts command.help
@@ -21,7 +20,8 @@ module PDK::CLI
21
20
 
22
21
  require 'pdk/generate/transport'
23
22
 
24
- PDK::Generate::Transport.new(module_dir, transport_name, opts).run
23
+ updates = PDK::Generate::Transport.new(PDK.context, transport_name, opts).run
24
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
25
25
  end
26
26
  end
27
27
  end
@@ -11,7 +11,7 @@ module PDK::CLI
11
11
  option nil, :'forge-upload-url', _('Set forge upload url path.'),
12
12
  argument: :required, default: 'https://forgeapi.puppetlabs.com/v3/releases'
13
13
 
14
- option nil, :'forge-token', _('Set Forge API token.'), argument: :required, default: nil
14
+ option nil, :'forge-token', _('Set Forge API token (you may also set via environment variable PDK_FORGE_TOKEN)'), argument: :required
15
15
 
16
16
  run do |opts, _args, cmd|
17
17
  # Make sure build is being run in a valid module directory with a metadata.json
@@ -27,6 +27,16 @@ module PDK::CLI
27
27
  opts[:'skip-build'] = true
28
28
  opts[:'skip-versionset'] = true
29
29
  opts[:force] = true unless PDK::CLI::Util.interactive?
30
+ opts[:'forge-token'] ||= PDK::Util::Env['PDK_FORGE_TOKEN']
31
+
32
+ if opts[:'forge-token'].nil? || opts[:'forge-token'].empty?
33
+ PDK.logger.error _(
34
+ 'You must supply a Forge API token either via `--forge-token` option ' \
35
+ 'or PDK_FORGE_TOKEN environment variable.',
36
+ )
37
+
38
+ exit 1
39
+ end
30
40
 
31
41
  Release.prepare_publish_interview(TTY::Prompt.new(help_color: :cyan), opts) unless opts[:force]
32
42
 
@@ -0,0 +1,20 @@
1
+ module PDK::CLI
2
+ @remove_cmd = @base_cmd.define_command do
3
+ name 'remove'
4
+ usage _('remove [subcommand] [options]')
5
+ summary _('Remove or delete information about the PDK or current project.')
6
+ default_subcommand 'help'
7
+
8
+ run do |_opts, args, _cmd|
9
+ if args == ['help']
10
+ PDK::CLI.run(%w[remove --help])
11
+ exit 0
12
+ end
13
+
14
+ PDK::CLI.run(%w[remove help]) if args.empty?
15
+ end
16
+ end
17
+ @remove_cmd.add_command Cri::Command.new_basic_help
18
+ end
19
+
20
+ require 'pdk/cli/remove/config'
@@ -0,0 +1,80 @@
1
+ module PDK::CLI
2
+ module Remove
3
+ module Config
4
+ def self.run(opts, args)
5
+ item_name = (args.count > 0) ? args[0] : nil
6
+ item_value = (args.count > 1) ? args[1].strip : nil
7
+ item_value = nil if !item_value.nil? && item_value.empty?
8
+
9
+ force = opts[:force] || false
10
+
11
+ raise PDK::CLI::ExitWithError, _('Configuration name is required') if item_name.nil?
12
+
13
+ current_value = PDK.config.get(item_name)
14
+ raise PDK::CLI::ExitWithError, _("The configuration item '%{name}' can not be removed.") % { name: item_name } if current_value.is_a?(PDK::Config::Namespace)
15
+ if current_value.nil?
16
+ PDK.logger.info(_("Could not remove '%{name}' as it has not been set") % { name: item_name })
17
+ return 0
18
+ end
19
+
20
+ PDK.logger.info(_("Ignoring the item value '%{value}' as --force has been set") % { value: item_value }) if current_value.is_a?(Array) && !item_value.nil? && force
21
+ PDK.logger.info(_('Ignoring --force as the setting is not multi-valued')) if !current_value.is_a?(Array) && force
22
+
23
+ # FIXME: It'd be nice to shortcircuit deleting default values. This causes the configuration file
24
+ # to be saved, even though nothing actually changes
25
+
26
+ # For most value types, just changing the value to nil is enough, however Arrays are a special case.
27
+ # Unless they're forced, array removal with either remove a single entry (matched by .to_s) or clear the
28
+ # array. When forced, the array is completed removed just like a string or number.
29
+ if current_value.is_a?(Array) && !force
30
+ # If the user didn't set a value then set the array as empty, otherwise remove that one item
31
+ new_value = item_value.nil? ? [] : current_value.reject { |item| item.to_s == item_value }
32
+ if current_value.count == new_value.count
33
+ if item_value.nil?
34
+ PDK.logger.info(_("Could not remove '%{name}' as it is already empty") % { name: item_name })
35
+ else
36
+ PDK.logger.info(_("Could not remove '%{value}' from '%{name}' as it has not been set") % { value: item_value, name: item_name })
37
+ end
38
+ return 0
39
+ end
40
+ PDK.config.set(item_name, new_value, force: true)
41
+ else
42
+ # Set the value to nil for deleting.
43
+ PDK.config.set(item_name, nil, force: true)
44
+ end
45
+
46
+ # Output the result to the user
47
+ new_value = PDK.config.get(item_name)
48
+ if current_value.is_a?(Array) && !force
49
+ # Arrays have a special output format. If item_value is nil then the user wanted to empty/clear
50
+ # the array otherwise they just wanted to remove a single entry.
51
+ if item_value.nil?
52
+ PDK.logger.info(_("Cleared '%{name}' which had a value of '%{from}'") % { name: item_name, from: current_value })
53
+ else
54
+ PDK.logger.info(_("Removed '%{value}' from '%{name}'") % { value: item_value, name: item_name })
55
+ end
56
+ elsif !new_value.nil?
57
+ PDK.logger.info(_("Could not remove '%{name}' as it using a default value of '%{to}'") % { name: item_name, to: new_value })
58
+ else
59
+ PDK.logger.info(_("Removed '%{name}' which had a value of '%{from}'") % { name: item_name, from: current_value })
60
+ end
61
+
62
+ # Same output as `get config`
63
+ $stdout.puts _('%{name}=%{value}') % { name: item_name, value: new_value }
64
+ 0
65
+ end
66
+ end
67
+ end
68
+
69
+ @remove_config_cmd = @remove_cmd.define_command do
70
+ name 'config'
71
+ usage _('config [name] [value]')
72
+ summary _('Remove or delete the configuration for <name>')
73
+
74
+ option :f, :force, _('Force multi-value configuration settings to be removed instead of emptied.'), argument: :forbidden
75
+
76
+ run do |opts, args, _cmd|
77
+ exit PDK::CLI::Remove::Config.run(opts, args)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,20 @@
1
+ module PDK::CLI
2
+ @set_cmd = @base_cmd.define_command do
3
+ name 'set'
4
+ usage _('set [subcommand] [options]')
5
+ summary _('Set or update information about the PDK or current project.')
6
+ default_subcommand 'help'
7
+
8
+ run do |_opts, args, _cmd|
9
+ if args == ['help']
10
+ PDK::CLI.run(%w[set --help])
11
+ exit 0
12
+ end
13
+
14
+ PDK::CLI.run(%w[set help]) if args.empty?
15
+ end
16
+ end
17
+ @set_cmd.add_command Cri::Command.new_basic_help
18
+ end
19
+
20
+ require 'pdk/cli/set/config'
@@ -0,0 +1,119 @@
1
+ module PDK::CLI
2
+ module Set
3
+ module Config
4
+ ALLOWED_TYPE_NAMES = %w[array boolean number string].freeze
5
+
6
+ # :nocov:
7
+ def self.pretty_allowed_names
8
+ ALLOWED_TYPE_NAMES.map { |name| "'#{name}'" }.join(', ')
9
+ end
10
+ # :nocov:
11
+
12
+ def self.transform_value(type_name, value)
13
+ normalized_name = type_name.downcase.strip
14
+ unless ALLOWED_TYPE_NAMES.include?(normalized_name)
15
+ raise PDK::CLI::ExitWithError, _('Unknown type %{type_name}. Expected one of %{allowed}') % { type_name: type_name, allowed: pretty_allowed_names }
16
+ end
17
+
18
+ # Short circuit string conversions as it's trivial
19
+ if normalized_name == 'string'
20
+ raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name } unless value.is_a?(String)
21
+ return value
22
+ end
23
+
24
+ begin
25
+ case normalized_name
26
+ when 'array'
27
+ convert_to_array(value)
28
+ when 'boolean'
29
+ convert_to_boolean(value)
30
+ when 'number'
31
+ convert_to_number(value)
32
+ else
33
+ value
34
+ end
35
+ rescue ArgumentError, TypeError
36
+ raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name }
37
+ end
38
+ end
39
+
40
+ def self.convert_to_array(value)
41
+ return [] if value.nil?
42
+ value.is_a?(Array) ? value : [value]
43
+ end
44
+ private_class_method :convert_to_array
45
+
46
+ def self.convert_to_boolean(value)
47
+ string_val = value.to_s.strip.downcase
48
+
49
+ return true if %w[yes true -1 1].include?(string_val)
50
+ return false if %w[no false 0].include?(string_val)
51
+
52
+ raise ArgumentError
53
+ end
54
+ private_class_method :convert_to_boolean
55
+
56
+ def self.convert_to_number(value)
57
+ float_val = Float(value)
58
+ # Return an Integer if this is actually and Integer, otherwise return the float
59
+ (float_val.truncate == float_val) ? float_val.truncate : float_val
60
+ end
61
+ private_class_method :convert_to_number
62
+
63
+ def self.run(opts, args)
64
+ item_name = (args.count > 0) ? args[0] : nil
65
+ item_value = (args.count > 1) ? args[1] : nil
66
+
67
+ opts[:type] = opts[:as] if opts[:type].nil? && !opts[:as].nil?
68
+ force = opts[:force] || false
69
+
70
+ # Transform the value if we need to
71
+ item_value = PDK::CLI::Set::Config.transform_value(opts[:type], item_value) unless opts[:type].nil?
72
+
73
+ raise PDK::CLI::ExitWithError, _('Configuration name is required') if item_name.nil?
74
+ raise PDK::CLI::ExitWithError, _('Configuration value is required. If you wish to remove a value use \'pdk remove config\'') if item_value.nil?
75
+
76
+ current_value = PDK.config.get(item_name)
77
+ raise PDK::CLI::ExitWithError, _("The configuration item '%{name}' can not have a value set.") % { name: item_name } if current_value.is_a?(PDK::Config::Namespace)
78
+
79
+ # If we're forcing the value, don't do any munging
80
+ unless force
81
+ # Check if the setting already exists
82
+ if current_value.is_a?(Array) && current_value.include?(item_value)
83
+ PDK.logger.info(_("No changes made to '%{name}' as it already contains value '%{to}'") % { name: item_name, to: item_value })
84
+ return 0
85
+ end
86
+ end
87
+
88
+ new_value = PDK.config.set(item_name, item_value, force: opts[:force])
89
+ if current_value.nil? || force
90
+ PDK.logger.info(_("Set initial value of '%{name}' to '%{to}'") % { name: item_name, to: new_value })
91
+ elsif current_value.is_a?(Array)
92
+ # Arrays have a special output format
93
+ PDK.logger.info(_("Added new value '%{to}' to '%{name}'") % { name: item_name, to: item_value })
94
+ else
95
+ PDK.logger.info(_("Changed existing value of '%{name}' from '%{from}' to '%{to}'") % { name: item_name, from: current_value, to: new_value })
96
+ end
97
+
98
+ # Same output as `get config`
99
+ $stdout.puts _('%{name}=%{value}') % { name: item_name, value: PDK.config.get(item_name) }
100
+ 0
101
+ end
102
+ end
103
+ end
104
+
105
+ @set_config_cmd = @set_cmd.define_command do
106
+ name 'config'
107
+ usage _('config [name] [value]')
108
+ summary _('Set or update the configuration for <name>')
109
+
110
+ option :f, :force, _('Force the configuration setting to be overwitten.'), argument: :forbidden
111
+
112
+ option :t, :type, _('The type of value to set. Acceptable values: %{values}') % { values: PDK::CLI::Set::Config.pretty_allowed_names }, argument: :required
113
+ option nil, :as, _('Alias of --type'), argument: :required
114
+
115
+ run do |opts, args, _cmd|
116
+ exit PDK::CLI::Set::Config.run(opts, args)
117
+ end
118
+ end
119
+ end