#!/usr/bin/env ruby require 'gli' require 'mince_migrator/version' require 'mince_migrator/list' require 'mince_migrator/cli_helper' include MinceMigrator::CliHelper module MinceMigrator class CliApp include GLI::App end end mince_migrator_cli_app = MinceMigrator::CliApp.new mince_migrator_cli_app.program_desc 'Manages database migrations for ruby applications' mince_migrator_cli_app.version MinceMigrator.version mince_migrator_cli_app.desc 'Creates a migration' mince_migrator_cli_app.arg_name 'Name of migration' mince_migrator_cli_app.command :create do |c| c.action do |global_options,options,args| name = args.join(" ") create_migration(name) end end mince_migrator_cli_app.desc 'Lists all migrations and their statuses' mince_migrator_cli_app.command :list do |c| c.action do |global_options,options,args| list_migrations(MinceMigrator::List.new) end end mince_migrator_cli_app.desc 'Reverts a migration' mince_migrator_cli_app.arg_name 'Name of migration' mince_migrator_cli_app.command :revert do |c| c.action do |global_options,options,args| name = args.join(" ") revert_migration(name: name) end end mince_migrator_cli_app.desc 'Deletes all migrations that have already ran' mince_migrator_cli_app.command :delete_ran do |c| c.action do |global_options,options,args| list = MinceMigrator::List.new('ran') if list.all.any? list.all.each do |migration| delete_migration(migration: migration) end else puts "No migrations were found." end end end mince_migrator_cli_app.desc 'Deletes a migration' mince_migrator_cli_app.arg_name 'Name of migration' mince_migrator_cli_app.command :delete do |c| c.action do |global_options,options,args| name = args.join(" ") delete_migration(name: name) end end mince_migrator_cli_app.desc 'Runs a migration' mince_migrator_cli_app.arg_name 'Name of migration' mince_migrator_cli_app.command :run do |c| c.action do |global_options,options,args| name = args.join(" ") run_migration(name: name) end end mince_migrator_cli_app.desc 'Runs all migrations that have not yet been ran' mince_migrator_cli_app.command :run_all do |c| c.action do list = MinceMigrator::List.new('not ran') if list.all.any? list.all.each do |migration| run_migration(migration: migration) end else puts "There were no migrations to run." end end end mince_migrator_cli_app.desc 'Shows the details of a migration' mince_migrator_cli_app.arg_name 'Name of migration' mince_migrator_cli_app.command :show do |c| c.action do |global_options,options,args| name = args.join(" ") if args.any? show_migration(name) else puts "You must provide a name" end end end mince_migrator_cli_app.pre do |global,command,options,args| # Pre logic here # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only if File.exists?(MinceMigrator::Config.config_file) require MinceMigrator::Config.config_file end true end mince_migrator_cli_app.post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end mince_migrator_cli_app.on_error do |exception| # Error logic here # return false to skip default error handling puts exception puts exception.backtrace.join("\n") false end exit mince_migrator_cli_app.run(ARGV)