Sha256: 1cf045ae9a5d4f5d9488bb09f00927ed43d990120a8107f10f2ab771c7510b91

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

require 'commands/base'

module Dreamy
  module Command
    class InvalidCommand < RuntimeError; end

    class << self
      def run(command, args)
        run_internal(command, args)
      rescue InvalidCommand
        error "Unknown command. Run 'dh help' for usage information."
      rescue ApiError => e
        error "DreamHost returned this error: #{e}"
      rescue Interrupt => e
        error "\n[canceled]"
      end

      def run_internal(command, args)
        namespace, command = parse(command)
        require "commands/#{namespace}"
        klass = Dreamy::Command.const_get(namespace.capitalize).new(args)
        raise InvalidCommand unless klass.respond_to?(command)
        klass.send(command)
      end

      def error(msg)
        STDERR.puts(msg)
        exit 1
      end

      def parse(command)
        parts = command.split(':')
        case parts.size
        when 1
          if namespaces.include? command
            return command, 'index'
          else
            raise InvalidCommand
          end
        when 2
          raise InvalidCommand unless namespaces.include? parts[0]
          return parts
        else
          raise InvalidCommand
        end
      end

      def namespaces
        @@namespaces ||= Dir["#{File.dirname(__FILE__)}/commands/*"].map do |namespace|
          namespace.gsub(/.*\//, '').gsub(/\.rb/, '')
        end
      end

    end

  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
jordan-brough-dreamy-0.5.1 lib/dreamy/command.rb
sant0sk1-dreamy-0.3.0 lib/dreamy/command.rb
sant0sk1-dreamy-0.4.1 lib/dreamy/command.rb
sant0sk1-dreamy-0.5.0 lib/dreamy/command.rb
sant0sk1-dreamy-0.5.1 lib/dreamy/command.rb
dreamy-0.5.3 lib/dreamy/command.rb
dreamy-0.5.2 lib/dreamy/command.rb
dreamy-0.5.1 lib/dreamy/command.rb