suricatactl: Clean up parser, improve help

So far the suricatactl parser was unclear about the options to use and
did not well display the required and optional param difference. Fix
that to make it legible for any user.

Before
```
└─ $ ▶ ./bin/suricatactl filestore -h
usage: suricatactl filestore [-h] {prune} ...

positional arguments:
  {prune}

optional arguments:
  -h, --help  show this help message and exit

└─ $ ▶ ./bin/suricatactl filestore prune -h
usage: suricatactl filestore prune [-h] [-d DIRECTORY] [--age AGE] [-n] [-v]
                                   [-q]

optional arguments:
  -h, --help            show this help message and exit
  -d DIRECTORY, --directory DIRECTORY
                        filestore directory
  --age AGE             prune files older than age
  -n, --dry-run         only print what would happen
  -v, --verbose         increase verbosity
  -q, --quiet           be quiet, log warnings and errors only
```

After
```
└─ $ ▶ ./bin/suricatactl filestore -h
usage: suricatactl filestore [-h] {prune} ...

positional arguments:
  {prune}     sub-command help
    prune     Remove files in specified directory older than specified age

optional arguments:
  -h, --help  show this help message and exit

└─ $ ▶ ./bin/suricatactl filestore prune -h
usage: suricatactl filestore prune [-h] -d DIRECTORY [--age AGE] [-n] [-v]
                                   [-q]

optional arguments:
  -h, --help            show this help message and exit
  -n, --dry-run         only print what would happen
  -v, --verbose         increase verbosity
  -q, --quiet           be quiet, log warnings and errors only

required arguments:
  -d DIRECTORY, --directory DIRECTORY
                        filestore directory
  --age AGE             prune files older than age, units: s, m, h, d
```
pull/3680/head
Shivani Bhardwaj 7 years ago committed by Victor Julien
parent 2b05f315e1
commit 342f3d5eec

@ -31,10 +31,14 @@ class InvalidAgeFormatError(Exception):
def register_args(parser): def register_args(parser):
parsers = parser.add_subparsers() subparser = parser.add_subparsers(help="sub-command help")
prune_parser = parsers.add_parser("prune") prune_parser = subparser.add_parser("prune",
prune_parser.add_argument("-d", "--directory", help="filestore directory") help="Remove files in specified directory older than specified age")
prune_parser.add_argument("--age", help="prune files older than age") required_args = prune_parser.add_argument_group("required arguments")
required_args.add_argument("-d", "--directory",
help="filestore directory", required=True)
required_args.add_argument("--age",
help="prune files older than age, units: s, m, h, d")
prune_parser.add_argument( prune_parser.add_argument(
"-n", "--dry-run", action="store_true", default=False, "-n", "--dry-run", action="store_true", default=False,
help="only print what would happen") help="only print what would happen")
@ -57,15 +61,13 @@ def parse_age(age):
raise InvalidAgeFormatError(age) raise InvalidAgeFormatError(age)
val = int(matched_age.group(1)) val = int(matched_age.group(1))
unit = matched_age.group(2) unit = matched_age.group(2)
if unit == "s": ts_units = ["s", "m", "h", "d"]
return val try:
if unit == "m": idx = ts_units.index(unit)
return val * 60 except ValueError:
if unit == "h": raise InvalidAgeFormatError("bad unit: %s" % (unit))
return val * 60 * 60 multiplier = 60 ** idx if idx != 3 else 24 * 60 ** 2
if unit == "d": return val * multiplier
return val * 60 * 60 * 24
raise InvalidAgeFormatError("bad unit: %s" % (unit))
def get_filesize(path): def get_filesize(path):
@ -81,7 +83,6 @@ def remove_file(path, dry_run):
def prune(args): def prune(args):
if args.verbose: if args.verbose:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
if args.quiet: if args.quiet:
@ -106,7 +107,6 @@ def prune(args):
# Do not go into the tmp directory. # Do not go into the tmp directory.
if "tmp" in dirnames: if "tmp" in dirnames:
dirnames.remove("tmp") dirnames.remove("tmp")
for filename in filenames: for filename in filenames:
path = os.path.join(dirpath, filename) path = os.path.join(dirpath, filename)
mtime = os.path.getmtime(path) mtime = os.path.getmtime(path)
@ -115,6 +115,5 @@ def prune(args):
logger.debug("Deleting %s; age=%ds", path, this_age) logger.debug("Deleting %s; age=%ds", path, this_age)
size += remove_file(path, args.dry_run) size += remove_file(path, args.dry_run)
count += 1 count += 1
logger.info("Removed %d files; %d bytes.", count, size) logger.info("Removed %d files; %d bytes.", count, size)
return 0 return 0

@ -34,11 +34,10 @@ def init_logger():
def main(): def main():
init_logger() init_logger()
parser = argparse.ArgumentParser(description="Suricata Control Tool") parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers( subparsers = parser.add_subparsers(help='sub-command help')
title="subcommands", fs_parser = subparsers.add_parser("filestore", help="Filestore related commands")
description="Commands") filestore.register_args(parser=fs_parser)
filestore.register_args(subparsers.add_parser("filestore"))
args = parser.parse_args() args = parser.parse_args()
try: try:
func = args.func func = args.func

Loading…
Cancel
Save