Skip to content

Instantly share code, notes, and snippets.

@btbytes
Last active March 21, 2024 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/73d6ddb0cf940e730edf22a60208691b to your computer and use it in GitHub Desktop.
Save btbytes/73d6ddb0cf940e730edf22a60208691b to your computer and use it in GitHub Desktop.
Gist created by gistash

gistash

Stash files on gist.github.com

Installation

$ wget https://gist.githubusercontent.com/btbytes/73d6ddb0cf940e730edf22a60208691b/raw/fbf9efb44ee1c88eaaa3f62d6437737402ea74ce/gistash 
$ chmod +x gistash

The script uses only python standard library. Tested with Python3.11.

You need to get a GITHUB_TOKEN from github.com/settings/tokens and make it available in your environment.

Typical usage

When creating a new gist:

$ gistash *.txt
    uploading a.txt
    uploading b.txt
    done.

You can pass --public flag when creating a new gist to make it publicly visible.

When updating an existing, gist(say 28fe346dc83fd26a43c351c507c6599d) that you have permissions to update:

$ gistash *.txt --gist-id 28fe346dc83fd26a43c351c507c6599d
    updating a.txt
    updating b.txt
    uploading c.txt
    done.

Note: --public is not implemented for update.

References

Original idea: gistash from my blog dated May 14, 2020.

#!/usr/bin/env python
# See https://www.btbytes.com/posts/2020-05-14-4-notes.html
# Pradeep Gowda, 2023-03-21
# TODO: Updating a secret gist to be public does not work yet
import os
import argparse
import json
import urllib.request
API_URL = "https://api.github.com/gists"
ACCESS_TOKEN = os.getenv("GITHUB_TOKEN")
def create_gist(files, public):
"""Create a new gist with the given files."""
data = {"description": "Gist created by gitstash", "public": public, "files": {}}
for file_path in files:
with open(file_path, "r") as file:
content = file.read()
file_name = os.path.basename(file_path)
data["files"][file_name] = {"content": content}
headers = {
"Authorization": f"token {ACCESS_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
data = json.dumps(data).encode("utf-8")
req = urllib.request.Request(API_URL, data=data, headers=headers, method="POST")
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode("utf-8"))
gist_url = response_data["html_url"]
print(f"Gist created: {gist_url}")
except urllib.error.HTTPError as e:
print("Failed to create gist.")
print(e.read().decode("utf-8"))
def update_gist(files, gist_id):
"""Update an existing gist with the given files."""
url = f"{API_URL}/{gist_id}"
data = {"files": {}}
for file_path in files:
with open(file_path, "r") as file:
content = file.read()
file_name = os.path.basename(file_path)
data["files"][file_name] = {"content": content}
headers = {
"Authorization": f"token {ACCESS_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
data = json.dumps(data).encode("utf-8")
req = urllib.request.Request(url, data=data, headers=headers, method="PATCH")
try:
with urllib.request.urlopen(req) as response:
print("Gist updated successfully.")
except urllib.error.HTTPError as e:
print("Failed to update gist.")
print(e.read().decode("utf-8"))
def main():
parser = argparse.ArgumentParser(description="Store files in a GitHub gist.")
parser.add_argument(
"files", metavar="FILE", nargs="+", help="Files to store in the gist."
)
parser.add_argument("--gist-id", help="ID of the gist to update.")
parser.add_argument(
"--public", action="store_true", help="Make the gist public (default: private)."
)
args = parser.parse_args()
if args.gist_id:
update_gist(args.files, args.gist_id)
else:
create_gist(args.files, args.public)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment