Skip to content

Instantly share code, notes, and snippets.

@kojix2
Last active March 1, 2026 04:14
Show Gist options
  • Select an option

  • Save kojix2/e461b9996cbbb8fc463f089e2a145b04 to your computer and use it in GitHub Desktop.

Select an option

Save kojix2/e461b9996cbbb8fc463f089e2a145b04 to your computer and use it in GitHub Desktop.
gdrive
#!/usr/bin/env ruby
# frozen_string_literal: true
# This is a script that uses rclone to mount and unmount Google Drive.
require "optparse"
require "fileutils"
MOUNT_POINT = File.expand_path("~/GoogleDrive")
REMOTE = "gdrive:"
LOG_FILE = File.expand_path("~/.config/rclone/rclone.log")
RCLONE_OPTS = [
"--daemon",
"--vfs-cache-mode", "full",
"--vfs-cache-max-size", "1G",
"--vfs-cache-max-age", "24h",
"--dir-cache-time", "1h",
"--log-file", LOG_FILE,
"--log-level", "INFO",
].freeze
def run(*cmd)
ok = system(*cmd)
unless ok
code = $?.exitstatus
warn "Command failed (exit=#{code}): #{cmd.join(' ')}"
exit(code || 1)
end
end
def mounted?(mount_point)
system("mountpoint", "-q", mount_point)
end
def mount_drive
if mounted?(MOUNT_POINT)
puts "Already mounted at #{MOUNT_POINT}"
return
end
FileUtils.mkdir_p(MOUNT_POINT)
FileUtils.mkdir_p(File.dirname(LOG_FILE))
puts "Mounting #{REMOTE} -> #{MOUNT_POINT}"
run("rclone", "mount", REMOTE, MOUNT_POINT, *RCLONE_OPTS)
end
def unmount_drive(lazy: false)
unless mounted?(MOUNT_POINT)
puts "Not mounted."
return
end
puts "Unmounting #{MOUNT_POINT}#{lazy ? " (lazy)" : ""}"
args = ["-u"]
args << "-z" if lazy
run("fusermount", *args, MOUNT_POINT)
end
def stat_drive
if mounted?(MOUNT_POINT)
puts "Mounted at #{MOUNT_POINT}"
run("df", "-h", MOUNT_POINT)
else
puts "Not mounted."
end
end
action = :mount
lazy_unmount = false
parser = OptionParser.new do |opts|
opts.banner = "Usage: gdrive.rb [options]\nDefault action: mount"
opts.on("-m", "--mount", "Mount Google Drive") do
action = :mount
end
opts.on("-u", "--unmount", "Unmount Google Drive") do
action = :unmount
end
opts.on("-s", "--stat", "Show mount status") do
action = :stat
end
opts.on("-l", "--lazy", "Lazy unmount (with -u)") do
lazy_unmount = true
end
opts.on("-h", "--help", "Show help") do
puts opts
exit 0
end
end
begin
parser.parse!(ARGV)
rescue OptionParser::ParseError => e
warn e.message
warn parser
exit 1
end
case action
when :mount
mount_drive
when :unmount
unmount_drive(lazy: lazy_unmount)
when :stat
stat_drive
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment