Sha256: 46f654c56b9206565c1633efe70a962ac5013e343f178a74f6ac44d5064dd647

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

module SafePusher
  class CLI
    def initialize(arguments:)
      @arguments = arguments
    end

    def start
      return version if arguments.first == '--version'

      help if commands.include?(nil)

      commands.compact.each { |command| execute_command(command) }
    end

    private

    attr_reader :arguments

    def execute_command(command)
      explain(command) if verbose

      results = SafePusher::Client
        .const_get(services[command].capitalize)
        .new
        .public_send(command)

      exit results unless results == 0
    end

    def commands
      @commands ||= arguments.map do |arg|
        next unless arg =~ valid_commands_regexp

        shortcut_to_command[arg] || arg
      end
    end

    def version
      puts SafePusher::VERSION
    end

    def explain(command)
      puts I18n.t("command.#{command}.verbose").yellow
    end

    def help
      puts I18n.t('help')
    end

    def valid_commands_regexp
      @valid_commands_regexp ||= /^(?!\s*$)(?:#{available_commands})$/
    end

    def available_commands
      @available_commands ||= (
        shortcut_to_command.keys + shortcut_to_command.values
      ).join('|')
    end

    def verbose
      @verbose ||= SafePusher.configuration.verbose
    end

    def services
      @services ||= SafePusher.configuration.services
    end

    def shortcut_to_command
      @shortcut_to_command ||= YAML
        .load_file("#{__dir__}/../../config/commands.yml")
        .reduce({}) { |o, (k, v)| o.update(v['shortcut'] => k) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
safe_pusher-0.5.7 lib/safe_pusher/cli.rb
safe_pusher-0.5.6 lib/safe_pusher/cli.rb
safe_pusher-0.5.5 lib/safe_pusher/cli.rb
safe_pusher-0.5.4 lib/safe_pusher/cli.rb