Add Rubocop

- Pull in the 37signals house style
- Autofix violations
- Add to CI
This commit is contained in:
Donal McBreen
2024-03-05 11:40:08 +00:00
parent c985fa33d1
commit 3ecfb3744f
51 changed files with 229 additions and 164 deletions

View File

@@ -177,7 +177,7 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
if name == "all"
KAMAL.accessory_names.each { |accessory_name| remove(accessory_name) }
else
if options[:confirmed] || ask("This will remove all containers, images and data directories for #{name}. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
if options[:confirmed] || ask("This will remove all containers, images and data directories for #{name}. Are you sure?", limited_to: %w[ y N ], default: "N") == "y"
with_accessory(name) do
stop(name)
remove_container(name)

View File

@@ -207,7 +207,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
run_locally do
info "Following logs on #{KAMAL.primary_host}..."
KAMAL.specific_roles ||= ["web"]
KAMAL.specific_roles ||= [ "web" ]
role = KAMAL.roles_on(KAMAL.primary_host).first
info KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)

View File

@@ -73,7 +73,7 @@ module Kamal::Cli
def print_runtime
started_at = Time.now
yield
return Time.now - started_at
Time.now - started_at
ensure
runtime = Time.now - started_at
puts " Finished all in #{sprintf("%.1f seconds", runtime)}"
@@ -181,5 +181,5 @@ module Kamal::Cli
execute(*KAMAL.server.ensure_run_directory)
end
end
end
end
end

View File

@@ -197,7 +197,7 @@ class Kamal::Cli::Main < Kamal::Cli::Base
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
def remove
mutating do
if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
if options[:confirmed] || ask("This will remove all containers and images. Are you sure?", limited_to: %w[ y N ], default: "N") == "y"
invoke "kamal:cli:traefik:remove", [], options.without(:confirmed)
invoke "kamal:cli:app:remove", [], options.without(:confirmed)
invoke "kamal:cli:accessory:remove", [ "all" ], options

View File

@@ -13,7 +13,7 @@ class Kamal::Cli::Traefik < Kamal::Cli::Base
option :rolling, type: :boolean, default: false, desc: "Reboot traefik on hosts in sequence, rather than in parallel"
def reboot
mutating do
host_groups = options[:rolling] ? KAMAL.traefik_hosts : [KAMAL.traefik_hosts]
host_groups = options[:rolling] ? KAMAL.traefik_hosts : [ KAMAL.traefik_hosts ]
host_groups.each do |hosts|
host_list = Array(hosts).join(",")
run_hook "pre-traefik-reboot", hosts: host_list

View File

@@ -103,7 +103,7 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base
end
def remove_env_file
[:rm, "-f", accessory_config.host_env_file_path]
[ :rm, "-f", accessory_config.host_env_file_path ]
end
private

View File

@@ -15,7 +15,7 @@ class Kamal::Commands::App < Kamal::Commands::Base
"--detach",
"--restart unless-stopped",
"--name", container_name,
*(["--hostname", hostname] if hostname),
*([ "--hostname", hostname ] if hostname),
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
"-e", "KAMAL_VERSION=\"#{config.version}\"",
*role.env_args,

View File

@@ -4,7 +4,7 @@ module Kamal::Commands::App::Assets
combine \
make_directory(role.asset_extracted_path),
[*docker(:stop, "-t 1", asset_container, "2> /dev/null"), "|| true"],
[ *docker(:stop, "-t 1", asset_container, "2> /dev/null"), "|| true" ],
docker(:run, "--name", asset_container, "--detach", "--rm", config.latest_image, "sleep 1000000"),
docker(:cp, "-L", "#{asset_container}:#{role.asset_path}/.", role.asset_extracted_path),
docker(:stop, "-t 1", asset_container),
@@ -17,7 +17,7 @@ module Kamal::Commands::App::Assets
old_extracted_path, old_volume_path = role.asset_extracted_path(old_version), role.asset_volume(old_version).host_path
end
commands = [make_directory(new_volume_path), copy_contents(new_extracted_path, new_volume_path)]
commands = [ make_directory(new_volume_path), copy_contents(new_extracted_path, new_volume_path) ]
if old_version.present?
commands << copy_contents(new_extracted_path, old_volume_path, continue_on_error: true)
@@ -46,6 +46,6 @@ module Kamal::Commands::App::Assets
end
def copy_contents(source, destination, continue_on_error: false)
[ :cp, "-rnT", "#{source}", destination, *("|| true" if continue_on_error)]
[ :cp, "-rnT", "#{source}", destination, *("|| true" if continue_on_error) ]
end
end

