Created
March 5, 2026 13:10
-
-
Save bbelderbos/34a65f7896579edf866b6de6eabce809 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
| # /// script | |
| # dependencies = [ | |
| # "python-decouple", | |
| # "requests", | |
| # ] | |
| # /// | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| from typing import Final | |
| from decouple import config | |
| import requests | |
| # Create a personal access token with the 'gist' scope | |
| TOKEN: Final[str] = config("GITHUB_TOKEN") | |
| def create_gist(filename: str, description: str, code: str, public: bool = True) -> str: | |
| payload = json.dumps( | |
| { | |
| "description": description, | |
| "public": public, | |
| "files": {filename: {"content": code}}, | |
| } | |
| ) | |
| gists_url = "https://api.github.com/gists" | |
| headers = {"Authorization": f"token {TOKEN}"} | |
| resp = requests.post(gists_url, data=payload, headers=headers) | |
| resp.raise_for_status() | |
| return resp.json()["html_url"] | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Create a GitHub gist") | |
| parser.add_argument("-f", "--file", help="File to read code from") | |
| parser.add_argument( | |
| "-n", | |
| "--name", | |
| help="Filename for the gist (required when using stdin, default: from --file)", | |
| ) | |
| parser.add_argument("-d", "--description", default="", help="Gist description") | |
| parser.add_argument("--private", action="store_true", help="Make gist private") | |
| args = parser.parse_args() | |
| if args.file: | |
| path = Path(args.file) | |
| code = path.read_text() | |
| filename = args.name or path.name | |
| else: | |
| if not args.name: | |
| parser.error("--name is required when reading from stdin") | |
| code = sys.stdin.read() | |
| filename = args.name | |
| gist_url = create_gist(filename, args.description, code, public=not args.private) | |
| print(f"Gist created: {gist_url}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment