Class: SchemaTransformer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_transformer/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



17
18
19
# File 'lib/schema_transformer/cli.rb', line 17

def initialize(args)
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

The array of (unparsed) command-line options



13
14
15
# File 'lib/schema_transformer/cli.rb', line 13

def args
  @args
end

#optionsObject (readonly)

The hash of (parsed) command-line options



15
16
17
# File 'lib/schema_transformer/cli.rb', line 15

def options
  @options
end

Class Method Details

.run(args) ⇒ Object



6
7
8
9
10
# File 'lib/schema_transformer/cli.rb', line 6

def self.run(args)
  cli = new(args)
  cli.parse_options!
  cli.run
end

Instance Method Details

#extract_environment_variables!Object

Extracts name=value pairs from the remaining command-line arguments and assigns them as environment variables.



72
73
74
75
76
77
# File 'lib/schema_transformer/cli.rb', line 72

def extract_environment_variables! #:nodoc:
  args.delete_if do |arg|
    next unless arg.match(/^(\w+)=(.*)$/)
    ENV[$1] = $2
  end
end

#option_parserObject

Return an OptionParser instance that defines the acceptable command line switches for cloud_info, and what their corresponding behaviors are.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schema_transformer/cli.rb', line 24

def option_parser
  # @logger = Logger.new
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] [action]"
    
    opts.on("-h", "--help", "Display this help message.") do
      puts help_message
      puts opts
      exit
    end

    opts.on("-v", "--verbose",
      "Verbose mode"
    ) { |value| options[:verbose] = true }
    
    opts.on("-s", "--stagger [VALUE]",
      "Number of seconds to wait inbetween each bulk insert. Default 0"
    ) { |value| options[:stagger] = value }
    
    opts.on("-V", "--version",
      "Display the schema_transformer version, and exit."
    ) do
      require File.expand_path("../version", __FILE__)
      puts "Schema Transformer v#{SchemaTransformer::VERSION}"
      exit
    end

  end
end

#parse_options!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/schema_transformer/cli.rb', line 54

def parse_options!
  @options = {:action => nil}
  
  if args.empty?
    warn "Please specifiy an action to execute."
    warn help_message
    warn option_parser
    exit 1
  end
  
  option_parser.parse!(args)
  extract_environment_variables!
  
  options[:action] = args # ignore remaining
end

#runObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/schema_transformer/cli.rb', line 79

def run
  @action = options[:action].first
  if @action == "analyze"
    SchemaTransformer::Analyze.run(options)
  else
    begin
      SchemaTransformer::Transform.run(options)
    rescue UsageError => e
      puts "Usage Error: #{e.message}"
      puts help_message
      puts option_parser
    end
  end
end