#!/usr/bin/env ruby require_relative "../lib/nando" require "bundler/setup" require "optparse" options = {} # TODO: review this, error when deploying Gem # gem_spec = Gem::Specification.find_by_name('nando') # TODO: loading Gem specification by name, there might be a better way parser = OptionParser.new do |opt| opt.on("-t", "--type [VALUE]", String) do |type| options[:type] = type end opt.on("-f", "--function [VALUE]", String) do |function| if function.nil? options[:functions_to_add] = NandoInterface.get_user_function_list() else options[:functions_to_add] = [] << function end end opt.on("-d", "--dry-run", "Pass value") do |bool| options[:dry_run] = true end opt.on("-h", "--help", "Show this message") do |value| NandoInterface.print_help_message() exit 0 end end begin parser.parse! action = ARGV.shift() case action when 'up' NandoMigrator.instance.migrate(options) when 'down' NandoMigrator.instance.rollback() when 'apply' if ARGV[0].nil? raise Nando::InputError.new('Insufficient arguments') # ARGV[0] is the migration version to apply end NandoMigrator.instance.apply(options, ARGV) when 'new' if ARGV[0].nil? raise Nando::InputError.new('Insufficient arguments') # ARGV[0] is the new migration name end NandoMigrator.instance.new_migration(options, ARGV) when 'parse' if ARGV[0].nil? || ARGV[1].nil? raise Nando::InputError.new('Insufficient arguments') end NandoMigrator.instance.parse(options, ARGV) when 'baseline' NandoMigrator.instance.baseline() when 'update' if ARGV[0].nil? raise Nando::InputError.new('Insufficient arguments') # ARGV[0] is the new migration file end NandoMigrator.instance.update_migration(options, ARGV) when 'diff' if ARGV[0].nil? || ARGV[1].nil? raise Nando::InputError.new('Insufficient arguments') end NandoMigrator.instance.diff_schemas(options, ARGV) else raise Nando::InputError.new("Invalid command '#{action}'") end rescue OptionParser::InvalidArgument => e _error "#{e.message.capitalize}" NandoInterface.print_help_message() exit 1 rescue Nando::GenericError => e _error "#{e}" exit 1 rescue Nando::InputError => e _error "#{e}" NandoInterface.print_help_message() exit 1 end