View File

@@ -2,7 +2,7 @@ module Kamal::Commands::App::Cord
def cord(version:)
pipe \
docker(:inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", container_name(version)),
[:awk, "'$2 == \"#{role.cord_volume.container_path}\" {print $1}'"]
[ :awk, "'$2 == \"#{role.cord_volume.container_path}\" {print $1}'" ]
end
def tie_cord(cord)
@@ -17,6 +17,6 @@ module Kamal::Commands::App::Cord
def create_empty_file(file)
chain \
make_directory_for(file),
[:touch, file]
[ :touch, file ]
end
end

View File

@@ -25,7 +25,7 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
pipe \
docker(:inspect, "-f", "'{{ .Config.Labels.service }}'", config.absolute_image),
any(
[:grep, "-x", config.service],
[ :grep, "-x", config.service ],
"(echo \"Image #{config.absolute_image} is missing the 'service' label\" && exit 1)"
)
end
@@ -38,8 +38,8 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
def build_cache
if cache_to && cache_from
["--cache-to", cache_to,
"--cache-from", cache_from]
[ "--cache-to", cache_to,
"--cache-from", cache_from ]
end
end

View File

@@ -1,5 +1,4 @@
class Kamal::Commands::Healthcheck < Kamal::Commands::Base
def run
primary = config.role(config.primary_role)

View File

@@ -5,14 +5,14 @@ require "base64"
class Kamal::Commands::Lock < Kamal::Commands::Base
def acquire(message, version)
combine \
[:mkdir, lock_dir],
[ :mkdir, lock_dir ],
write_lock_details(message, version)
end
def release
combine \
[:rm, lock_details_file],
[:rm, "-r", lock_dir]
[ :rm, lock_details_file ],
[ :rm, "-r", lock_dir ]
end
def status
@@ -24,19 +24,19 @@ class Kamal::Commands::Lock < Kamal::Commands::Base
private
def write_lock_details(message, version)
write \
[:echo, "\"#{Base64.encode64(lock_details(message, version))}\""],
[ :echo, "\"#{Base64.encode64(lock_details(message, version))}\"" ],
lock_details_file
end
def read_lock_details
pipe \
[:cat, lock_details_file],
[:base64, "-d"]
[ :cat, lock_details_file ],
[ :base64, "-d" ]
end
def stat_lock_dir
write \
[:stat, lock_dir],
[ :stat, lock_dir ],
"/dev/null"
end
@@ -45,7 +45,7 @@ class Kamal::Commands::Lock < Kamal::Commands::Base
end
def lock_details_file
[lock_dir, :details].join("/")
[ lock_dir, :details ].join("/")
end
def lock_details(message, version)

View File

@@ -26,7 +26,7 @@ class Kamal::Commands::Prune < Kamal::Commands::Base
private
def stopped_containers_filters
[ "created", "exited", "dead" ].flat_map { |status| ["--filter", "status=#{status}"] }
[ "created", "exited", "dead" ].flat_map { |status| [ "--filter", "status=#{status}" ] }
end
def active_image_list
@@ -43,4 +43,4 @@ class Kamal::Commands::Prune < Kamal::Commands::Base
def healthcheck_service_filter
[ "--filter", "label=service=#{config.healthcheck_service}" ]
end
end
end

View File

@@ -1,5 +1,5 @@
class Kamal::Commands::Server < Kamal::Commands::Base
def ensure_run_directory
[:mkdir, "-p", config.run_directory]
[ :mkdir, "-p", config.run_directory ]
end
end

View File

@@ -4,7 +4,7 @@ class Kamal::Commands::Traefik < Kamal::Commands::Base
DEFAULT_IMAGE = "traefik:v2.10"
CONTAINER_PORT = 80
DEFAULT_ARGS = {
'log.level' => 'DEBUG'
"log.level" => "DEBUG"
}
DEFAULT_LABELS = {
# These ensure we serve a 502 rather than a 404 if no containers are available
@@ -84,7 +84,7 @@ class Kamal::Commands::Traefik < Kamal::Commands::Base
end
def remove_env_file
[:rm, "-f", host_env_file_path]
[ :rm, "-f", host_env_file_path ]
end
private

View File

@@ -16,7 +16,7 @@ class Kamal::Configuration::Accessory
end
def hosts
if (specifics.keys & ["host", "hosts", "roles"]).size != 1
if (specifics.keys & [ "host", "hosts", "roles" ]).size != 1
raise ArgumentError, "Specify one of `host`, `hosts` or `roles` for accessory `#{name}`"
end
@@ -159,7 +159,7 @@ class Kamal::Configuration::Accessory
if specifics.key?("host")
host = specifics["host"]
if host
[host]
[ host ]
else
raise ArgumentError, "Missing host for accessory `#{name}`"
end

View File

@@ -8,7 +8,7 @@ class Kamal::Configuration::Boot
limit = @options["limit"]
if limit.to_s.end_with?("%")
[@host_count * limit.to_i / 100, 1].max
[ @host_count * limit.to_i / 100, 1 ].max
else
limit
end

View File

@@ -88,7 +88,7 @@ class Kamal::Configuration::Builder
private
def valid?
if @options["cache"] && @options["cache"]["type"]
raise ArgumentError, "Invalid cache type: #{@options["cache"]["type"]}" unless ["gha", "registry"].include?(@options["cache"]["type"])
raise ArgumentError, "Invalid cache type: #{@options["cache"]["type"]}" unless [ "gha", "registry" ].include?(@options["cache"]["type"])
end
end
@@ -109,7 +109,7 @@ class Kamal::Configuration::Builder
end
def cache_to_config_for_gha
[ "type=gha", @options["cache"]&.fetch("options", nil)].compact.join(",")
[ "type=gha", @options["cache"]&.fetch("options", nil) ].compact.join(",")
end
def cache_to_config_for_registry

View File

@@ -67,7 +67,7 @@ class Kamal::Configuration::Role
end
def host_env_file_path
File.join host_env_directory, "#{[config.service, name, config.destination].compact.join("-")}.env"
File.join host_env_directory, "#{[ config.service, name, config.destination ].compact.join("-")}.env"
end
def env_args
@@ -123,13 +123,13 @@ class Kamal::Configuration::Role
end
def cord_host_directory
File.join config.run_directory_as_docker_volume, "cords", [container_prefix, config.run_id].join("-")
File.join config.run_directory_as_docker_volume, "cords", [ container_prefix, config.run_id ].join("-")
end
def cord_volume
if (cord = health_check_options["cord"])
@cord_volume ||= Kamal::Configuration::Volume.new \
host_path: File.join(config.run_directory, "cords", [container_prefix, config.run_id].join("-")),
host_path: File.join(config.run_directory, "cords", [ container_prefix, config.run_id ].join("-")),
container_path: cord
end
end
@@ -229,7 +229,7 @@ class Kamal::Configuration::Role
def specializations
if config.servers.is_a?(Array) || config.servers[name].is_a?(Array)
{ }
{}
else
config.servers[name].except("hosts")
end

View File

@@ -3,7 +3,7 @@ class Kamal::EnvFile
def initialize(env)
@env = env
end
def to_s
env_file = StringIO.new.tap do |contents|
if (secrets = @env["secret"]).present?
@@ -26,10 +26,10 @@ class Kamal::EnvFile
end
alias to_str to_s
private
def docker_env_file_line(key, value)
"#{key.to_s}=#{escape_docker_env_file_value(value)}\n"
"#{key}=#{escape_docker_env_file_value(value)}\n"
end
# Escape a value to make it safe to dump in a docker file.

View File

@@ -9,7 +9,7 @@ module Kamal::Utils
if value.present?
attr = "#{key}=#{escape_shell_value(value)}"
attr = self.sensitive(attr, redaction: "#{key}=[REDACTED]") if sensitive
[ argument, attr]
[ argument, attr ]
else
[ argument, key ]
end
@@ -29,7 +29,7 @@ module Kamal::Utils
# Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair
def flatten_args(args)
args.flat_map { |key, value| value.try(:map) { |entry| [key, entry] } || [ [ key, value ] ] }
args.flat_map { |key, value| value.try(:map) { |entry| [ key, entry ] } || [ [ key, value ] ] }
end
# Marks sensitive values for redaction in logs and human-visible output.
@@ -66,7 +66,7 @@ module Kamal::Utils
Array(filters).select do |filter|
matches += Array(items).select do |item|
# Only allow * for a wildcard
pattern = Regexp.escape(filter).gsub('\*', '.*')
pattern = Regexp.escape(filter).gsub('\*', ".*")
# items are roles or hosts
(item.respond_to?(:name) ? item.name : item).match(/^#{pattern}$/)
end