Skip to content

Instantly share code, notes, and snippets.

@alex-quiterio
Last active September 4, 2026 22:43
Show Gist options
  • Select an option

  • Save alex-quiterio/9bacefe805c7f2a270ae116011d4744e to your computer and use it in GitHub Desktop.

Select an option

Save alex-quiterio/9bacefe805c7f2a270ae116011d4744e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# pull_gists.rb — pulls gist files that don't exist locally
#
# Setup:
# 1. Install gh CLI: https://cli.github.com
# 2. Run: gh auth login
# 3. Create a .gist-ids file in your gist directory:
# my_script.rb=abc123def456
# notes.md=xyz789ghi012
#
# Usage:
# ruby pull_gists.rb [directory] # defaults to current dir
# ruby pull_gists.rb --force [dir] # overwrite existing files too
require "open3"
require "fileutils"
# ── args ──────────────────────────────────────────────────────────────────────
force = ARGV.delete("--force")
dir = ARGV.first || "."
mapping_file = File.join(dir, ".gist-ids")
# ── checks ────────────────────────────────────────────────────────────────────
unless system("which gh > /dev/null 2>&1")
abort "❌ gh CLI not found. Install it: https://cli.github.com"
end
unless File.exist?(mapping_file)
abort <<~MSG
❌ No .gist-ids file found in '#{dir}'
Create one with the format:
filename.rb=gist_id_here
notes.md=another_gist_id
MSG
end
FileUtils.mkdir_p(dir)
# ── pull ──────────────────────────────────────────────────────────────────────
success = 0
skipped = 0
failed = 0
puts "📂 Pulling gists into: #{File.realpath(dir)}"
puts ""
File.foreach(mapping_file, encoding: "UTF-8") do |line|
line.strip!
next if line.empty? || line.start_with?("#")
filename, gist_id = line.split("=", 2).map(&:strip)
next unless filename && gist_id
filepath = File.join(dir, filename)
if File.exist?(filepath) && !force
puts "⏭ Skipping '#{filename}' — already exists (use --force to overwrite)"
skipped += 1
next
end
print "↓ Pulling '#{filename}' (gist: #{gist_id})... "
# gh gist view <id> --filename <file> --raw
stdout, stderr, status = Open3.capture3("gh", "gist", "view", gist_id, "--filename", filename, "--raw")
if status.success?
File.write(filepath, stdout.force_encoding("UTF-8"), mode: "w:UTF-8")
puts "✅"
success += 1
else
puts "❌ failed"
$stderr.puts " #{stderr.strip}" unless stderr.strip.empty?
failed += 1
end
end
# ── summary ───────────────────────────────────────────────────────────────────
puts ""
puts "─────────────────────────────"
puts "✅ Pulled : #{success}"
puts "⏭ Skipped : #{skipped}" if skipped > 0
puts "❌ Failed : #{failed}" if failed > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment