Module: PDK::CLI::Util
- Defined in:
- lib/pdk/cli/util.rb,
lib/pdk/cli/util/interview.rb,
lib/pdk/cli/util/option_validator.rb,
lib/pdk/cli/util/option_normalizer.rb,
lib/pdk/cli/util/command_redirector.rb
Defined Under Namespace
Classes: CommandRedirector, Interview, OptionNormalizer, OptionValidator
Class Method Summary
collapse
Class Method Details
.ensure_in_module!(opts = {}) ⇒ Object
Ensures the calling code is being run from inside a module directory.
13
14
15
16
17
18
19
|
# File 'lib/pdk/cli/util.rb', line 13
def ensure_in_module!(opts = {})
return unless PDK::Util.module_root.nil?
return if opts[:check_module_layout] && PDK::Util.in_module_root?
message = opts.fetch(:message, _('This command must be run from inside a valid module (no metadata.json found).'))
raise PDK::CLI::ExitWithError.new(message, opts)
end
|
.interactive? ⇒ Boolean
51
52
53
54
55
56
57
|
# File 'lib/pdk/cli/util.rb', line 51
def interactive?
return false if PDK.logger.debug?
return !ENV['PDK_FRONTEND'].casecmp('noninteractive').zero? if ENV['PDK_FRONTEND']
return false unless $stderr.isatty
true
end
|
.module_version_check ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/pdk/cli/util.rb', line 60
def module_version_check
module_pdk_ver = PDK::Util.module_pdk_version
raise PDK::CLI::ExitWithError, _('This module is not PDK compatible. Run `pdk convert` to make it compatible with your version of PDK.') if module_pdk_ver.nil?
if Gem::Version.new(module_pdk_ver) < Gem::Version.new('1.3.1')
PDK.logger.warn _('This module template is out of date. Run `pdk convert` to make it compatible with your version of PDK.')
elsif Gem::Version.new(PDK::VERSION) < Gem::Version.new(module_pdk_ver)
PDK.logger.warn _('This module is compatible with a newer version of PDK. Upgrade your version of PDK to ensure compatibility.')
elsif Gem::Version.new(PDK::VERSION) > Gem::Version.new(module_pdk_ver)
PDK.logger.warn _('This module is compatible with an older version of PDK. Run `pdk update` to update it to your version of PDK.')
end
end
|
.prompt_for_yes(question_text, opts = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/pdk/cli/util.rb', line 33
def prompt_for_yes(question_text, opts = {})
prompt = opts[:prompt] || TTY::Prompt.new(help_color: :cyan)
validator = proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }
response = nil
begin
response = prompt.yes?(question_text) do |q|
q.default opts[:default] unless opts[:default].nil?
q.validate(validator, _('Answer "Y" to continue or "n" to cancel.'))
end
rescue TTY::Prompt::Reader::InputInterrupt
PDK.logger.info opts[:cancel_message] if opts[:cancel_message]
end
response
end
|
.puppet_from_opts_or_env(opts) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/pdk/cli/util.rb', line 85
def puppet_from_opts_or_env(opts)
desired_puppet_version = (opts || {})[:'puppet-version'] || ENV['PDK_PUPPET_VERSION']
desired_pe_version = (opts || {})[:'pe-version'] || ENV['PDK_PE_VERSION']
begin
puppet_env =
if desired_puppet_version
PDK::Util::PuppetVersion.find_gem_for(desired_puppet_version)
elsif desired_pe_version
PDK::Util::PuppetVersion.from_pe_version(desired_pe_version)
else
PDK::Util::PuppetVersion.from_module_metadata || PDK::Util::PuppetVersion.latest_available
end
rescue ArgumentError => e
raise PDK::CLI::ExitWithError, e.message
end
PDK.logger.info(_('Using Ruby %{version}') % {
version: puppet_env[:ruby_version],
})
gemset = { puppet: puppet_env[:gem_version].to_s }
gemset.each do |gem, version|
next if version.nil?
PDK.logger.info(_('Using %{gem} %{version}') % {
gem: gem.to_s.capitalize,
version: version,
})
end
{
gemset: gemset,
ruby_version: puppet_env[:ruby_version],
}
end
|
22
23
24
25
26
27
28
29
30
|
# File 'lib/pdk/cli/util.rb', line 22
def spinner_opts_for_platform
windows_opts = {
success_mark: '*',
error_mark: 'X',
}
return windows_opts if Gem.win_platform?
{}
end
|
.validate_puppet_version_opts(opts) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/pdk/cli/util.rb', line 126
def validate_puppet_version_opts(opts)
puppet_ver_specs = []
puppet_ver_specs << '--puppet-version option' if opts[:'puppet-version']
puppet_ver_specs << 'PDK_PUPPET_VERSION environment variable' if ENV['PDK_PUPPET_VERSION'] && !ENV['PDK_PUPPET_VERSION'].empty?
pe_ver_specs = []
pe_ver_specs << '--pe-version option' if opts[:'pe-version']
pe_ver_specs << 'PDK_PE_VERSION environment variable' if ENV['PDK_PE_VERSION'] && !ENV['PDK_PE_VERSION'].empty?
puppet_ver_specs.each do |pup_ver_spec|
next if pe_ver_specs.empty?
offending = [pup_ver_spec, pe_ver_specs[0]].sort
raise PDK::CLI::ExitWithError, _('You cannot specify a %{first} and %{second} at the same time.') % {
first: offending[0],
second: offending[1],
}
end
if puppet_ver_specs.size == 2
warning_str = 'Puppet version option from command line: "--puppet-version=%{pup_ver_opt}" '
warning_str += 'overrides value from environment: "PDK_PUPPET_VERSION=%{pup_ver_env}". You should not specify both.'
PDK.logger.warn(_(warning_str) % {
pup_ver_opt: opts[:'puppet-version'],
pup_ver_env: ENV['PDK_PUPPET_VERSION'],
})
elsif pe_ver_specs.size == 2
warning_str = 'Puppet Enterprise version option from command line: "--pe-version=%{pe_ver_opt}" '
warning_str += 'overrides value from environment: "PDK_PE_VERSION=%{pe_ver_env}". You should not specify both.'
PDK.logger.warn(_(warning_str) % {
pup_ver_opt: opts[:'pe-version'],
pup_ver_env: ENV['PDK_PE_VERSION'],
})
end
end
|