Skip to content

Commit

Permalink
Dry up code (many thanks to @miah!!!)
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Aldrich <[email protected]>
  • Loading branch information
jerryaldrichiii committed Feb 6, 2018
1 parent 03f8e43 commit 7dc6fa1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 132 deletions.
61 changes: 59 additions & 2 deletions lib/train/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require 'train/extras/stat'

module Train
class File
class File # rubocop:disable Metrics/ClassLength
def initialize(backend, path, follow_symlink = true)
@backend = backend
@path = path || ''
Expand Down Expand Up @@ -130,9 +130,66 @@ def mounted?
!mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.empty?
end

def md5sum
return perform_checksum(:md5) unless @backend.os.family == 'windows'

perform_checksum_windows(:md5)
end

def sha256sum
return perform_checksum(:sha256) unless @backend.os.family == 'windows'

perform_checksum_windows(:sha256)
end

private

# Called by child classes when md5/sha256 checksum fails
def md5_command
case @backend.os.family
when 'darwin'
# `-r` reverses output to match `md5sum`
'md5 -r'
when 'solaris'
'digest -a md5'
else
'md5sum'
end
end

def sha256_command
case @backend.os.family
when 'darwin', 'hpux'
'shasum -a 256'
when 'solaris'
'digest -a sha256'
else
'sha256sum'
end
end

def perform_checksum(method)
case method
when :md5
cmd = "#{md5_command} #{@path}"
when :sha256
cmd = "#{sha256_command} #{@path}"
end

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def perform_checksum_windows(method)
cmd = "CertUtil -hashfile #{@path} #{method.to_s.upcase}"

res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def raise_checksum_error(cmd, res)
fail "Failed to get checksum with `#{cmd}`.\n" \
"STDOUT: #{res.stdout}\n" \
Expand Down
33 changes: 0 additions & 33 deletions lib/train/file/local/unix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,6 @@ def unix_mode_mask(owner, type)
t & o
end

def md5sum
case @backend.os.family
when 'darwin'
# `-r` reverses output to match `md5sum`
cmd = "md5 -r #{@path}"
when 'solaris'
cmd = "digest -a md5 #{@path}"
else
cmd = "md5sum #{@path}"
end

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
case @backend.os.family
when 'darwin', 'hpux'
cmd = "shasum -a 256 #{@path}"
when 'solaris'
cmd = "digest -a sha256 #{@path}"
else
cmd = "sha256sum #{@path}"
end

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

private

def pw_username(uid)
Expand Down
16 changes: 0 additions & 16 deletions lib/train/file/local/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ def stat

@stat
end

def md5sum
cmd = "CertUtil -hashfile #{@path} MD5"
res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
cmd = "CertUtil -hashfile #{@path} SHA256"
res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
end
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/train/file/remote/aix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@ def link_path
def mounted
@mounted ||= @backend.run_command("lsfs -c #{@spath}")
end

def md5sum
cmd = "md5sum #{path}"
res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
cmd = "sha256sum #{@path}"
res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/train/file/remote/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ def content
@content = nil if directory? or size.nil? or size > 0
@content
end

def md5sum
cmd = "md5sum #{path}"
res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
cmd = "sha256sum #{@path}"
res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end
end
end
end
Expand Down
33 changes: 0 additions & 33 deletions lib/train/file/remote/unix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,6 @@ def path
@link_path ||= read_target_path
end

def md5sum
case @backend.os.family
when 'darwin'
# `-r` reverses output to match `md5sum`
cmd = "md5 -r #{@path}"
when 'solaris'
cmd = "digest -a md5 #{@path}"
else
fail 'No application defined to perform md5sum'
end

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
case @backend.os.family
when 'darwin', 'hpux'
cmd = "shasum -a 256 #{@path}"
when 'solaris'
cmd = "digest -a sha256 #{@path}"
else
fail 'No application defined to perform sha256sum'
end

res = @backend.run_command(cmd)
return res.stdout.split(' ').first if res.exit_status == 0

raise_checksum_error(cmd, res)
end

private

# Returns full path of a symlink target(real dest) or '' on symlink loop
Expand Down
16 changes: 0 additions & 16 deletions lib/train/file/remote/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ def mounted
nil
end

def md5sum
cmd = "CertUtil -hashfile #{@path} MD5"
res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
end

def sha256sum
cmd = "CertUtil -hashfile #{@path} SHA256"
res = @backend.run_command(cmd)
return res.stdout.split("\r\n")[1].tr(' ', '') if res.exit_status == 0

raise_checksum_error(cmd, res)
end

private

def attributes
Expand Down

0 comments on commit 7dc6fa1

Please sign in to comment.