require 'ey-core/cli/subcommand'

module Ey
  module Core
    module Cli
      class Servers < Subcommand
        title "servers"
        summary "List servers you have access to"

        option :account,
          short: 'c',
          long: 'account',
          description: 'Filter by account name or id',
          argument: 'Account'

        option :environment,
          short: "-e",
          long: "environment",
          description: "Filter by environment.",
          argument: "environment"

        def handle
          puts TablePrint::Printer.
            new(servers, [{id: {width: 10}}, :role, :provisioned_id]).
            table_print
        end

        private
        def servers
          if option(:account)
            core_client.servers.all(account: core_account)
          elsif environment = option(:environment)
            (core_client.environments.get(environment) || core_client.environments.first(name: environment)).servers.all
          else
            core_client.servers.all
          end
        end
      end
    end
  end
end

require 'ey-core/cli/subcommand'

module Ey
  module Core
    module Cli
      class Servers < Subcommand
        title 'servers'
        summary 'List servers you have access to'

        option :account,
          short: 'c',
          long: 'account',
          description: 'Filter by account name or id',
          argument: 'account'

        option :environment,
          short: 'e',
          long: 'environment',
          description: 'Filter by environment.',
          argument: 'environment'

        def handle
          puts TablePrint::Printer.
            new(servers, [{id: {width: 10}}, :role, :provisioned_id]).
            table_print
        end

        private
        def servers
          filter_opts = {}

          operator =
            if option(:environment)
              core_account.environments.first(name: option(:environment))
            else
              filter_opts[:account] = core_account.id if option(:account)
              core_client
            end

          operator ? operator.servers.all(filter_opts) : nil
        end
      end
    end
  end
end

Fix account and environment filters
Fix account and environment filters

The previous iteration returned an error when specifying an account. Furthermore, when a user has access to multiple accounts with multiple environments of the same name, the environments toggle would return only return the environment for the main account—and since the account couldn’t be set, this was problematic. Also, the environments short option has been fixed (“-e” has been changed to “e”).
