Skip to content

Instantly share code, notes, and snippets.

@kraftwerk28
Created December 23, 2019 08:54
Show Gist options
  • Save kraftwerk28/2276ac4f0655d22f529d40543cc5d4d5 to your computer and use it in GitHub Desktop.
Save kraftwerk28/2276ac4f0655d22f529d40543cc5d4d5 to your computer and use it in GitHub Desktop.
Script used to sync VSCode settings with gist
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