#!/usr/bin/env ruby require 'gli' require 'nib' include GLI::App program_desc 'A docker-compose wrapper geared towards Ruby/Rails development' version Nib::VERSION subcommand_option_handling :normal arguments :strict autocomplete_commands false preserve_argv true GLI::Commands::Help.skips_post = false flag( %i(f file), arg_name: 'FILE', desc: 'Specify an alternate compose file', default_value: 'docker-compose.yml' ) 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 true end post do |global, command, options, args| # Post logic here # Use skips_post before a command to skip this # block on that command only if command.is_a? GLI::Commands::Help Nib::UnrecognizedHelp.execute(options, args) Nib::CheckForUpdate.execute(options, args) end end # return false to skip default error handling on_error do |exception| case exception when GLI::UnknownCommand # delegate unknown commands over to `docker-compose` exec("docker-compose #{ARGV.join(' ')}") false else true end end desc 'Run bundle for the given service' arg :service arg :command, %i(optional multiple) command :bundle do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :bundle), '--no-deps') end end desc 'Run codeclimate checks on the given service' arg :service arg :command, %i(optional multiple) command :codeclimate do |c| c.action do |global, options, args| Nib::CodeClimate.execute(args, '--no-deps') end end desc 'Start a REPL session for the given service' long_desc 'This command will try to detect the applications environment (ie. rails console or bundle console) and start the most appropriate REPL possible. It\'s possible to directly override this behaviour by supplying an executable file at $pwd/bin/console.' arg :service arg :command, %i(optional multiple) command :console do |c| c.action do |global, options, args| Nib::Console.execute(args) end end desc 'Connect to a running byebug server for a given service' long_desc 'This command requires a little extra setup. - The byebug server must be running inside of a web service - The RUBY_DEBUG_PORT must be defined in docker-compose.yml for the service - The debug port must be exposed by the service' arg :service command :debug do |c| c.action do |global, options, args| Nib::Debug.execute(args, '--no-deps') end end desc 'Attach an interactive shell session to a running container' arg :service arg :command, %i(optional multiple) command :exec do |c| Nib::Options::Augmenter.augment(c) c.action do |global, options, args| Nib::Exec.execute(args, Nib::Options::Parser.parse(options)) end end desc 'Run the guard command for the given service' arg :service arg :command, %i(optional multiple) command :guard do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :guard)) end end desc 'Run the rails command for the given service' arg :service arg :command, %i(optional multiple) command :rails do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :rails)) end end desc 'Run the rake command for the given service' arg :service arg :command, %i(optional multiple) command :rake do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :rake)) end end desc 'Runs the rspec command for the given service' arg :service arg :command, %i(optional multiple) command :rspec do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :rspec)) end end desc 'Runs the rubocop command for the given service' arg :service arg :command, %i(optional multiple) command :rubocop do |c| c.action do |global, options, args| Nib::Run.execute(args.insert(1, :"rubocop -D"), '--no-deps') end end desc 'Wraps normal \'docker-compose run\' to ensure that --rm is always passed' arg :service arg :command, %i(optional multiple) command :run do |c| Nib::Options::Augmenter.augment(c) c.action do |global, options, args| # work around an `OptionParser` limitation: https://git.io/vDKrh options[:'no-deps'] = options['no-deps'] = ARGV.include?('--no-deps') Nib::Run.execute(args, Nib::Options::Parser.parse(options)) end end desc 'Runs application specific setup for the given service' long_desc 'By default the setup command will execute \'gem install bundler && bundle install\'. This behavior can be overriden by providing an executable file in the project at $pwd/bin/setup. Additionally the setup process can be augmented by providing either $pwd/bin/setup.before or $pwd/bin/setup.after. This allows for extending the default behavior without having to redefine it.' arg :service command :setup do |c| c.action do |global, options, args| Nib::Setup.execute(args) end end desc 'Start a shell session in a one-off service container' arg :service arg :command, %i(optional multiple) command :shell do |c| c.action do |global, options, args| Nib::Shell.execute(args) end end exit run(ARGV)