From 6b65efd8c7985388c33069268bb1737ecd1c06e3 Mon Sep 17 00:00:00 2001 From: ducklet Date: Fri, 19 Aug 2022 21:36:30 +0200 Subject: [PATCH] add `ls` option `--stop-on-error` --- metadex/__main__.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/metadex/__main__.py b/metadex/__main__.py index 85bd8a6..cf8d819 100644 --- a/metadex/__main__.py +++ b/metadex/__main__.py @@ -148,6 +148,7 @@ def getargs(): parser_ls.add_argument( "--match", choices=("regex", "glob", "fuzzy"), default="glob" ) + parser_ls.add_argument("--stop-on-error", action="store_true") # Parse args. @@ -220,8 +221,9 @@ def cmd_rm(args): metadex.close() +def cmd_ls(args) -> int: + return_code = 0 -def cmd_ls(args): metadex.init(args.db) args.file = [f for f in args.file if f] @@ -236,7 +238,10 @@ def cmd_ls(args): for pathspec in args.file: + is_match = False for file in metadex.ls(pathspec, type=args.type, match=args.match): + is_match = True + date = file.stat_modified.isoformat(sep=" ", timespec="seconds") size = utils.size_for_display( file.stat_bytes, precision=1, format="compact" @@ -255,9 +260,19 @@ def cmd_ls(args): ", ".join(keys), exc_info=err, ) - return 5 + return_code = 5 + return return_code print(out) + if not is_match: + log.error("No match: %a", pathspec) + return_code = 3 + + if args.stop_on_error: + return return_code + + return return_code + def is_stdout_piped(): s = os.fstat(sys.stdout.fileno()) @@ -295,7 +310,8 @@ def main(): log.info( "If the hostname cannot be found automatically, try setting it using --hostname." ) - return 2 + return_code = 2 + return return_code if config.dryrun: log.info(f"--- DRY RUN ---")