Last active
September 4, 2026 22:43
-
-
Save alex-quiterio/20e113ffabcfda62ae38022ff159afe6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # update_gists.rb — updates all gists in a directory | |
| # | |
| # 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 update-gists.rb [directory] # update all gists | |
| # ruby update-gists.rb [directory] my_script.rb # update one gist | |
| require "open3" | |
| require 'json' | |
| # ── args ────────────────────────────────────────────────────────────────────── | |
| dir = ARGV[0] || "." | |
| only = ARGV[1] # optional filename filter | |
| 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 | |
| if only && !File.foreach(mapping_file, encoding: "UTF-8").any? { |l| l.split("=", 2).first&.strip == only } | |
| abort "❌ '#{only}' not found in .gist-ids" | |
| end | |
| # ── update ──────────────────────────────────────────────────────────────────── | |
| success = 0 | |
| failed = 0 | |
| skipped = 0 | |
| if only | |
| puts "📂 Updating '#{only}' in: #{File.realpath(dir)}" | |
| else | |
| puts "📂 Updating all gists in: #{File.realpath(dir)}" | |
| end | |
| 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 | |
| next if only && filename != only | |
| filepath = File.join(dir, filename) | |
| unless File.exist?(filepath) | |
| puts "⚠️ Skipping '#{filename}' — file not found" | |
| skipped += 1 | |
| next | |
| end | |
| print "↑ Updating '#{filename}' (gist: #{gist_id})... " | |
| payload = { files: { filename => { content: File.read(filepath, encoding: "UTF-8") } } }.to_json | |
| _, stderr, status = Open3.capture3( | |
| "gh", "api", "/gists/#{gist_id}", | |
| "-X", "PATCH", | |
| "--input", "-", | |
| stdin_data: payload | |
| ) | |
| if status.success? | |
| puts "✅" | |
| success += 1 | |
| else | |
| puts "❌ failed" | |
| $stderr.puts " #{stderr.strip}" unless stderr.strip.empty? | |
| failed += 1 | |
| end | |
| end | |
| # ── summary ─────────────────────────────────────────────────────────────────── | |
| puts "" | |
| puts "─────────────────────────────" | |
| puts "✅ Updated : #{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