]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricatactl: Clean up parser, improve help
authorShivani Bhardwaj <shivanib134@gmail.com>
Wed, 20 Feb 2019 18:10:14 +0000 (23:40 +0530)
committerVictor Julien <victor@inliniac.net>
Sun, 24 Feb 2019 19:00:55 +0000 (20:00 +0100)
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
```

python/suricata/ctl/filestore.py
python/suricata/ctl/main.py

index 76ca5cb8b33ad12052b3e6340db280c9c51f8c2f..076e4ef310c1c80d295c4d83f61b602a5b0844b0 100644 (file)
@@ -31,10 +31,14 @@ class InvalidAgeFormatError(Exception):
 
 
 def register_args(parser):
-    parsers = parser.add_subparsers()
-    prune_parser = parsers.add_parser("prune")
-    prune_parser.add_argument("-d", "--directory", help="filestore directory")
-    prune_parser.add_argument("--age", help="prune files older than age")
+    subparser = parser.add_subparsers(help="sub-command help")
+    prune_parser = subparser.add_parser("prune",
+            help="Remove files in specified directory older than specified 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(
         "-n", "--dry-run", action="store_true", default=False,
         help="only print what would happen")
@@ -57,15 +61,13 @@ def parse_age(age):
         raise InvalidAgeFormatError(age)
     val = int(matched_age.group(1))
     unit = matched_age.group(2)
-    if unit == "s":
-        return val
-    if unit == "m":
-        return val * 60
-    if unit == "h":
-        return val * 60 * 60
-    if unit == "d":
-        return val * 60 * 60 * 24
-    raise InvalidAgeFormatError("bad unit: %s" % (unit))
+    ts_units = ["s", "m", "h", "d"]
+    try:
+        idx = ts_units.index(unit)
+    except ValueError:
+        raise InvalidAgeFormatError("bad unit: %s" % (unit))
+    multiplier = 60 ** idx if idx != 3 else 24 * 60 ** 2
+    return val * multiplier
 
 
 def get_filesize(path):
@@ -81,7 +83,6 @@ def remove_file(path, dry_run):
 
 
 def prune(args):
-
     if args.verbose:
         logger.setLevel(logging.DEBUG)
     if args.quiet:
@@ -106,7 +107,6 @@ def prune(args):
         # Do not go into the tmp directory.
         if "tmp" in dirnames:
             dirnames.remove("tmp")
-
         for filename in filenames:
             path = os.path.join(dirpath, filename)
             mtime = os.path.getmtime(path)
@@ -115,6 +115,5 @@ def prune(args):
                 logger.debug("Deleting %s; age=%ds", path, this_age)
                 size += remove_file(path, args.dry_run)
                 count += 1
-
     logger.info("Removed %d files; %d bytes.", count, size)
     return 0
index a8125f96c133b59df5791bf03b3595ffefeab679..d51a5be61e1cfc4fc3a389e101da56480c1e35e1 100644 (file)
@@ -34,11 +34,10 @@ def init_logger():
 
 def main():
     init_logger()
-    parser = argparse.ArgumentParser(description="Suricata Control Tool")
-    subparsers = parser.add_subparsers(
-        title="subcommands",
-        description="Commands")
-    filestore.register_args(subparsers.add_parser("filestore"))
+    parser = argparse.ArgumentParser()
+    subparsers = parser.add_subparsers(help='sub-command help')
+    fs_parser = subparsers.add_parser("filestore", help="Filestore related commands")
+    filestore.register_args(parser=fs_parser)
     args = parser.parse_args()
     try:
         func = args.func