Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Created July 8, 2011 02:24
Show Gist options
  • Save kfatehi/1070978 to your computer and use it in GitHub Desktop.
Save kfatehi/1070978 to your computer and use it in GitHub Desktop.
Gist Sync Daemon
#!/usr/bin/env ruby
# I didn't know gists were not editable by multiple people when i first wrote this... oh well.
class GistFile
def initialize(filename, gist_id)
@filename = filename
@file = File.new(filename)
@gist_id = gist_id
end
def sync
log "Pull any remote changes"
self.pull
gstat = `git status`
if gstat.include?('modified')
log "Local changes detected. Pushing..."
self.push
else
log "No local change. No need to push."
end
end
def push
system "git commit -am \"gsyncd\""
system "git push git@gist.github.com:#{@gist_id}.git"
end
def pull
system "git pull"
end
def self.daemon(filename, gist_id)
gsyncd = self.new(filename, gist_id)
while true
gsyncd.sync
sleep 1
end
end
end
def msg(s)
puts "[GsyncD] #{s}"
end
def log(s)
msg "[log] #{s}"
end
case ARGV.size
when 1
if File.exists?((file = ARGV[0]))
path = File.expand_path(file).split('/')
path.pop
gist_id = path.last
Dir.chdir path.join('/')
msg "File #{file} exists."
msg "Checking git situation..."
if system("git status")
msg "Looks like we're in a git repository. Launching!"
GistFile.daemon(file, gist_id)
else
log "Not a git repository. Make a new gist and clone it first."
end
else
msg "File #{file} does not exist."
end
else
msg "Constantly sync a file using gist & github."
msg "Usage: ruby gsyncd.rb <filename>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment