From 7d881c28add6b58ee57191085b5a3a5eb16baa0e Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 13:53:28 +0900 Subject: [PATCH 1/9] Added a way to add subcommand options --- lib/kaiser/cli.rb | 8 +++++++- lib/kaiser/cmds/up.rb | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/kaiser/cli.rb b/lib/kaiser/cli.rb index a95696ad..4adf74b7 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -5,6 +5,12 @@ module Kaiser # The commandline class Cli + attr_reader :opts + + def initialize + @opts = [] + end + 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 +49,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.opts) # 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 diff --git a/lib/kaiser/cmds/up.rb b/lib/kaiser/cmds/up.rb index f4bfde43..f4d3db65 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -3,6 +3,12 @@ module Kaiser module Cmds class Up < Cli + def initialize + @opts = [ + [:attach, "Bind mount the current source code directory in the app container", short: '-a'] + ] + end + 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. From cb22228028d5b3883aa483e56ca062bce2a58cb1 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 14:51:07 +0900 Subject: [PATCH 2/9] Provided a little syntax sugar for setting options in Cli subclasses. --- lib/kaiser/cli.rb | 21 ++++++++++++++++++--- lib/kaiser/cmds/up.rb | 6 +----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/kaiser/cli.rb b/lib/kaiser/cli.rb index 4adf74b7..ad2e1329 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -5,11 +5,25 @@ module Kaiser # The commandline class Cli - attr_reader :opts + @opts = [] - def initialize - @opts = [] + class << self + def self.option(*opt) + @opts ||= [] + @opts << opt + end + alias :opt :option + + def self.options + @opts + end + alias :opts :options + end + + def options + self.class.options || [] end + alias :opts :options def set_config # This is here for backwards compatibility since it can be used in Kaiserfiles. @@ -49,6 +63,7 @@ def self.register(name, klass) def self.run_command(name, global_opts) cmd = @subcommands[name] + p cmd.opts opts = cmd.define_options(global_opts + cmd.opts) # The define_options method has stripped all arguments from the cli so now diff --git a/lib/kaiser/cmds/up.rb b/lib/kaiser/cmds/up.rb index f4d3db65..7264a190 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -3,11 +3,7 @@ module Kaiser module Cmds class Up < Cli - def initialize - @opts = [ - [:attach, "Bind mount the current source code directory in the app container", short: '-a'] - ] - end + option :attach, "Bind mount the current source code directory in the app container", short: '-a' def usage <<~EOS From a6175315d403963b841408d953647c7a9d37fef8 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 15:21:24 +0900 Subject: [PATCH 3/9] Made it so that the opts hash is available in the Cli subclass instances. Also got rid of the alsiases because it's confusion there's bot an instance variable and a class instance variable called opts --- lib/kaiser/cli.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/kaiser/cli.rb b/lib/kaiser/cli.rb index ad2e1329..e4674e8e 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -5,25 +5,20 @@ module Kaiser # The commandline class Cli - @opts = [] + @options = [] class << self - def self.option(*opt) - @opts ||= [] - @opts << opt + def option(*option) + @options ||= [] + @options << option end - alias :opt :option - def self.options - @opts + def options + @options || [] end - alias :opts :options end - def options - self.class.options || [] - end - alias :opts :options + attr_reader :opts def set_config # This is here for backwards compatibility since it can be used in Kaiserfiles. @@ -49,7 +44,7 @@ def define_options(global_opts = []) # the scope to Optimist::Parser. We can still reference variables but we can't # call instance methods of a Kaiser::Cli class. u = usage - Optimist.options do + @opts = Optimist.options do banner u global_opts.each { |o| opt *o } @@ -63,8 +58,7 @@ def self.register(name, klass) def self.run_command(name, global_opts) cmd = @subcommands[name] - p cmd.opts - opts = cmd.define_options(global_opts + cmd.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 @@ -78,7 +72,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') From 0deceb3acc6c2812d95442accbb98720a4e80a93 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 15:44:28 +0900 Subject: [PATCH 4/9] Implemented the option to attach when using kaiser up --- lib/kaiser/cli.rb | 23 +++++++++++++++++++++++ lib/kaiser/cmds/attach.rb | 24 +----------------------- lib/kaiser/cmds/up.rb | 7 ++++++- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/lib/kaiser/cli.rb b/lib/kaiser/cli.rb index e4674e8e..c588e3b4 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -230,6 +230,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/cmds/attach.rb b/lib/kaiser/cmds/attach.rb index e7720f05..65f3f098 100644 --- a/lib/kaiser/cmds/attach.rb +++ b/lib/kaiser/cmds/attach.rb @@ -13,31 +13,9 @@ def usage def execute 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/up.rb b/lib/kaiser/cmds/up.rb index 7264a190..d74eb185 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -19,7 +19,12 @@ def execute ensure_setup setup_app setup_db - start_app + + if opts[:attach] + attach_app + else + start_app + end end def setup_app From ed64c299f0a63264c314128ee83c25eb7ed316b5 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 15:48:46 +0900 Subject: [PATCH 5/9] Some quick documentation updates. --- lib/kaiser/cmds/attach.rb | 2 ++ lib/kaiser/cmds/up.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kaiser/cmds/attach.rb b/lib/kaiser/cmds/attach.rb index 65f3f098..622df151 100644 --- a/lib/kaiser/cmds/attach.rb +++ b/lib/kaiser/cmds/attach.rb @@ -7,6 +7,8 @@ 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 diff --git a/lib/kaiser/cmds/up.rb b/lib/kaiser/cmds/up.rb index d74eb185..09fd1e1c 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -3,7 +3,7 @@ module Kaiser module Cmds class Up < Cli - option :attach, "Bind mount the current source code directory in the app container", short: '-a' + option :attach, "Bind mount the current source code directory in the app container (as the \`kaiser attach\` command would)", short: '-a' def usage <<~EOS From 03e2d943c0186591fba21ecf0aaef41c142ccd72 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 15:50:32 +0900 Subject: [PATCH 6/9] Version bump --- lib/kaiser/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 7089eea70d5273f7594a14a4a731101638f3c068 Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 16:07:16 +0900 Subject: [PATCH 7/9] Added the version bump to the Gemfile.lock as well --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 62175ac4013a830e6cfdb81fd4c643a2290a349e Mon Sep 17 00:00:00 2001 From: Metallion Date: Wed, 9 Oct 2019 16:22:27 +0900 Subject: [PATCH 8/9] Fixed failing tests. --- spec/kaiser_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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", ' ') From fa7efb41516fad8ce180dc088a22482452427cc0 Mon Sep 17 00:00:00 2001 From: Metallion Date: Thu, 10 Oct 2019 17:10:13 +0900 Subject: [PATCH 9/9] Moved the CLI options to their own module and pass them into execute instead of setting an instance variable --- lib/kaiser.rb | 1 + lib/kaiser/cli.rb | 19 +++---------------- lib/kaiser/cli_options.rb | 14 ++++++++++++++ lib/kaiser/cmds/attach.rb | 2 +- lib/kaiser/cmds/db_load.rb | 2 +- lib/kaiser/cmds/db_reset.rb | 2 +- lib/kaiser/cmds/db_reset_hard.rb | 2 +- lib/kaiser/cmds/db_save.rb | 2 +- lib/kaiser/cmds/deinit.rb | 2 +- lib/kaiser/cmds/down.rb | 2 +- lib/kaiser/cmds/init.rb | 2 +- lib/kaiser/cmds/login.rb | 2 +- lib/kaiser/cmds/logs.rb | 2 +- lib/kaiser/cmds/set.rb | 2 +- lib/kaiser/cmds/show.rb | 2 +- lib/kaiser/cmds/shutdown.rb | 2 +- lib/kaiser/cmds/up.rb | 2 +- 17 files changed, 32 insertions(+), 30 deletions(-) create mode 100644 lib/kaiser/cli_options.rb 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 c588e3b4..a3c687b6 100644 --- a/lib/kaiser/cli.rb +++ b/lib/kaiser/cli.rb @@ -5,20 +5,7 @@ module Kaiser # The commandline class Cli - @options = [] - - class << self - def option(*option) - @options ||= [] - @options << option - end - - def options - @options || [] - end - end - - attr_reader :opts + extend Kaiser::CliOptions def set_config # This is here for backwards compatibility since it can be used in Kaiserfiles. @@ -44,7 +31,7 @@ def define_options(global_opts = []) # the scope to Optimist::Parser. We can still reference variables but we can't # call instance methods of a Kaiser::Cli class. u = usage - @opts = Optimist.options do + Optimist.options do banner u global_opts.each { |o| opt *o } @@ -86,7 +73,7 @@ def self.run_command(name, global_opts) ) cmd.set_config - cmd.execute + cmd.execute(opts) end def self.all_subcommands_usage 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 622df151..a2846106 100644 --- a/lib/kaiser/cmds/attach.rb +++ b/lib/kaiser/cmds/attach.rb @@ -13,7 +13,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup attach_app start_app 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 09fd1e1c..a80d4049 100644 --- a/lib/kaiser/cmds/up.rb +++ b/lib/kaiser/cmds/up.rb @@ -15,7 +15,7 @@ def usage EOS end - def execute + def execute(opts) ensure_setup setup_app setup_db