diff --git a/Gemfile.lock b/Gemfile.lock index b913a64b..cdb7e0a6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - kaiser (0.4.0) + kaiser (0.4.1) optimist GEM diff --git a/lib/kaiser.rb b/lib/kaiser.rb index 5bdd9278..0ac9e150 100644 --- a/lib/kaiser.rb +++ b/lib/kaiser.rb @@ -2,6 +2,7 @@ require 'kaiser/version' require 'kaiser/kaiserfile' +require 'kaiser/cli_options' require 'kaiser/cli' require 'kaiser/after_dotter' require 'kaiser/dotter' diff --git a/lib/kaiser/cli.rb b/lib/kaiser/cli.rb index a95696ad..a3c687b6 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -5,6 +5,8 @@ module Kaiser # The commandline class Cli + extend Kaiser::CliOptions + def set_config # This is here for backwards compatibility since it can be used in Kaiserfiles. # It would be a good idea to deprecate this and make it more abstract. @@ -43,7 +45,7 @@ def self.register(name, klass) def self.run_command(name, global_opts) cmd = @subcommands[name] - opts = cmd.define_options(global_opts) + opts = cmd.define_options(global_opts + cmd.class.options) # The define_options method has stripped all arguments from the cli so now # all that we're left with in ARGV are the subcommand to be run and possibly @@ -57,7 +59,6 @@ def self.run_command(name, global_opts) # unless they create a Kaiserfile firest. out = Kaiser::Dotter.new info_out = Kaiser::AfterDotter.new(dotter: out) - if opts[:quiet] out = File.open(File::NULL, 'w') info_out = File.open(File::NULL, 'w') @@ -72,7 +73,7 @@ def self.run_command(name, global_opts) ) cmd.set_config - cmd.execute + cmd.execute(opts) end def self.all_subcommands_usage @@ -216,6 +217,29 @@ def default_db_image db_image_path('.default') end + def attach_app + cmd = (ARGV || []).join(' ') + killrm app_container_name + + attach_mounts = Config.kaiserfile.attach_mounts + volumes = attach_mounts.map { |from, to| "-v #{`pwd`.chomp}/#{from}:#{to}" }.join(' ') + + system "docker run -ti + --name #{app_container_name} + --network #{network_name} + --dns #{ip_of_container(Config.config[:shared_names][:dns])} + --dns-search #{http_suffix} + -p #{app_port}:#{app_expose} + -e DEV_APPLICATION_HOST=#{envname}.#{http_suffix} + -e VIRTUAL_HOST=#{envname}.#{http_suffix} + -e VIRTUAL_PORT=#{app_expose} + #{volumes} + #{app_params} + kaiser:#{envname}-#{current_branch} #{cmd}".tr("\n", ' ') + + Config.out.puts 'Cleaning up...' + end + def start_app Config.info_out.puts 'Starting up application' killrm app_container_name diff --git a/lib/kaiser/cli_options.rb b/lib/kaiser/cli_options.rb new file mode 100644 index 00000000..8c1b9cb7 --- /dev/null +++ b/lib/kaiser/cli_options.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Kaiser + module CliOptions + def option(*option) + @options ||= [] + @options << option + end + + def options + @options || [] + end + end +end diff --git a/lib/kaiser/cmds/attach.rb b/lib/kaiser/cmds/attach.rb index e7720f05..a2846106 100644 --- a/lib/kaiser/cmds/attach.rb +++ b/lib/kaiser/cmds/attach.rb @@ -7,37 +7,17 @@ def usage <<~EOS Shuts down the application container and starts it up again with the current directory bind mounted inside. This way the application will run from the source code in the current directory and any edits you make will immediately show up inside the container. This is ideal for development. + Once the attached container exits (through the use of control+c for example) it will be replaced by a regular non-attached version of the app container. + USAGE: kaiser attach EOS end - def execute + def execute(opts) ensure_setup - cmd = (ARGV || []).join(' ') - killrm app_container_name - - volumes = attach_mounts.map { |from, to| "-v #{`pwd`.chomp}/#{from}:#{to}" }.join(' ') - - system "docker run -ti - --name #{app_container_name} - --network #{network_name} - --dns #{ip_of_container(Config.config[:shared_names][:dns])} - --dns-search #{http_suffix} - -p #{app_port}:#{app_expose} - -e DEV_APPLICATION_HOST=#{envname}.#{http_suffix} - -e VIRTUAL_HOST=#{envname}.#{http_suffix} - -e VIRTUAL_PORT=#{app_expose} - #{volumes} - #{app_params} - kaiser:#{envname}-#{current_branch} #{cmd}".tr("\n", ' ') - - Config.out.puts 'Cleaning up...' + attach_app start_app end - - def attach_mounts - Config.kaiserfile.attach_mounts - end end end end diff --git a/lib/kaiser/cmds/db_load.rb b/lib/kaiser/cmds/db_load.rb index 9c23de13..73e81007 100644 --- a/lib/kaiser/cmds/db_load.rb +++ b/lib/kaiser/cmds/db_load.rb @@ -19,7 +19,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup name = ARGV.shift || '.default' load_db(name) diff --git a/lib/kaiser/cmds/db_reset.rb b/lib/kaiser/cmds/db_reset.rb index 7d54815c..b726217f 100644 --- a/lib/kaiser/cmds/db_reset.rb +++ b/lib/kaiser/cmds/db_reset.rb @@ -13,7 +13,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup load_db('.default') end diff --git a/lib/kaiser/cmds/db_reset_hard.rb b/lib/kaiser/cmds/db_reset_hard.rb index 5aee5c85..bf1d49ad 100644 --- a/lib/kaiser/cmds/db_reset_hard.rb +++ b/lib/kaiser/cmds/db_reset_hard.rb @@ -11,7 +11,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup FileUtils.rm db_image_path('.default') if File.exist?(db_image_path('.default')) setup_db diff --git a/lib/kaiser/cmds/db_save.rb b/lib/kaiser/cmds/db_save.rb index 255af69b..37652aa5 100644 --- a/lib/kaiser/cmds/db_save.rb +++ b/lib/kaiser/cmds/db_save.rb @@ -16,7 +16,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup name = ARGV.shift || '.default' save_db(name) diff --git a/lib/kaiser/cmds/deinit.rb b/lib/kaiser/cmds/deinit.rb index 91ee4da1..b706019a 100644 --- a/lib/kaiser/cmds/deinit.rb +++ b/lib/kaiser/cmds/deinit.rb @@ -11,7 +11,7 @@ def usage EOS end - def execute + def execute(opts) down Config.config[:envs].delete(envname) Config.config[:envnames].delete(Config.work_dir) diff --git a/lib/kaiser/cmds/down.rb b/lib/kaiser/cmds/down.rb index 460f870a..43564741 100644 --- a/lib/kaiser/cmds/down.rb +++ b/lib/kaiser/cmds/down.rb @@ -11,7 +11,7 @@ def usage EOS end - def execute + def execute(opts) down end end diff --git a/lib/kaiser/cmds/init.rb b/lib/kaiser/cmds/init.rb index 8b29f5a7..6dff4a4f 100644 --- a/lib/kaiser/cmds/init.rb +++ b/lib/kaiser/cmds/init.rb @@ -12,7 +12,7 @@ def usage EOS end - def execute + def execute(opts) return Optimist.die "Already initialized as #{envname}" if envname name = ARGV.shift diff --git a/lib/kaiser/cmds/login.rb b/lib/kaiser/cmds/login.rb index d2f1be93..46e2a348 100644 --- a/lib/kaiser/cmds/login.rb +++ b/lib/kaiser/cmds/login.rb @@ -11,7 +11,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup cmd = (ARGV || []).join(' ') exec "docker exec -ti #{app_container_name} #{cmd}" diff --git a/lib/kaiser/cmds/logs.rb b/lib/kaiser/cmds/logs.rb index 120ff625..66f6a045 100644 --- a/lib/kaiser/cmds/logs.rb +++ b/lib/kaiser/cmds/logs.rb @@ -11,7 +11,7 @@ def usage EOS end - def execute + def execute(opts) exec "docker logs -f #{app_container_name}" end end diff --git a/lib/kaiser/cmds/set.rb b/lib/kaiser/cmds/set.rb index 92a6a112..1430f0b7 100644 --- a/lib/kaiser/cmds/set.rb +++ b/lib/kaiser/cmds/set.rb @@ -21,7 +21,7 @@ def usage EOS end - def execute + def execute(opts) cmd = ARGV.shift if cmd == 'cert-url' Config.config[:cert_source] = { diff --git a/lib/kaiser/cmds/show.rb b/lib/kaiser/cmds/show.rb index 21b6940c..aa213d7d 100644 --- a/lib/kaiser/cmds/show.rb +++ b/lib/kaiser/cmds/show.rb @@ -13,7 +13,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup cmd = ARGV.shift valid_cmds = 'ports cert-source http-suffix' diff --git a/lib/kaiser/cmds/shutdown.rb b/lib/kaiser/cmds/shutdown.rb index 110ac08a..80bcc0cf 100644 --- a/lib/kaiser/cmds/shutdown.rb +++ b/lib/kaiser/cmds/shutdown.rb @@ -13,7 +13,7 @@ def usage EOS end - def execute + def execute(opts) Config.config[:shared_names].each do |_, container_name| killrm container_name end diff --git a/lib/kaiser/cmds/up.rb b/lib/kaiser/cmds/up.rb index f4bfde43..a80d4049 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -3,6 +3,8 @@ module Kaiser module Cmds class Up < Cli + option :attach, "Bind mount the current source code directory in the app container (as the \`kaiser attach\` command would)", short: '-a' + def usage <<~EOS Boots up the application in docker as defined in the \`Kaiserfile\` in its source code. Usually this will create two docker containers \`-db\` and \`-app\` running your database and application respectively. @@ -13,11 +15,16 @@ def usage EOS end - def execute + def execute(opts) ensure_setup setup_app setup_db - start_app + + if opts[:attach] + attach_app + else + start_app + end end def setup_app diff --git a/lib/kaiser/version.rb b/lib/kaiser/version.rb index 8e985996..39921b72 100644 --- a/lib/kaiser/version.rb +++ b/lib/kaiser/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Kaiser - VERSION = '0.4.0' + VERSION = '0.4.1' end diff --git a/spec/kaiser_spec.rb b/spec/kaiser_spec.rb index 1195e3cf..ae39a4c6 100644 --- a/spec/kaiser_spec.rb +++ b/spec/kaiser_spec.rb @@ -61,7 +61,11 @@ shared_examples 'single help' do it "prints the help message for just #{name} and nothing else" do # Remove the help arguments generated by Optimist as they won't be in the usage method - unwrapped_output = cmd_stdout.lines.delete_if { |l| l =~ /^ -.+/ }.join + # We remove each line after the first argument. We can't just check for lines that start + # with ' -' because optimist might wrap them over multiple lines. + lines = cmd_stdout.lines + args_line = lines.index { |l| l =~ /^ -.+/ } + unwrapped_output = lines[0, args_line].join # Remove all newlines because Optimist will wordwrap according to terminal size unwrapped_output.gsub!("\n", ' ')