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

Constant Summary collapse

MODULE_FOLDERS =
%w[
  manifests
  lib
  tasks
  facts.d
  functions
  types
].freeze

Class Method Summary collapse

Class Method Details

.ensure_in_module!(opts = {}) ⇒ Object

Ensures the calling code is being run from inside a module directory.

Parameters:

  • opts (Hash) (defaults to: {})

    options to change the behavior of the check logic.

Options Hash (opts):

  • :check_module_layout (Boolean)

    Set to true to check for stardard module folder layout if the module does not contain a metadata.json file.

Raises:



22
23
24
25
26
27
28
# File 'lib/pdk/cli/util.rb', line 22

def ensure_in_module!(opts = {})
  return unless PDK::Util.module_root.nil?
  return if opts[:check_module_layout] && PDK::CLI::Util::MODULE_FOLDERS.any? { |dir| File.directory?(dir) }

  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

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/pdk/cli/util.rb', line 60

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

.prompt_for_yes(question_text, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pdk/cli/util.rb', line 42

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

.spinner_opts_for_platformObject



31
32
33
34
35
36
37
38
39
# File 'lib/pdk/cli/util.rb', line 31

def spinner_opts_for_platform
  windows_opts = {
    success_mark: '*',
    error_mark: 'X',
  }

  return windows_opts if Gem.win_platform?
  {}
end