Skip to content

Commit

Permalink
Add handling of subcommands and subcommand/option validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cinek810 committed Jan 31, 2022
1 parent c7a12ab commit b11d2b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
32 changes: 28 additions & 4 deletions ansible_deploy/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def get_sub_command(command):
"""Function to check the first arguments (argv[1..]) looking for a subcommand"""
if command == "run":
subcommand = "run"
elif command == "lock":
subcommand = "lock"
elif command == "unlock":
subcommand = "unlock"
elif command == "list":
subcommand = "list"
else:
logger.error("Unknown subcommand :%s", (command))
sys.exit("55")
Expand Down Expand Up @@ -99,11 +105,29 @@ def create_workdir(timestamp: str, base_dir: str):
def validate_options(options, subcommand):
"""Function checking if the options set are allowed in this subcommand"""
logger.debug("validate_options running for subcommand: %s", (subcommand))
required = []
notsupported = []
if subcommand == "run":
if not options["task"]:
logger.error("run requires --task")
sys.exit(55)
return True
required = ["task", "infra", "stage"]
elif subcommand in ("lock", "unlock"):
required = ["infra", "stage"]
notsupported = ["task", "commit"]
elif subcommand == "list":
notsupported = ["commit"]

failed = False
for req in required:
if options[req] is None:
logger.error("%s is required for %s", req, subcommand)
failed = True

for notsup in notsupported:
if options[notsup] is not None:
logger.error("%s is not supported by %s", notsup, subcommand)
failed = True

if failed:
sys.exit(55)

def load_configuration():
"""Function responsible for reading configuration files and running a schema validator against
Expand Down
27 changes: 26 additions & 1 deletion tests/01-test_argument_parsing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ check_output() {
fi
}

#Check wrong combinations
check_output 'ansible-deploy' '\[ERROR\]: To fee arguments'
check_output 'ansible-deploy run' '\[ERROR\]: run requires --task'
check_output 'ansible-deploy run' '\[ERROR\]: task is required for run'
check_output 'ansible-deploy run' '\[ERROR\]: infra is required for run'
check_output 'ansible-deploy run' '\[ERROR\]: stage is required for run'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: stage is required for run'
check_output 'ansible-deploy run -t testTask' '\[ERROR\]: infra is required for run'
check_output 'ansible-deploy run -t testTask -i testInfra' 'stage is required for run'

check_output 'ansible-deploy lock -t testTask -i testInfra' '\[ERROR\]: task is not supported by lock'
check_output 'ansible-deploy lock -t testTask -i testInfra -s prod' '\[ERROR\]: task is not supported by lock'
check_output 'ansible-deploy lock -t testTask -s prod' '\[ERROR\]: infra is required for lock'
check_output 'ansible-deploy lock -t testTask -s prod -c X' '\[ERROR\]: commit is not supported by lock'

check_output 'ansible-deploy unlock -t testTask -i testInfra' '\[ERROR\]: task is not supported by unlock'
check_output 'ansible-deploy unlock -t testTask -i testInfra -s prod' '\[ERROR\]: task is not supported by unlock'
check_output 'ansible-deploy unlock -t testTask -s test' '\[ERROR\]: infra is required for unlock'
check_output 'ansible-deploy unlock -t testTask -s prod -c X' '\[ERROR\]: commit is not supported by unlock'

check_output 'ansible-deploy list --commit testTask' '\[ERROR\]: commit is not supported by list'

#Check if correct combinations are accepted
ansible-deploy run -t run_bin_true -s prod -i test1
ansible-deploy run -t run_bin_true -s prod -i test1 --commit test_version
ansible-deploy lock -s prod -i test1
ansible-deploy unlock -s prod -i test1
ansible-deploy list
ansible-deploy list --task="run_bin_true"

0 comments on commit b11d2b9

Please sign in to comment.