#!/usr/bin/env ruby require 'hackmac' include Hackmac require 'term/ansicolor' class String include Term::ANSIColor end def x(cmd, verbose: true) prompt = cmd =~ /\A\s*sudo/ ? ?# : ?$ output = `#{cmd}` if $?.success? print "#{prompt} #{cmd}".green puts verbose ? "" : " >/dev/null".yellow else print "#{prompt} #{cmd}".red puts verbose ? "" : " >/dev/null".yellow STDERR.puts "command #{cmd.inspect} failed with exit status #{$?.exitstatus}".on_red.white end if verbose print output.italic end output end def clone(from:, to:) print "Cloning from #{from} to #{to} now? (y/n) ".bold.yellow if gets !~ /\Ay/i return end x %{sudo mkdir -v "/Volumes/#{from}"} x %{sudo mkdir -v "/Volumes/#{to}"} begin x %{sudo diskutil mount -mountPoint "/Volumes/#{from}" "#{from}"} x %{sudo diskutil mount -mountPoint "/Volumes/#{to}" "#{to}"} x %{rsync -nav --exclude ".*" --delete "/Volumes/#{from}/" "/Volumes/#{to}/"} print "This will be copied/deleted. Really do it? (y/n) ".bold.yellow if gets !~ /\Ay/i puts " *** Interrupted.".bold.yellow exit 1 end x %{rsync -av --exclude ".*" --delete "/Volumes/#{from}/" "/Volumes/#{to}/"} ensure x %{sudo diskutil unmount "#{from}"} x %{sudo diskutil unmount "#{to}"} end end def dev value = x(%{bdmesg}, verbose: false).lines. find { |l| l =~ /SelfDevicePath=(.*)\r/ and break $1 } uuid = value.split('\\').last[/(?: