Skip to content

Commit

Permalink
DEV: lint the rest of the files
Browse files Browse the repository at this point in the history
  • Loading branch information
featheredtoast committed Oct 23, 2023
1 parent 159e8eb commit cb6bd78
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 87 deletions.
26 changes: 12 additions & 14 deletions lib/pups.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

require 'logger'
require 'yaml'
require "logger"
require "yaml"

require 'pups/version'
require 'pups/config'
require 'pups/command'
require 'pups/exec_command'
require 'pups/merge_command'
require 'pups/replace_command'
require 'pups/file_command'
require 'pups/docker'
require 'pups/runit'
require "pups/version"
require "pups/config"
require "pups/command"
require "pups/exec_command"
require "pups/merge_command"
require "pups/replace_command"
require "pups/file_command"
require "pups/docker"
require "pups/runit"

module Pups
class ExecError < RuntimeError
Expand All @@ -28,9 +28,7 @@ def self.log=(logger)
end

def self.silence
if @logger
@logger.close
end
@logger.close if @logger

@logger = Logger.new(File.open(File::NULL, "w"))
end
Expand Down
6 changes: 4 additions & 2 deletions lib/pups/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module Pups
class Command
def self.run(command, params)
case command
when String then from_str(command, params).run
when Hash then from_hash(command, params).run
when String
from_str(command, params).run
when Hash
from_hash(command, params).run
end
end

Expand Down
13 changes: 5 additions & 8 deletions lib/pups/docker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'shellwords'
require "shellwords"

class Pups::Docker
class << self
Expand All @@ -16,7 +16,7 @@ def generate_env_arguments(config)
def generate_link_arguments(config)
output = []
config&.each do |c|
output << "--link #{c['link']['name']}:#{c['link']['alias']}"
output << "--link #{c["link"]["name"]}:#{c["link"]["alias"]}"
end
normalize_output(output)
end
Expand All @@ -36,7 +36,7 @@ def generate_expose_arguments(config)
def generate_volume_arguments(config)
output = []
config&.each do |c|
output << "--volume #{c['volume']['host']}:#{c['volume']['guest']}"
output << "--volume #{c["volume"]["host"]}:#{c["volume"]["guest"]}"
end
normalize_output(output)
end
Expand All @@ -50,6 +50,7 @@ def generate_label_arguments(config)
end

private

def escape_user_string_literal(str)
# We need to escape the following strings as they are more likely to contain
# special characters than any of the other config variables on a Linux system:
Expand All @@ -59,11 +60,7 @@ def escape_user_string_literal(str)
end

def normalize_output(output)
if output.empty?
""
else
output.join(" ")
end
output.empty? ? "" : output.join(" ")
end
end
end
77 changes: 46 additions & 31 deletions lib/pups/exec_command.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,66 @@
# frozen_string_literal: true

require 'timeout'
require 'English'
require "timeout"
require "English"

module Pups
class ExecCommand < Pups::Command
attr_reader :commands, :cd
attr_accessor :background, :raise_on_fail, :stdin, :stop_signal

def self.terminate_async(opts = {})
return unless defined? @@asyncs
return unless defined?(@@asyncs)

Pups.log.info('Terminating async processes')
Pups.log.info("Terminating async processes")

@@asyncs.each do |async|
Pups.log.info("Sending #{async[:stop_signal]} to #{async[:command]} pid: #{async[:pid]}")
Pups.log.info(
"Sending #{async[:stop_signal]} to #{async[:command]} pid: #{async[:pid]}"
)
begin
Process.kill(async[:stop_signal], async[:pid])
rescue StandardError
nil
end
end

@@asyncs.map do |async|
Thread.new do
Timeout.timeout(opts[:wait] || 10) do
Process.wait(async[:pid])
rescue StandardError
nil
end
rescue Timeout::Error
Pups.log.info("#{async[:command]} pid:#{async[:pid]} did not terminate cleanly, forcing termination!")
begin
Process.kill('KILL', async[:pid])
Process.wait(async[:pid])
rescue Errno::ESRCH
rescue Errno::ECHILD
@@asyncs
.map do |async|
Thread.new do
Timeout.timeout(opts[:wait] || 10) do
Process.wait(async[:pid])
rescue StandardError
nil
end
rescue Timeout::Error
Pups.log.info(
"#{async[:command]} pid:#{async[:pid]} did not terminate cleanly, forcing termination!"
)
begin
Process.kill("KILL", async[:pid])
Process.wait(async[:pid])
rescue Errno::ESRCH
rescue Errno::ECHILD
end
end
end
end.each(&:join)
.each(&:join)
end

def self.from_hash(hash, params)
cmd = new(params, hash['cd'])
cmd = new(params, hash["cd"])

case c = hash['cmd']
when String then cmd.add(c)
when Array then c.each { |i| cmd.add(i) }
case c = hash["cmd"]
when String
cmd.add(c)
when Array
c.each { |i| cmd.add(i) }
end

