Sha256: 385b033965bdfd7d8ff4d3703bcb54531fbfd4d28f2842411aecda247744dced

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require "swa/cli/base_command"
require "swa/cli/cloud_formation_command"
require "swa/cli/ec2_command"
require "swa/cli/elb_command"
require "swa/cli/glue_command"
require "swa/cli/iam_command"
require "swa/cli/kms_command"
require "swa/cli/s3_command"

module Swa
  module CLI

    class MainCommand < BaseCommand

      subcommand ["cf", "cloudformation"], "CloudFormation stuff", CloudFormationCommand
      subcommand "ec2", "EC2 stuff", Ec2Command
      subcommand "elb", "elb stuff", ElbCommand
      subcommand "glue", "Glue stuff", GlueCommand
      subcommand "iam", "IAM stuff", IamCommand
      subcommand "kms", "KMS stuff", KmsCommand
      subcommand "s3", "S3 stuff", S3Command

      protected

      RESOURCE_PREFIXES_BY_SERVICE = {
        "ec2" => %w(ami i sg subnet vpc)
      }

      def subcommand_for_prefix(prefix)
        RESOURCE_PREFIXES_BY_SERVICE.each do |subcommand, prefixes|
          return subcommand if prefixes.member?(prefix)
        end
      end

      def parse_parameters
        case remaining_arguments.first
        when /^(\w+)-/
          subcommand = subcommand_for_prefix($1)
          remaining_arguments.unshift(subcommand) if subcommand
        when %r{^s3://([^/]+)/(.+/)?$}
          remaining_arguments[0, 1] = ["s3", "bucket", $1, "objects", "--prefix", $2]
        when %r{^s3://([^/]+)/(.+)\*$}
          remaining_arguments[0, 1] = ["s3", "bucket", $1, "objects", "--prefix", $2]
        when %r{^s3://([^/]+)/(.+)$}
          remaining_arguments[0, 1] = ["s3", "bucket", $1, "object", $2]
        when %r{^s3://([^/]+)$}
          remaining_arguments[0, 1] = ["s3", "bucket", $1]
        end
        super
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
swa-0.7.2 lib/swa/cli/main_command.rb
swa-0.7.1 lib/swa/cli/main_command.rb
swa-0.7.0 lib/swa/cli/main_command.rb