Sha256: 0f668240e3ee66b7ac03389d5c9b139ea0c3de3803ecf93dd99088f4c7e523fa

Contents?: true

Size: 1.89 KB

Versions: 53

Compression:

Stored size: 1.89 KB

Contents

require_relative '../opto'

module Kontena::Cli::Stacks
  module YAML
    class Prompt < ::Opto::Resolver
      include Kontena::Cli::Common

      using ::Opto::Extension::HashStringOrSymbolKey

      def enum?
        option.type == 'enum'
      end

      def boolean?
        option.type == 'boolean'
      end

      def prompt_word
        return "Select" if enum?
        return "Enable" if boolean?
        "Enter"
      end

      def question_text
        (!hint.nil? && hint != option.name) ? "#{hint} :" : "#{prompt_word} #{option.label || option.name} :"
      end

      def enum_can_be_other?
        enum? && option.handler.options[:can_be_other] ? true : false
      end

      def enum
        opts = option.handler.options[:options]
        opts << { label: '(Other)', value: nil, description: '(Other)' } if enum_can_be_other?

        answer = prompt.select(question_text) do |menu|
          menu.enum ':' # makes it show numbers before values, you can press the number to select.
          menu.default(opts.index {|opt| opt[:value] == option.default }.to_i + 1) if option.default
          opts.each do |opt|
            menu.choice opt[:label], opt[:value]
          end
        end

        if answer.nil? && enum_can_be_other?
          ask
        else
          answer
        end
      end

      def bool
        prompt.yes?(question_text, default: option.default.nil? || option.default)
      end

      def echo?
        return true if option.handler.nil?
        return true if option.handler.options.nil?
        return true if option.handler.options[:echo].nil?
        option.handler.options[:echo]
      end

      def ask
        prompt.ask(question_text, default: option.default, echo: echo?)
      end

      def resolve
        return nil if option.skip?
        if enum?
          enum
        elsif boolean?
          bool
        else
          ask
        end
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
krates-1.7.11 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.10 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.9 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.8 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.7 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.6 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.5 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.4 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.3 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.2 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.1 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.7.0 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.9 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.8 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.7 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.6 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.5 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.4 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.3 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb
krates-1.6.2 lib/kontena/cli/stacks/yaml/opto/prompt_resolver.rb