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

This commit is contained in:
2023-02-25 23:36:51 +01:00
parent b5b21f6ca9
commit dd9396cfdf
5 changed files with 117 additions and 29 deletions

View File

@@ -3,15 +3,19 @@
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 ...]
cliget [-v] [-c URL] versions TOOLS ...
cliget [-v] [-c URL] allversions TOOL
cliget [-v] [-c URL] install TOOLS ...
cliget [-v] [-c URL] update [--all] [TOOLS ...]
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
list list all installed tools
search search catalog for tools with the given pattern
versions TOOLS list current and latest versions of some tools
allversions TOOL list all versions of a tool
install TOOLS install some tools
update list all updatable tools
update TOOLS update tools
update --all update all updatable tools
Options:
@@ -41,13 +45,13 @@ class DotDict(dict):
def __getattr__(self, name):
return self[name] if name in self else None
def loadcatalog(options)->dict:
def _load_catalog(options)->dict:
catalog=options.get('__catalog', 'catalog.yaml')
o = load(open(catalog), SafeLoader)
trace(o)
return { k:DotDict(v) for k,v in o.items()}
def find_semver(s:str) -> VersionInfo:
def _find_semver(s:str) -> VersionInfo:
ver = VersionInfo(0,0,0)
try:
ver = VersionInfo.parse(s)
@@ -59,17 +63,24 @@ def find_semver(s:str) -> VersionInfo:
return ver
_version = lambda cmd: run([cmd, '--version'], input='', text=True, capture_output=True, check=True, timeout=0.1).stdout.split('\n')[0]
def _local_version(cmd):
ver = VersionInfo(0)
try:
first_line = run([cmd, '--version'], input='', text=True, capture_output=True, check=True, timeout=0.1).stdout.split('\n')[0]
ver = _find_semver(first_line)
except Exception as e:
trace("run error", e)
return ver
def _internal_list(options) -> tuple[str,VersionInfo]:
"""list installed tools and their version"""
ctl = loadcatalog(options)
ctl = _load_catalog(options)
for cli, props in ctl.items():
# search in path
try:
vers = _version(cli)
vers = _local_version(cli)
trace(cli, vers)
yield cli, props, find_semver(vers)
yield cli, props, vers
except CalledProcessError:
trace(cli, "call error")
except TimeoutExpired:
@@ -83,13 +94,13 @@ def dolist(options):
def dosearch(options):
pat = options.PAT
ctl = loadcatalog(options)
ctl = _load_catalog(options)
L = []
for cli, props in ctl.items():
trace(cli, props)
rtitle = fuzz.ratio(cli, pat)
rdesc = fuzz.partial_ratio(props['desc'], pat)
score = 2 * rtitle + rdesc
rdesc = fuzz.partial_token_set_ratio(props['desc'], pat)
score = 0.2 * rtitle + rdesc
L.append((cli, props['desc'], score))
L = sorted(L, key=lambda x: -x[-1])
# TODO format a as table
@@ -104,15 +115,48 @@ def dolistupdate(options):
pass
def _gh_versions(repo:str) -> [VersionInfo|None]:
[owner, repo] = repo.split("/")
url = f'https://api.github.com/repos/{owner}/{repo}/releases'
response = requests.get(url)
return [ _find_semver(o.get("tag_name")) for o in response.json()]
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")
return _find_semver(response.json().get("tag_name"))
def doversions(options):
tools = options.TOOLS
ctl = _load_catalog(options)
for cli in tools:
if cli in ctl:
props = ctl[cli]
rver = _gh_version(props.github) if props.github else VersionInfo(0)
lver = _local_version(cli)
trace(lver)
trace(rver)
print(f"{cli} | {lver} | {rver}")
else:
warn(f'{tool} not in catalog')
def doallversions(options):
tool = options.TOOL
ctl = _load_catalog(options)
if tool in ctl:
props = ctl[tool]
if props.github:
vers = _gh_versions(props.github)
trace(vers)
print("\n".join(vers))
else:
warn(f'{tool} not in catalog')
def doinstall(options):
tools = options.TOOL
ctl = loadcatalog(options)
tools = options.TOOLS
ctl = _load_catalog(options)
for tool in tools:
if tool in ctl:
props = ctl[tool]
@@ -122,6 +166,13 @@ def doinstall(options):
else:
warn(f'{tool} not in catalog')
def perform_install(cli, repo):
# get arch and os
# dl asset
# mkdirs
# unpack
# symlink
if __name__ == '__main__':
if "DEBUG" in os.environ:
@@ -136,10 +187,14 @@ if __name__ == '__main__':
dolist(options)
elif options.search:
dosearch(options)
elif options.versions:
doversions(options)
elif options.allversions(options):
doallversions(options)
elif options.install or options.update:
if not options.__all and len(options.TOOL)==0:
if not options.__all and len(options.TOOLS)==0:
dolistupdate(options)
elif len(options.TOOL)>0:
elif len(options.TOOLS)>0:
doinstall(options)
else:
print("not implemented")