cmd.background = hash['background']
cmd.stop_signal = hash['stop_signal'] || 'TERM'
cmd.raise_on_fail = hash['raise_on_fail'] if hash.key? 'raise_on_fail'
cmd.stdin = interpolate_params(hash['stdin'], params)
cmd.background = hash["background"]
cmd.stop_signal = hash["stop_signal"] || "TERM"
cmd.raise_on_fail = hash["raise_on_fail"] if hash.key? "raise_on_fail"
cmd.stdin = interpolate_params(hash["stdin"], params)

cmd
end
Expand Down Expand Up @@ -88,7 +96,11 @@ def run
def spawn(command)
if background
pid = Process.spawn(command)
(@@asyncs ||= []) << { pid: pid, command: command, stop_signal: (stop_signal || 'TERM') }
(@@asyncs ||= []) << {
pid: pid,
command: command,
stop_signal: (stop_signal || "TERM")
}
Thread.new do
begin
Process.wait(pid)
Expand All @@ -100,7 +112,7 @@ def spawn(command)
return pid
end

IO.popen(command, 'w+') do |f|
IO.popen(command, "w+") do |f|
if stdin
# need a way to get stdout without blocking
Pups.log.info(stdin)
Expand All @@ -112,7 +124,10 @@ def spawn(command)
end

unless $CHILD_STATUS == 0
err = Pups::ExecError.new("#{command} failed with return #{$CHILD_STATUS.inspect}")
err =
Pups::ExecError.new(
"#{command} failed with return #{$CHILD_STATUS.inspect}"
)
err.exit_code = $CHILD_STATUS.exitstatus
raise err
end
Expand Down
12 changes: 5 additions & 7 deletions lib/pups/file_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class FileCommand < Pups::Command

def self.from_hash(hash, params)
command = new
command.path = hash['path']
command.contents = hash['contents']
command.chmod = hash['chmod']
command.chown = hash['chown']
command.path = hash["path"]
command.contents = hash["contents"]
command.chmod = hash["chmod"]
command.chown = hash["chown"]
command.params = params

command
Expand All @@ -26,9 +26,7 @@ def run
path = interpolate_params(@path)

`mkdir -p #{File.dirname(path)}`
File.open(path, 'w') do |f|
f.write(interpolate_params(contents))
end
File.open(path, "w") { |f| f.write(interpolate_params(contents)) }
`chmod #{@chmod} #{path}` if @chmod
`chown #{@chown} #{path}` if @chown
Pups.log.info("File > #{path} chmod: #{@chmod} chown: #{@chown}")
Expand Down
31 changes: 17 additions & 14 deletions lib/pups/merge_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ def self.from_str(command, params)
end

def self.parse_command(command)
split = command.split(' ')
raise ArgumentError, "Invalid merge command #{command}" unless split[-1][0] == '$'
split = command.split(" ")
unless split[-1][0] == "$"
raise ArgumentError, "Invalid merge command #{command}"
end

[split[0..-2].join(' '), split[-1][1..-1]]
[split[0..-2].join(" "), split[-1][1..-1]]
end

def initialize(command, params)
Expand All @@ -25,25 +27,26 @@ def initialize(command, params)

def run
merged = self.class.deep_merge(YAML.load_file(@filename), @merge_hash)
File.open(@filename, 'w') { |f| f.write(merged.to_yaml) }
File.open(@filename, "w") { |f| f.write(merged.to_yaml) }
Pups.log.info("Merge: #{@filename} with: \n#{@merge_hash.inspect}")
end

def self.deep_merge(first, second, *args)
args ||= []
merge_arrays = args.include? :merge_arrays

merger = proc { |_key, v1, v2|
if v1.is_a?(Hash) && v2.is_a?(Hash)
v1.merge(v2, &merger)
elsif v1.is_a?(Array) && v2.is_a?(Array)
merge_arrays ? v1 + v2 : v2
elsif v2.is_a?(NilClass)
v1
else
v2
merger =
proc do |_key, v1, v2|
if v1.is_a?(Hash) && v2.is_a?(Hash)
v1.merge(v2, &merger)
elsif v1.is_a?(Array) && v2.is_a?(Array)
merge_arrays ? v1 + v2 : v2
elsif v2.is_a?(NilClass)
v1
else
v2
end
end
}
first.merge(second, &merger)
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/pups/runit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def initialize(name)
def setup
`mkdir -p /etc/service/#{name}`
run = "/etc/service/#{name}/run"
File.open(run, 'w') do |f|
f.write(run_script)
end
File.open(run, "w") { |f| f.write(run_script) }
`chmod +x #{run}`
end

Expand All @@ -31,9 +29,7 @@ def cd_script
end

def env_script
@env&.map do |k, v|
"export #{k}=#{v}"
end&.join("\n")
@env&.map { |k, v| "export #{k}=#{v}" }&.join("\n")
end
end
end
2 changes: 1 addition & 1 deletion lib/pups/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Pups
VERSION = '1.1.1'
VERSION = "1.1.1"
end
8 changes: 4 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'pups'
require 'pups/cli'
require 'minitest/autorun'
require 'minitest/pride'
require "pups"
require "pups/cli"
require "minitest/autorun"
require "minitest/pride"

0 comments on commit cb6bd78

Please sign in to comment.