Added argcomplete to command line tools

pull/30/head
Thorsten Sick 3 years ago
parent 80351db1dd
commit 0b9c2b7a8d

@ -183,4 +183,12 @@ Code review will be happening on github. If everything is nice, you should squas
``` ```
git rebase --interactive git rebase --interactive
git push --force git push --force
``` ```
### Argcomplete
https://kislyuk.github.io/argcomplete/
Is a argparse extension that registers the command line arguments for bash. It requires a command line command to register it globally. This is added to init.sh
The configuration will be set in /etc/bash_completion.d/ . Keep in mind: It will require a shell restart to be activated

@ -1,9 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" A command line tool to control a caldera server """ """ A command line tool to control a caldera server """
import argparse import argparse
from pprint import pprint from pprint import pprint
import argcomplete
# from app.calderacontrol import CalderaControl # from app.calderacontrol import CalderaControl
from app.calderaapi_4 import CalderaAPI from app.calderaapi_4 import CalderaAPI
@ -319,6 +320,7 @@ def create_parser():
if __name__ == "__main__": if __name__ == "__main__":
parser = create_parser() parser = create_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args() args = parser.parse_args()
print(args.caldera_url) print(args.caldera_url)

@ -1,8 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" Generate human readable document describing the attack based on an attack log """ """ Generate human readable document describing the attack based on an attack log """
import argparse import argparse
import argcomplete
from app.doc_generator import DocGenerator from app.doc_generator import DocGenerator
DEFAULT_ATTACK_LOG = "removeme/loot/2021_09_08___07_41_35/attack.json" # FIN 7 first run on environment DEFAULT_ATTACK_LOG = "removeme/loot/2021_09_08___07_41_35/attack.json" # FIN 7 first run on environment
@ -10,16 +11,18 @@ DEFAULT_ATTACK_LOG = "removeme/loot/2021_09_08___07_41_35/attack.json" # FIN 7
def create_parser(): def create_parser():
""" Creates the parser for the command line arguments""" """ Creates the parser for the command line arguments"""
parser = argparse.ArgumentParser("Controls an experiment on the configured systems") lparser = argparse.ArgumentParser("Controls an experiment on the configured systems")
parser.add_argument("--attack_log", default=DEFAULT_ATTACK_LOG, help="The attack log the document is based on") lparser.add_argument("--attack_log", default=DEFAULT_ATTACK_LOG, help="The attack log the document is based on")
parser.add_argument("--outfile", default="tools/human_readable_documentation/source/contents.rst", help="The default output file") lparser.add_argument("--outfile", default="tools/human_readable_documentation/source/contents.rst", help="The default output file")
return parser return lparser
if __name__ == "__main__": if __name__ == "__main__":
arguments = create_parser().parse_args() parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()
dg = DocGenerator() dg = DocGenerator()
dg.generate(arguments.attack_log, arguments.outfile) dg.generate(arguments.attack_log, arguments.outfile)

@ -1,7 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" The main tool to run experiments """ """ The main tool to run experiments """
import argparse import argparse
import argcomplete
from app.experimentcontrol import Experiment from app.experimentcontrol import Experiment
@ -36,11 +38,11 @@ def run(args):
def create_parser(): def create_parser():
""" Creates the parser for the command line arguments""" """ Creates the parser for the command line arguments"""
parser = argparse.ArgumentParser("Controls an experiment on the configured systems") lparser = argparse.ArgumentParser("Controls an experiment on the configured systems")
subparsers = parser.add_subparsers(help="sub-commands") subparsers = lparser.add_subparsers(help="sub-commands")
parser.set_defaults(func=explain) lparser.set_defaults(func=explain)
parser.add_argument('--verbose', '-v', action='count', default=0) lparser.add_argument('--verbose', '-v', action='count', default=0)
# Sub parser for machine creation # Sub parser for machine creation
parser_run = subparsers.add_parser("run", help="run experiments") parser_run = subparsers.add_parser("run", help="run experiments")
@ -49,9 +51,11 @@ def create_parser():
parser_run.add_argument("--caldera_attack", default=None, help="The id of a specific caldera attack to run, will override experiment configuration for attacks") parser_run.add_argument("--caldera_attack", default=None, help="The id of a specific caldera attack to run, will override experiment configuration for attacks")
parser_run.add_argument("--caldera_attack_file", default=None, help="The file name containing a list of caldera attacks to run, will override experiment configuration for attacks") parser_run.add_argument("--caldera_attack_file", default=None, help="The file name containing a list of caldera attacks to run, will override experiment configuration for attacks")
return parser return lparser
if __name__ == "__main__": if __name__ == "__main__":
arguments = create_parser().parse_args() parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()
arguments.func(arguments) arguments.func(arguments)

@ -13,4 +13,7 @@ sudo apt-get -y install latexmk texlive-fonts-recommended texlive-latex-recommen
python3 -m venv venv python3 -m venv venv
source venv/bin/activate source venv/bin/activate
pip3 install -r requirements.txt pip3 install -r requirements.txt
# Registering argcomplete globally. See README.md
sudo ./venv/bin/activate-global-python-argcomplete

@ -1,7 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" Demo program to set up and control the machines """ """ Demo program to set up and control the machines """
import argparse import argparse
import argcomplete
import yaml import yaml
@ -89,7 +91,7 @@ def create_parser():
if __name__ == "__main__": if __name__ == "__main__":
parser = create_parser() parser = create_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args() args = parser.parse_args()
args.func(args) args.func(args)

@ -1,8 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" Managing plugins """ """ Managing plugins """
import argparse import argparse
import sys import sys
import argcomplete
from app.pluginmanager import PluginManager from app.pluginmanager import PluginManager
from app.attack_log import AttackLog from app.attack_log import AttackLog
@ -66,7 +68,7 @@ def create_parser():
if __name__ == "__main__": if __name__ == "__main__":
parser = create_parser() parser = create_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args() args = parser.parse_args()
exval = args.func(args) exval = args.func(args)

@ -1,10 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" A command line tool to verify PurpleDome configuration files """ """ A command line tool to verify PurpleDome configuration files """
import argparse import argparse
from pprint import pprint from pprint import pprint
import sys import sys
import argcomplete
import yaml import yaml
from app.config_verifier import MainConfig from app.config_verifier import MainConfig
@ -18,15 +20,17 @@ def load(filename):
def create_parser(): def create_parser():
""" Creates the parser for the command line arguments""" """ Creates the parser for the command line arguments"""
parser = argparse.ArgumentParser("Parse a config file and verifies it") lparser = argparse.ArgumentParser("Parse a config file and verifies it")
parser.add_argument('--filename', default="experiment_ng.yaml") lparser.add_argument('--filename', default="experiment_ng.yaml")
return parser return lparser
if __name__ == "__main__": if __name__ == "__main__":
arguments = create_parser().parse_args() parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()
try: try:
r = load(arguments.filename) r = load(arguments.filename)
except TypeError as e: except TypeError as e:

@ -26,3 +26,6 @@ types-PyYAML==5.4.6
types-requests==2.25.6 types-requests==2.25.6
types-simplejson==3.17.0 types-simplejson==3.17.0
types-paramiko==2.7.0 types-paramiko==2.7.0
# Argcomplete. See README.md
argcomplete==2.0.0

@ -35,6 +35,7 @@ deps = -r requirements.txt
safety safety
bandit bandit
pylint pylint
argcomplete
commands = commands =

Loading…
Cancel
Save