From d3ab10be22836d92dc570a2cf305cbf37c253b10 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 12 Jan 2023 14:57:34 +0100 Subject: [PATCH] Better require setup --- lib/mrsk/commands.rb | 24 ------------------------ lib/mrsk/commands/app.rb | 2 ++ lib/mrsk/commands/base.rb | 21 +++++++++++++++++++++ lib/mrsk/commands/prune.rb | 4 ++++ lib/mrsk/commands/registry.rb | 2 ++ lib/mrsk/commands/traefik.rb | 2 ++ 6 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 lib/mrsk/commands/base.rb diff --git a/lib/mrsk/commands.rb b/lib/mrsk/commands.rb index 3e46ced3..5551c854 100644 --- a/lib/mrsk/commands.rb +++ b/lib/mrsk/commands.rb @@ -1,26 +1,2 @@ -require "sshkit" - module Mrsk::Commands - class Base - attr_accessor :config - - def initialize(config) - @config = config - end - - private - def docker(*args) - args.compact.unshift :docker - end - - # Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes - def redact(arg) # Used in execute_command to hide redact() args a user passes in - arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc - end - end end - -require "mrsk/commands/app" -require "mrsk/commands/prune" -require "mrsk/commands/traefik" -require "mrsk/commands/registry" diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 9aa13e8a..082930b8 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -1,3 +1,5 @@ +require "mrsk/commands/base" + class Mrsk::Commands::App < Mrsk::Commands::Base def push docker :buildx, :build, "--push", "--platform linux/amd64,linux/arm64", "-t", config.absolute_image, "." diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb new file mode 100644 index 00000000..8cf0879a --- /dev/null +++ b/lib/mrsk/commands/base.rb @@ -0,0 +1,21 @@ +require "sshkit" + +module Mrsk::Commands + class Base + attr_accessor :config + + def initialize(config) + @config = config + end + + private + def docker(*args) + args.compact.unshift :docker + end + + # Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes + def redact(arg) # Used in execute_command to hide redact() args a user passes in + arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc + end + end +end diff --git a/lib/mrsk/commands/prune.rb b/lib/mrsk/commands/prune.rb index 4a181deb..19451b67 100644 --- a/lib/mrsk/commands/prune.rb +++ b/lib/mrsk/commands/prune.rb @@ -1,3 +1,7 @@ +require "mrsk/commands/base" +require "active_support/duration" +require "active_support/core_ext/numeric/time" + class Mrsk::Commands::Prune < Mrsk::Commands::Base PRUNE_IMAGES_AFTER = 30.days.in_hours.to_i PRUNE_CONTAINERS_AFTER = 3.days.in_hours.to_i diff --git a/lib/mrsk/commands/registry.rb b/lib/mrsk/commands/registry.rb index 4e9b1ce4..8d05b0cb 100644 --- a/lib/mrsk/commands/registry.rb +++ b/lib/mrsk/commands/registry.rb @@ -1,3 +1,5 @@ +require "mrsk/commands/base" + class Mrsk::Commands::Registry < Mrsk::Commands::Base delegate :registry, to: :config diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 5512341a..c2bdef4e 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -1,3 +1,5 @@ +require "mrsk/commands/base" + class Mrsk::Commands::Traefik < Mrsk::Commands::Base def run docker :run, "--name traefik",