Sha256: 0cdd6ea90f04046387689988872c5bd8246d0945372c50944e188bd0aa358346

Contents?: true

Size: 1.55 KB

Versions: 15

Compression:

Stored size: 1.55 KB

Contents

module Capistrano
  class Configuration
    class Question
      def initialize(key, default, options={})
        @key = key
        @default = default
        @options = options
      end

      def call
        ask_question
        value_or_default
      end

      private

      attr_reader :key, :default, :options

      def ask_question
        $stdout.print question
        $stdout.flush
      end

      def value_or_default
        if response.empty?
          default
        else
          response
        end
      end

      def response
        return @response if defined? @response

        @response = (gets || "").chomp
      end

      def gets
        return unless stdin.tty?

        if echo?
          stdin.gets
        else
          stdin.noecho(&:gets).tap { $stdout.print "\n" }
        end
      rescue Errno::EIO
        # when stdio gets closed
        return
      end

      def question
        if prompt && default.nil?
          I18n.t(:question_prompt, key: prompt, scope: :capistrano)
        elsif prompt
          I18n.t(:question_prompt_default, key: prompt, default_value: default, scope: :capistrano)
        elsif default.nil?
          I18n.t(:question, key: key, scope: :capistrano)
        else
          I18n.t(:question_default, key: key, default_value: default, scope: :capistrano)
        end
      end

      def echo?
        (options || {}).fetch(:echo, true)
      end

      def stdin
        (options || {}).fetch(:stdin, $stdin)
      end

      def prompt
        (options || {}).fetch(:prompt, nil)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
capistrano-3.19.2 lib/capistrano/configuration/question.rb
capistrano-3.19.1 lib/capistrano/configuration/question.rb
capistrano-3.19.0 lib/capistrano/configuration/question.rb
capistrano-3.18.1 lib/capistrano/configuration/question.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/capistrano-3.18.0/lib/capistrano/configuration/question.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/capistrano-3.18.0/lib/capistrano/configuration/question.rb
capistrano-3.18.0 lib/capistrano/configuration/question.rb
capistrano-3.17.3 lib/capistrano/configuration/question.rb
capistrano-3.17.2 lib/capistrano/configuration/question.rb
capistrano-3.17.1 lib/capistrano/configuration/question.rb
capistrano-3.17.0 lib/capistrano/configuration/question.rb
capistrano-3.16.0 lib/capistrano/configuration/question.rb
capistrano-3.15.0 lib/capistrano/configuration/question.rb
capistrano-3.14.1 lib/capistrano/configuration/question.rb
capistrano-3.14.0 lib/capistrano/configuration/question.rb