bake icon indicating copy to clipboard operation
bake copied to clipboard

sub-tasks and command line swtiches

Open kyleburton opened this issue 9 years ago • 1 comments

It'd be nice if bake provided support for creating sub-tasks and for tasks supporting command line switches w/o having to write additional case statements or having to implement getopt loops in each of the commands.

kyleburton avatar Jan 31 '16 20:01 kyleburton

One way to implement this, sticking with the constraint that bash is 'flat' is to create a bake_subtask function that takes an additional argument and just does the concatenation for you:

bake_subtask docker start "Starts your docker container"
function docker-start () {
...
}

bake_subtask docker stop "Stops your docker container"
function docker-stop () {
...
}

This should hit the bulk of the usefulness, as it'd clean up the output of the 'help' to make it less noisy, and allow for better refactoring of the code.

Proposed implementation:

New array's: BAKE_SUPERTASKS, BAKE_SUBTASKS, and BAKE_SUBTASK_DESCRIPTIONS

'docker' would be the supertask in the above example. 'start' and 'stop' would be subtasks in the above example.

When producing the basic help (responding to 'help' or no arguments), bake would combine together all of the BAKE_TASKS and BAKE_SUPERTASKS to produce the flat, top-level listing. When running bake help supertask, it would print the all BAKE_SUBTASKS that match supertask-*.

On task execution, if the first argument to bake (typically the command) isn't in BAKE_TASKS, but is in BAKE_SUPERTASKS, concatenate $1 and $2 with a '-' and pull it from BAKE_SUBTASKS

PRO: implementation is straight-forward CON: using a '-' as the super/sub task separator still allows for name conflicts if you define tasks. This is a trade-off given that bash has no namespaces, so it'll remain a gotcha for the tooling.

TODO: add new arrays TODO: bake_subtask: on register, ensure the supertask is tracked TODO: bake_subtask: add subtask & description into tracking array TODO: bake_bakefile_help: combine tasks & supertasks into a single list TODO: bake_run: recognize 'help cmd' TODO: bake_bakefile_help: fallback to BAKE_SUBTASKS when $task is not in BAKE_TASKS TODO: bake_run: recognize cmd + subcmd TODO: bake_is_registered_supertask TODO: bake_is_registered_subtask TODO: bake_run: defer the shift, if bake_is_registered_task returns false, try bake_is_registered_subtask "$@"

Optional Extension:

New array: BAKE_SUPERTASK_DESCRIPTIONS and a new command: bake_supertask

These would allow the supertask to have a registered help string independent of its sub-tasks.

kyleburton avatar Mar 03 '16 16:03 kyleburton