Sha256: 309a92e0a84385e127eeb5cd686a436923c98dde4626140225308505cbb865b9

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

module Wordmove
  class EnvironmentsList
    attr_reader :movefile, :logger, :remote_vhosts, :local_vhost

    class << self
      def print(cli_options)
        new(cli_options).print
      end
    end

    def initialize(options)
      @logger = Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
      @movefile = Wordmove::Movefile.new(options[:config])
      @remote_vhosts = []
      @local_vhost = []
    end

    def print
      contents = parse_movefile(movefile: movefile)
      generate_vhost_list(contents: contents)
      output
    end

    private

    def select_vhost(contents:)
      target = contents.select { |_key, env| env[:vhost].present? }
      target.map { |key, env| { env: key, vhost: env[:vhost] } }
    end

    def parse_movefile(movefile:)
      movefile.fetch
    end

    def output
      logger.task('Listing Local')
      logger.plain(output_string(vhost_list: local_vhost))

      logger.task('Listing Remotes')
      logger.plain(output_string(vhost_list: remote_vhosts))
    end

    def output_string(vhost_list:)
      return 'vhost list is empty' if vhost_list.empty?

      vhost_list.each_with_object("") do |entry, retval|
        retval << "#{entry[:env]}: #{entry[:vhost]}\n"
      end
    end

    #
    # return env, vhost map
    # Exp. {:env=>:local, :vhost=>"http://vhost.local"},
    #      {:env=>:production, :vhost=>"http://example.com"}
    #
    def generate_vhost_list(contents:)
      # select object which has 'vhost' only
      vhosts = select_vhost(contents: contents)
      vhosts.each do |list|
        if list[:env] == :local
          @local_vhost << list
        else
          @remote_vhosts << list
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wordmove-5.2.2 lib/wordmove/environments_list.rb
wordmove-5.2.1 lib/wordmove/environments_list.rb
wordmove-5.2.0 lib/wordmove/environments_list.rb
wordmove-5.1.0 lib/wordmove/environments_list.rb
wordmove-5.0.2 lib/wordmove/environments_list.rb
wordmove-5.0.1 lib/wordmove/environments_list.rb
wordmove-5.0.0.dev lib/wordmove/environments_list.rb
wordmove-5.0.0 lib/wordmove/environments_list.rb