Skip to content

Commit

Permalink
Added argcomplete to command line tools
Browse files Browse the repository at this point in the history
  • Loading branch information
avastthorsi committed Feb 1, 2022
1 parent 80351db commit 0b9c2b7
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 22 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,12 @@ Code review will be happening on github. If everything is nice, you should squas
```
git rebase --interactive
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
4 changes: 3 additions & 1 deletion caldera_control.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3

# PYTHON_ARGCOMPLETE_OK
""" A command line tool to control a caldera server """

import argparse
from pprint import pprint
import argcomplete

# from app.calderacontrol import CalderaControl
from app.calderaapi_4 import CalderaAPI
Expand Down Expand Up @@ -319,6 +320,7 @@ def create_parser():

if __name__ == "__main__":
parser = create_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()

print(args.caldera_url)
Expand Down
15 changes: 9 additions & 6 deletions doc_generator.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
#!/usr/bin/env python3

# PYTHON_ARGCOMPLETE_OK
""" Generate human readable document describing the attack based on an attack log """

import argparse
import argcomplete
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


def create_parser():
""" 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")
parser.add_argument("--outfile", default="tools/human_readable_documentation/source/contents.rst", help="The default output file")
lparser.add_argument("--attack_log", default=DEFAULT_ATTACK_LOG, help="The attack log the document is based on")
lparser.add_argument("--outfile", default="tools/human_readable_documentation/source/contents.rst", help="The default output file")

return parser
return lparser


if __name__ == "__main__":
arguments = create_parser().parse_args()
parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()

dg = DocGenerator()
dg.generate(arguments.attack_log, arguments.outfile)
16 changes: 10 additions & 6 deletions experiment_control.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" The main tool to run experiments """

import argparse
import argcomplete

from app.experimentcontrol import Experiment

Expand Down Expand Up @@ -36,11 +38,11 @@ def run(args):

def create_parser():
""" Creates the parser for the command line arguments"""
parser = argparse.ArgumentParser("Controls an experiment on the configured systems")
subparsers = parser.add_subparsers(help="sub-commands")
lparser = argparse.ArgumentParser("Controls an experiment on the configured systems")
subparsers = lparser.add_subparsers(help="sub-commands")

parser.set_defaults(func=explain)
parser.add_argument('--verbose', '-v', action='count', default=0)
lparser.set_defaults(func=explain)
lparser.add_argument('--verbose', '-v', action='count', default=0)

# Sub parser for machine creation
parser_run = subparsers.add_parser("run", help="run experiments")
Expand All @@ -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_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__":
arguments = create_parser().parse_args()
parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()
arguments.func(arguments)
5 changes: 4 additions & 1 deletion init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ sudo apt-get -y install latexmk texlive-fonts-recommended texlive-latex-recommen

python3 -m venv venv
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
4 changes: 3 additions & 1 deletion machine_control.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" Demo program to set up and control the machines """

import argparse
import argcomplete

import yaml

Expand Down Expand Up @@ -89,7 +91,7 @@ def create_parser():
if __name__ == "__main__":

parser = create_parser()

argcomplete.autocomplete(parser)
args = parser.parse_args()

args.func(args)
4 changes: 3 additions & 1 deletion plugin_manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
""" Managing plugins """

import argparse
import sys
import argcomplete

from app.pluginmanager import PluginManager
from app.attack_log import AttackLog
Expand Down Expand Up @@ -66,7 +68,7 @@ def create_parser():
if __name__ == "__main__":

parser = create_parser()

argcomplete.autocomplete(parser)
args = parser.parse_args()

exval = args.func(args)
Expand Down
14 changes: 9 additions & 5 deletions pydantic_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3

# PYTHON_ARGCOMPLETE_OK
""" A command line tool to verify PurpleDome configuration files """

import argparse
from pprint import pprint
import sys
import argcomplete

import yaml
from app.config_verifier import MainConfig

Expand All @@ -18,15 +20,17 @@ def load(filename):

def create_parser():
""" 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__":
arguments = create_parser().parse_args()
parser = create_parser()
argcomplete.autocomplete(parser)
arguments = parser.parse_args()
try:
r = load(arguments.filename)
except TypeError as e:
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ types-PyYAML==5.4.6
types-requests==2.25.6
types-simplejson==3.17.0
types-paramiko==2.7.0

# Argcomplete. See README.md
argcomplete==2.0.0
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ deps = -r requirements.txt
safety
bandit
pylint
argcomplete


commands =
Expand Down

0 comments on commit 0b9c2b7

Please sign in to comment.