more entries in the catalog ; more readme and faq ; onging impl

This commit is contained in:
2023-02-15 18:24:14 +01:00
parent d6a31f58f9
commit b5b21f6ca9
6 changed files with 92 additions and 20 deletions

View File

@@ -3,10 +3,12 @@
Usage:
cliget [-v] [-c URL] list
cliget [-v] [-c URL] search PAT
cliget [-v] [-c URL] install TOOL ...
cliget [-v] [-c URL] update [--all] [TOOL ...]
list list all installed tools
search search for tools in the catalog with the given pattern
install TOOL install some tools
update list all updatable tools
update TOOL update tools
update --all update all updatable tools
@@ -18,6 +20,7 @@ Options:
-v, --verbose
"""
import logging
import sys, os
from docopt import docopt
from yaml import load, SafeLoader
@@ -25,10 +28,14 @@ from semver import VersionInfo
import re
from subprocess import run, CalledProcessError, TimeoutExpired
from fuzzywuzzy import fuzz
import requests
def debug(*mess):
#print("DEBUG", mess)
pass
def trace(*mess):
if "TRACE" in os.environ:
print("TRACE", mess)
def warn(*mess):
print(f"WARN {mess[0]}")
class DotDict(dict):
def __getattr__(self, name):
@@ -36,7 +43,9 @@ class DotDict(dict):
def loadcatalog(options)->dict:
catalog=options.get('__catalog', 'catalog.yaml')
return load(open(catalog), SafeLoader)
o = load(open(catalog), SafeLoader)
trace(o)
return { k:DotDict(v) for k,v in o.items()}
def find_semver(s:str) -> VersionInfo:
ver = VersionInfo(0,0,0)
@@ -46,7 +55,7 @@ def find_semver(s:str) -> VersionInfo:
try:
ver = VersionInfo(*list(i.group(0) for i in re.finditer('\d+', s))[:3])
except Exception as e:
debug("parse error", e)
trace("parse error", e)
return ver
@@ -59,14 +68,14 @@ def _internal_list(options) -> tuple[str,VersionInfo]:
# search in path
try:
vers = _version(cli)
debug(cli, vers)
trace(cli, vers)
yield cli, props, find_semver(vers)
except CalledProcessError:
debug(cli, "call error")
trace(cli, "call error")
except TimeoutExpired:
debug(cli, "timeout")
trace(cli, "timeout")
except FileNotFoundError:
debug(cli, "not found")
trace(cli, "not found")
def dolist(options):
for (cli, _, ver) in _internal_list(options):
@@ -77,7 +86,7 @@ def dosearch(options):
ctl = loadcatalog(options)
L = []
for cli, props in ctl.items():
debug(cli, props)
trace(cli, props)
rtitle = fuzz.ratio(cli, pat)
rdesc = fuzz.partial_ratio(props['desc'], pat)
score = 2 * rtitle + rdesc
@@ -93,17 +102,44 @@ def dolistupdate(options):
if props.github:
pass
pass
def _gh_version(repo:str) -> [VersionInfo|None]:
[owner, repo] = repo.split("/")
url = f'https://api.github.com/repos/{owner}/{repo}/releases/latest'
response = requests.get(url)
return response.json().get("name")
def doinstall(options):
tools = options.TOOL
ctl = loadcatalog(options)
for tool in tools:
if tool in ctl:
props = ctl[tool]
if props.github:
vers = _gh_version(props.github)
trace(vers)
else:
warn(f'{tool} not in catalog')
if __name__ == '__main__':
if "DEBUG" in os.environ:
logging.basicConfig(level=logging.DEBUG)
logging.debug("debug is on")
else:
logging.info("not in debug")
options = docopt(__doc__, version='Cliget 0.1.0')
options = DotDict({k.replace('-','_'):v for (k,v) in options.items() if v is not None})
debug(options)
trace(options)
if options.list:
dolist(options)
elif options.search:
dosearch(options)
elif options.update:
elif options.install or options.update:
if not options.__all and len(options.TOOL)==0:
dolistupdate(options)
elif len(options.TOOL)>0:
doinstall(options)
else:
print("not implemented")