Class: FlattenDB::CLI

Inherits:
Nuggets::CLI
  • Object
show all
Defined in:
lib/flattendb/cli.rb

Constant Summary collapse

TYPES =
{
  :mysql => {
    :title => 'MySQL',
    :opts  => lambda { |opts, options|
      opts.on('-x', '--xml', 'Input file is of type XML [This is the default]') {
        options[:type] = :xml
      }
      opts.on('-s', '--sql', 'Input file is of type SQL') {
        options[:type] = :sql
      }
    }
  },
  :mdb => {
    :title => 'MS Access',
    :opts  => lambda { |opts, options|
      opts.separator("    NOTE: Repeat '-i' for each .mdb file")
    }
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#typeObject

Returns the value of attribute type.



75
76
77
# File 'lib/flattendb/cli.rb', line 75

def type
  @type
end

Class Method Details

.defaultsObject



60
61
62
63
64
65
66
67
# File 'lib/flattendb/cli.rb', line 60

def defaults
  super.merge(
    :input  => '-',
    :inputs => [],
    :output => '-',
    :config => 'config.yaml'
  )
end

.execute(type = nil, *args) ⇒ Object



69
70
71
# File 'lib/flattendb/cli.rb', line 69

def execute(type = nil, *args)
  new(nil, type).execute(*args)
end

Instance Method Details

#run(arguments) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/flattendb/cli.rb', line 77

def run(arguments)
  if type
    require "flattendb/types/#{type}"
  else
    quit 'Database type is required!'
  end

  options[:input] = if type == :mdb
    if options[:inputs].empty?
      if arguments.empty?
        options[:inputs] << options[:input]
      else
        options[:inputs].concat(arguments)
        arguments.clear
      end
    end

    options[:inputs].map! { |file| open_file_or_std(file) }
  else
    open_file_or_std(
      options[:inputs].last || arguments.shift || options[:input]
    )
  end

  quit unless arguments.empty?

  options[:output] = open_file_or_std(options[:output], true)

  FlattenDB[type].to_flat!(options)
end