From dadd8225da5cb8abaa7246f22a97a8cd7af9665c Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Sun, 18 Jun 2023 23:39:44 +0400 Subject: [PATCH] Various code style improvements --- lib/mrsk/commands/builder.rb | 4 +- lib/mrsk/commands/builder/base.rb | 21 +++++---- lib/mrsk/commands/builder/multiarch/remote.rb | 12 ++--- lib/mrsk/commands/builder/native/remote.rb | 6 +-- lib/mrsk/configuration/builder.rb | 47 +++++++++++-------- test/configuration/builder_test.rb | 6 +-- 6 files changed, 54 insertions(+), 42 deletions(-) diff --git a/lib/mrsk/commands/builder.rb b/lib/mrsk/commands/builder.rb index b100f498..074890ce 100644 --- a/lib/mrsk/commands/builder.rb +++ b/lib/mrsk/commands/builder.rb @@ -7,9 +7,9 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base def target case - when !config.builder.multiarch? && !config.builder.cache? + when !config.builder.multiarch? && !config.builder.cached? native - when !config.builder.multiarch? && config.builder.cache? + when !config.builder.multiarch? && config.builder.cached? native_cached when config.builder.local? && config.builder.remote? multiarch_remote diff --git a/lib/mrsk/commands/builder/base.rb b/lib/mrsk/commands/builder/base.rb index 2e697311..b45cee4d 100644 --- a/lib/mrsk/commands/builder/base.rb +++ b/lib/mrsk/commands/builder/base.rb @@ -3,6 +3,7 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base class BuilderError < StandardError; end delegate :argumentize, to: Mrsk::Utils + delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, to: :builder_config def clean docker :image, :rm, "--force", config.absolute_image @@ -27,9 +28,9 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base end def build_cache - if config.builder.cache? - ["--cache-to", config.builder.cache_to, - "--cache-from", config.builder.cache_from] + if cache_to && cache_from + ["--cache-to", cache_to, + "--cache-from", cache_from] end end @@ -38,18 +39,22 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base end def build_args - argumentize "--build-arg", config.builder.args, sensitive: true + argumentize "--build-arg", args, sensitive: true end def build_secrets - argumentize "--secret", config.builder.secrets.collect { |secret| [ "id", secret ] } + argumentize "--secret", secrets.collect { |secret| [ "id", secret ] } end def build_dockerfile - if Pathname.new(File.expand_path(config.builder.dockerfile)).exist? - argumentize "--file", config.builder.dockerfile + if Pathname.new(File.expand_path(dockerfile)).exist? + argumentize "--file", dockerfile else - raise BuilderError, "Missing #{config.builder.dockerfile}" + raise BuilderError, "Missing #{dockerfile}" end end + + def builder_config + config.builder + end end diff --git a/lib/mrsk/commands/builder/multiarch/remote.rb b/lib/mrsk/commands/builder/multiarch/remote.rb index 2bc566a9..9e27b2f5 100644 --- a/lib/mrsk/commands/builder/multiarch/remote.rb +++ b/lib/mrsk/commands/builder/multiarch/remote.rb @@ -22,17 +22,17 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult end def create_local_buildx - docker :buildx, :create, "--name", builder_name, builder_name_with_arch(config.builder.local_arch), "--platform", "linux/#{config.builder.local_arch}" + docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local_arch), "--platform", "linux/#{local_arch}" end def append_remote_buildx - docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(config.builder.remote_arch), "--platform", "linux/#{config.builder.remote_arch}" + docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(remote_arch), "--platform", "linux/#{remote_arch}" end def create_contexts combine \ - create_context(config.builder.local_arch, config.builder.local_host), - create_context(config.builder.remote_arch, config.builder.remote_host) + create_context(local_arch, local_host), + create_context(remote_arch, remote_host) end def create_context(arch, host) @@ -41,8 +41,8 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult def remove_contexts combine \ - remove_context(config.builder.local_arch), - remove_context(config.builder.remote_arch) + remove_context(local_arch), + remove_context(remote_arch) end def remove_context(arch) diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb index 31c9e0c3..2fc00e0f 100644 --- a/lib/mrsk/commands/builder/native/remote.rb +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -33,16 +33,16 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native end def builder_name_with_arch - "#{builder_name}-#{config.builder.remote_arch}" + "#{builder_name}-#{remote_arch}" end def platform - "linux/#{config.builder.remote_arch}" + "linux/#{remote_arch}" end def create_context docker :context, :create, - builder_name_with_arch, "--description", "'#{builder_name} #{config.builder.remote_arch} native host'", "--docker", "'host=#{config.builder.remote_host}'" + builder_name_with_arch, "--description", "'#{builder_name} #{remote_arch} native host'", "--docker", "'host=#{remote_host}'" end def remove_context diff --git a/lib/mrsk/configuration/builder.rb b/lib/mrsk/configuration/builder.rb index 0cf3a21a..edae8de8 100644 --- a/lib/mrsk/configuration/builder.rb +++ b/lib/mrsk/configuration/builder.rb @@ -23,7 +23,7 @@ class Mrsk::Configuration::Builder !!@options["remote"] end - def cache? + def cached? !!@options["cache"] end @@ -60,39 +60,28 @@ class Mrsk::Configuration::Builder end def cache_from - if cache? + if cached? case @options["cache"]["type"] when 'gha' "type=gha" when 'registry' - [ - "type=registry", - "ref=#{@server}/#{cache_image}" - ].compact.join(",") + cache_from_config_for_registry end end end def cache_to - if cache? + if cached? case @options["cache"]["type"] when "gha" - [ - "type=gha", - @options["cache"]&.fetch("options", nil), - ].compact.join(",") + cache_to_config_for_gha when "registry" - [ - "type=registry", - @options["cache"]&.fetch("options", nil), - "ref=#{@server}/#{cache_image}" - ].compact.join(",") + cache_to_config_for_registry end end end private - def valid? if @options["local"] && !@options["remote"] raise ArgumentError, "You must specify both local and remote builder config for remote multiarch builds" @@ -107,7 +96,25 @@ class Mrsk::Configuration::Builder @options["cache"]&.fetch("image", nil) || "#{@image}-build-cache" end - def current_branch - `git rev-parse --abbrev-ref HEAD`.strip + def cache_from_config_for_registry + [ + "type=registry", + "ref=#{@server}/#{cache_image}" + ].compact.join(",") end -end \ No newline at end of file + + def cache_to_config_for_gha + [ + "type=gha", + @options["cache"]&.fetch("options", nil), + ].compact.join(",") + end + + def cache_to_config_for_registry + [ + "type=registry", + @options["cache"]&.fetch("options", nil), + "ref=#{@server}/#{cache_image}" + ].compact.join(",") + end +end diff --git a/test/configuration/builder_test.rb b/test/configuration/builder_test.rb index 9b17ea7f..1b355187 100644 --- a/test/configuration/builder_test.rb +++ b/test/configuration/builder_test.rb @@ -68,8 +68,8 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", @config_with_builder_option.builder.local_host end - test "cache?" do - assert_equal false, @config.builder.cache? + test "cached?" do + assert_equal false, @config.builder.cached? end test "invalid cache type specified" do @@ -148,4 +148,4 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase assert_equal "..", @config_with_builder_option.builder.context end -end \ No newline at end of file +end