fix: check if templates is valid + exists

This commit is contained in:
Jordan ⌨️ 2023-05-29 18:08:12 +02:00 committed by Sleepy Monax
parent 5a926057a7
commit b0f18a83d2

View file

@ -1,5 +1,4 @@
import os import os
import json
import logging import logging
import tempfile import tempfile
import requests import requests
@ -246,7 +245,6 @@ def initCmd(args: Args):
repo = const.DEFAULT_REPO_TEMPLATES if not "repo" in args.opts else args.opts["repo"] repo = const.DEFAULT_REPO_TEMPLATES if not "repo" in args.opts else args.opts["repo"]
list = "list" in args.opts list = "list" in args.opts
if list:
logger.info("Fetching registry...") logger.info("Fetching registry...")
r = requests.get( r = requests.get(
f'https://raw.githubusercontent.com/{repo}/main/registry.json') f'https://raw.githubusercontent.com/{repo}/main/registry.json')
@ -255,9 +253,15 @@ def initCmd(args: Args):
logger.error('Failed to fetch registry') logger.error('Failed to fetch registry')
exit(1) exit(1)
registry = r.json()
if list:
print('\n'.join( print('\n'.join(
f"* {entry['id']} - {entry['description']}" for entry in json.loads(r.text))) f"* {entry['id']} - {entry['description']}" for entry in registry))
else: else:
if not any(filter(lambda t: t['id'] == template, registry)):
raise RuntimeError(f"Unknown template {template}")
with tempfile.TemporaryDirectory() as tmp: with tempfile.TemporaryDirectory() as tmp:
shell.exec(*["git", "clone", "-n", "--depth=1", shell.exec(*["git", "clone", "-n", "--depth=1",
"--filter=tree:0", f"https://github.com/{repo}", tmp, "-q"]) "--filter=tree:0", f"https://github.com/{repo}", tmp, "-q"])