Created
October 19, 2010 14:42
-
-
Save tily/634302 to your computer and use it in GitHub Desktop.
use gist as memo pad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# usage: gistpad [id] | |
require 'rubygems' | |
require 'gisty' | |
require 'temp_dir' | |
class GistPad < Gisty | |
@commands = { | |
:push => 'push origin master', | |
:commit => "commit %s -m memo." | |
} | |
@commands.each do |method, option| | |
define_method method do |file| | |
dir, base = File.split file | |
FileUtils.cd dir do | |
Kernel.system "git #{option}" % file | |
end | |
end | |
end | |
end | |
CONF_PATH = ENV['HOME'] + '/.gistpad' | |
EDITOR = ENV['EDITOR'] || 'vi' | |
FILE_NAME = 'blog.txt' | |
GISTY_DIR = ENV['GISTY_DIR'] | |
def main arg | |
@g = GistPad.new ENV['GISTY_DIR'] | |
id = if arg =~ /^\d+$/ | |
arg | |
elsif File.exist? CONF_PATH | |
File.read CONF_PATH | |
else | |
nil | |
end | |
id ? edit(id) : init | |
end | |
def edit id | |
file = [GISTY_DIR, id, FILE_NAME].join('/') | |
unless File.exist? file | |
@g.clone id | |
File.open(CONF_PATH, 'w') {|f| f.write id } | |
end | |
Kernel.system EDITOR, file | |
@g.commit file | |
@g.push file | |
end | |
def init | |
id = nil | |
TempDir.create do |dir| | |
file = "#{dir}/#{FILE_NAME}" | |
unless Kernel.system EDITOR, file | |
raise 'editor ended abnormally.' | |
end | |
url = @g.create(file, :private => true) | |
id = url.split('/').last | |
end | |
@g.clone id | |
File.open(CONF_PATH, 'w') {|f| f.write id } | |
end | |
main ARGV[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment