Created
February 8, 2012 16:11
-
-
Save wr0ngway/1770775 to your computer and use it in GitHub Desktop.
Utility script to archive and cleanup old gists
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 | |
# Add your gems here | |
# | |
gemfile_contents = <<-EOF | |
source "http://rubygems.org" | |
gem 'clamp' | |
gem 'iniparse' | |
gem 'rest-client' | |
gem 'json' | |
EOF | |
require 'open-uri' | |
eval open('https://raw.github.com/gist/1770601/gist-bundler.rb').read, binding | |
# Your code goes here | |
# | |
require 'fileutils' | |
require 'open-uri' | |
class GistUtil < Clamp::Command | |
parameter "ACTION", "The action to perform, archive or destroy" | |
option ["--verbose", "-v"], :flag, "Be verbose" | |
option ["--interactive", "-i"], :flag, "Prompt for each action" | |
option ["--single", "-s"], "SINGLE_ID", "Process a single item" | |
option ["--config", "-c"], "CONFIG_FILE", "Use the given configuration file\n", :default => "~/.gitconfig" | |
def execute | |
conf = File.expand_path(config) | |
signal_usage_error "No config file at: #{config}" unless File.exist?(conf) | |
ini = IniParse.parse(File.read(conf)) | |
section = ini['github'] | |
user = section['user'] | |
pass = section['password'] | |
signal_usage_error "A user and password value need to be set in #{config}" unless user && pass | |
@github = RestClient::Resource.new('https://api.github.com', user, pass) | |
process_gists | |
end | |
def process_gists | |
count = 0 | |
page = 1 | |
loop do | |
if single | |
puts "Fetching #{single}" if verbose? | |
resp = @github["/gists/#{single}"].get | |
data = [JSON.parse(resp)] | |
else | |
puts "Fetching page: #{page}" if verbose? | |
resp = @github['/gists'].get(:params => {:page => page}) | |
data = JSON.parse(resp) | |
end | |
data.each do |d| | |
case action | |
when 'destroy' | |
destroy(d) | |
when 'archive' | |
archive(d) | |
else | |
raise "Invalid operation: #{op}" | |
end | |
end | |
count += data.size | |
break if single || data.size == 0 | |
page += 1 | |
end | |
puts "Count: #{count}" if verbose? | |
end | |
def fetch_url(url) | |
url = url.gsub(' ', '%20') | |
url = url.gsub('gist.github.com/raw/', 'raw.github.com/gist/') | |
open(url).read | |
end | |
def archive(d) | |
ident = d['id'].to_s | |
dir = "gists/#{ident}" | |
FileUtils.mkdir_p(dir) | |
File.open("#{dir}/#{ident}.json", 'w') { |f| f.write JSON.pretty_generate(d) } | |
d['files'].each do |k, v| | |
File.open("#{dir}/#{k}", 'w') do |f| | |
if v['content'] | |
f.write v['content'] | |
elsif v['raw_url'] | |
f.write fetch_url(v['raw_url']) | |
else | |
puts "No data for #{ident}" if verbose? | |
end | |
end | |
end | |
end | |
def destroy(d) | |
destroy = true | |
ident = d['id'].to_s | |
puts | |
puts "=" * (ident.size + 1) | |
puts "#{ident}:" | |
puts "=" * (ident.size + 1) | |
puts | |
if interactive? | |
d['files'].each do |k, v| | |
puts | |
puts "-" * (k.size + 1) | |
puts "#{k}:" | |
puts "-" * (k.size + 1) | |
puts | |
if v['content'] | |
puts v['content'] | |
elsif v['raw_url'] | |
puts fetch_url(v['raw_url']) | |
else | |
puts "No data for #{ident}" | |
end | |
puts | |
end | |
print "Destroy #{ident} [Y/n]? " | |
destroy = ( $stdin.gets() !~ /^n/i ) | |
end | |
@github["/gists/#{ident}"].delete if destroy | |
end | |
end | |
GistUtil.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment