Sha256: a53792a14636b96b54b621b4a7b7d34803d598bd5498bf31945112e45624317e

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 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 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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capistrano-3.13.0 lib/capistrano/configuration/question.rb
capistrano-3.12.1 lib/capistrano/configuration/question.rb
capistrano-3.12.0 lib/capistrano/configuration/question.rb
capistrano-3.11.2 lib/capistrano/configuration/question.rb