Created
December 23, 2019 08:54
-
-
Save kraftwerk28/2276ac4f0655d22f529d40543cc5d4d5 to your computer and use it in GitHub Desktop.
Script used to sync VSCode settings with gist
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
from os import getenv, path | |
import requests | |
from pprint import pprint | |
import json | |
from termcolor import colored | |
USERNAME = 'username' | |
TOKEN = 'your_github_app_token' | |
GHAPI = 'https://api.github.com' | |
GIST_ID = 'gist_id' | |
def make_data(content): | |
return json.dumps({ | |
'description': '', | |
'files': { | |
'user_settings.json': { | |
'content': content, | |
'filename': 'user_settings.json' | |
} | |
} | |
}) | |
def fail(response): | |
print(colored('Failed to update', 'red', attrs=['bold'])) | |
pprint(res.json()) | |
def main(): | |
url = f'{GHAPI}/gists/{GIST_ID}' | |
headers = { | |
'Authorization': f'token {TOKEN}' | |
} | |
remote_gist = requests.get(url, headers=headers) | |
if remote_gist.status_code != 200: | |
fail(remote_gist) | |
settings_path = path.join(getenv('HOME'), '.config/Code/User/settings.json') | |
content = open(settings_path).read() | |
if content == remote_gist.json()['files']['user_settings.json']['content']: | |
print(colored('Settings didn\'t change, exiting...', 'yellow')) | |
return | |
res = requests.patch(url, headers=headers, data=make_data(content)) | |
if res.status_code == 200: | |
print(colored('Updated successfully!', 'green', attrs=['bold'])) | |
else: | |
fail(res) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment