Sha256: 94026dee3c37f5534cc8b3680de2bc9470b7c109d2b9713f4bfbd7ea8a4f2d07

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require_relative "includes"

module AwsAssumeRole::Ui
    include AwsAssumeRole

    ::I18n.load_path += Dir.glob(File.join(File.realpath(__dir__), "..", "..", "i18n", "*.{rb,yml,yaml}"))
    ::I18n.locale = ENV.fetch("LANG", nil).split(".").first.split("_").first || I18n.default_locale

    module_function

    def out(text)
        puts text
    end

    def pastel
        @pastel ||= Pastel.new
    end

    def input
        @input ||= HighLine.new
    end

    def validation_errors_to_s(result)
        text = result.errors.keys.map do |k|
            result.errors[k].join(";")
        end.join(" ")
        text
    end

    def error(text)
        puts pastel.red(text)
    end

    def show_validation_errors(result)
        error validation_errors_to_s(result)
    end

    def ask_with_validation(variable_name, question, type: Dry::Types["coercible.string"], &block)
        STDOUT.puts pastel.yellow question
        validator = Dry::Validation.Schema do
            configure do
                config.messages = :i18n
            end
            required(variable_name) { instance_eval(&block) }
        end
        result = validator.call(variable_name => type[(STDIN.gets || "").chomp])
        return result.to_h[variable_name] if result.success?
        show_validation_errors result
        ask_with_validation variable_name, question, &block
    end

    def t(*options)
        ::I18n.t(options).first
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aws_assume_role-1.0.1 lib/aws_assume_role/ui.rb
aws_assume_role-1.0.0 lib/aws_assume_role/ui.rb