Skip to content

Instantly share code, notes, and snippets.

@chsh
Created April 26, 2013 07:21
Show Gist options
  • Save chsh/5465517 to your computer and use it in GitHub Desktop.
Save chsh/5465517 to your computer and use it in GitHub Desktop.
Volume unmount utility for Mac OS X.
#!/usr/bin/env ruby
class Runner
attr_reader :args
def initialize(args)
@args = args
end
def run
targets = targets_by_args %w(/Volumes/Name1 /Volumes/Name2 /Volumes/Name3)
disks = pick_disks targets
umount_disks disks
end
private
def pick_disks(*names)
names = [names].flatten
lines = `df`.chomp.split(/\r?\n/)
disks = []
names.each do |name|
lines.each do |line|
if line.index name
disks << line.split(/\s+/)[0]
end
end
end
disks
end
def targets_by_args(*names)
names = [names].flatten
return names if @args.size == 0
targets = []
@args.each do |arg|
da = arg.downcase
names.each do |name|
if name.downcase.index(da)
targets << name
end
end
end
targets
end
def umount_disks(disks)
disks.each do |disk|
cmd = "diskutil umount #{disk}"
puts cmd
`#{cmd}`
end
end
end
Runner.new(ARGV).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment