Improve clarity and intent

This commit is contained in:
David Heinemeier Hansson
2023-05-02 14:04:23 +02:00
parent f719540e0c
commit ae2effb80c
4 changed files with 22 additions and 22 deletions

View File

@@ -16,7 +16,7 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
run_locally do run_locally do
begin begin
if cli.dependencies if cli.verify_dependencies
MRSK.with_verbosity(:debug) { execute *MRSK.builder.push } MRSK.with_verbosity(:debug) { execute *MRSK.builder.push }
end end
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
@@ -82,15 +82,17 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
end end
end end
desc "dependencies", "Check local dependencies"
def dependencies desc "", "" # Really a private method, but needed to be invoked from #push
def verify_dependencies
run_locally do run_locally do
begin begin
execute *MRSK.builder.dependencies execute *MRSK.builder.ensure_dependencies_installed
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
build_error = e.message =~ /command not found/ ? build_error = e.message =~ /command not found/ ?
"Docker is not installed locally" : "Docker is not installed locally" :
"Docker buildx plugin is not installed locally" "Docker buildx plugin is not installed locally"
raise BuildError, build_error raise BuildError, build_error
end end
end end

View File

@@ -2,7 +2,7 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, to: :target delegate :create, :remove, :push, :clean, :pull, :info, to: :target
def name def name
target.class.to_s.remove("Mrsk::Commands::Builder::").underscore target.class.to_s.remove("Mrsk::Commands::Builder::").underscore.inquiry
end end
def target def target
@@ -34,26 +34,23 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
@multiarch_remote ||= Mrsk::Commands::Builder::Multiarch::Remote.new(config) @multiarch_remote ||= Mrsk::Commands::Builder::Multiarch::Remote.new(config)
end end
def native_and_local?
name == 'native'
end
def dependencies def ensure_dependencies_installed
if native_and_local? if name.native?
docker_version ensure_docker_installed
else else
combine \ combine \
docker_version, ensure_docker_installed,
docker_buildx_version ensure_buildx_installed
end end
end end
private private
def docker_version def ensure_docker_installed
docker "--version" docker "--version"
end end
def docker_buildx_version def ensure_buildx_installed
docker :buildx, "version" docker :buildx, "version"
end end
end end

View File

@@ -9,7 +9,7 @@ class CliBuildTest < CliTestCase
end end
test "push" do test "push" do
Mrsk::Cli::Build.any_instance.stubs(:dependencies).returns(true) Mrsk::Cli::Build.any_instance.stubs(:verify_dependencies).returns(true)
run_command("push").tap do |output| run_command("push").tap do |output|
assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder mrsk-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*@localhost/, output assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder mrsk-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*@localhost/, output
end end
@@ -17,7 +17,7 @@ class CliBuildTest < CliTestCase
test "push without builder" do test "push without builder" do
stub_locking stub_locking
Mrsk::Cli::Build.any_instance.stubs(:dependencies).returns(true) Mrsk::Cli::Build.any_instance.stubs(:verify_dependencies).returns(true)
SSHKit::Backend::Abstract.any_instance.stubs(:execute) SSHKit::Backend::Abstract.any_instance.stubs(:execute)
.with { |arg| arg == :docker } .with { |arg| arg == :docker }
.raises(SSHKit::Command::Failed.new("no builder")) .raises(SSHKit::Command::Failed.new("no builder"))
@@ -70,9 +70,10 @@ class CliBuildTest < CliTestCase
end end
end end
test "dependencies" do test "verify dependencies" do
Mrsk::Commands::Builder.any_instance.stubs(:native_and_local?).returns(false) Mrsk::Commands::Builder.any_instance.stubs(:name).returns("remote".inquiry)
run_command("dependencies").tap do |output|
run_command("verify_dependencies").tap do |output|
assert_match /docker --version && docker buildx version/, output assert_match /docker --version && docker buildx version/, output
end end
end end
@@ -83,7 +84,7 @@ class CliBuildTest < CliTestCase
.raises(SSHKit::Command::Failed.new("no buildx")) .raises(SSHKit::Command::Failed.new("no buildx"))
Mrsk::Commands::Builder.any_instance.stubs(:native_and_local?).returns(false) Mrsk::Commands::Builder.any_instance.stubs(:native_and_local?).returns(false)
assert_raises(Mrsk::Cli::Build::BuildError) { run_command("dependencies") } assert_raises(Mrsk::Cli::Build::BuildError) { run_command("verify_dependencies") }
end end
private private

View File

@@ -48,7 +48,7 @@ class ConfigurationTest < ActiveSupport::TestCase
end end
test "role" do test "role" do
assert_equal "web", @config.role(:web).name assert @config.role(:web).name.web?
assert_equal "workers", @config_with_roles.role(:workers).name assert_equal "workers", @config_with_roles.role(:workers).name
assert_nil @config.role(:missing) assert_nil @config.role(:missing)
end end