diff --git a/examples/README.md b/examples/README.md index 10a16d1d..7287792f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -42,6 +42,7 @@ Each of these examples demonstrates one aspect or feature of bashly. - [commands-expose](commands-expose#readme) - showing subcommands in the parent's help - [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags - [command-examples-on-error](command-examples-on-error#readme) - showing examples on error +- [internal-run](internal-run#readme) - calling other commands internally ## Customization diff --git a/examples/dependencies-alt/README.md b/examples/dependencies-alt/README.md index e1beb362..269dfd7c 100644 --- a/examples/dependencies-alt/README.md +++ b/examples/dependencies-alt/README.md @@ -52,16 +52,6 @@ commands: ### `$ ./cli download` ````shell -# This file is located at 'src/download_command.sh'. -# It contains the implementation for the 'cli download' command. -# The code you write here will be wrapped by a function named 'cli_download_command()'. -# Feel free to edit this file; your changes will persist when regenerating. -args: none - -deps: -- ${deps[git]} = /usr/bin/git -- ${deps[http_client]} = /usr/bin/curl -- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.3.6/bin/ruby ```` diff --git a/examples/internal-run/.gitignore b/examples/internal-run/.gitignore new file mode 100644 index 00000000..76ec9f59 --- /dev/null +++ b/examples/internal-run/.gitignore @@ -0,0 +1 @@ +cli \ No newline at end of file diff --git a/examples/internal-run/README.md b/examples/internal-run/README.md new file mode 100644 index 00000000..abb69607 --- /dev/null +++ b/examples/internal-run/README.md @@ -0,0 +1,143 @@ +# Internal Run Example + +This example demonstrates how to use the `run` function to call commands +internally, using the exact same syntax as a user would in the CLI. This +approach can be useful for chaining commands or reusing logic without +duplicating code. + +This example was generated with: + +```bash +$ bashly init +# ... now edit src/bashly.yml to match the example ... +$ bashly generate +# ... now edit the generated command files to match the example ... +$ bashly generate +``` + +Note that while this pattern of calling `run` internally is supported, you may +want to consider using [lib functions] +instead, for a more robust codebase. + +While this pattern of calling run internally is supported, you may want to +consider using [lib functions](https://bashly.dannyb.co/usage/writing-your-scripts/#adding-common-functions) +instead. lib functions can help create a more robust and maintainable codebase +by centralizing reusable logic. + + + + + +----- + +## `bashly.yml` + +````yaml +name: cli +help: Internal run test +version: 0.1.0 + +commands: +- name: build + help: Build code + args: + - name: env + help: Build environment + default: development + allowed: [development, production] + +- name: test + help: Test code + flags: + - long: --full + help: Run all tests + +- name: deploy + help: Deploy code + flags: + - long: --build + help: Build before deploying + - long: --test + help: Test before deploying +```` + +## `src/build_command.sh` + +````bash +echo "BUILD complete" +inspect_args +echo +```` + + +## Output + +### `$ ./cli -h` + +````shell +cli - Internal run test + +Usage: + cli COMMAND + cli [COMMAND] --help | -h + cli --version | -v + +Commands: + build Build code + test Test code + deploy Deploy code + +Options: + --help, -h + Show this help + + --version, -v + Show version number + + + +```` + +### `$ ./cli deploy -h` + +````shell +cli deploy - Deploy code + +Usage: + cli deploy [OPTIONS] + cli deploy --help | -h + +Options: + --build + Build before deploying + + --test + Test before deploying + + --help, -h + Show this help + + + +```` + +### `$ ./cli deploy --build --test` + +````shell +BUILD complete +args: +- ${args[env]} = production + +TEST complete +args: +- ${args[--full]} = 1 + +DEPLOY complete +args: +- ${args[--full]} = 1 + + +```` + + + diff --git a/examples/internal-run/src/bashly.yml b/examples/internal-run/src/bashly.yml new file mode 100644 index 00000000..5558f729 --- /dev/null +++ b/examples/internal-run/src/bashly.yml @@ -0,0 +1,26 @@ +name: cli +help: Internal run test +version: 0.1.0 + +commands: +- name: build + help: Build code + args: + - name: env + help: Build environment + default: development + allowed: [development, production] + +- name: test + help: Test code + flags: + - long: --full + help: Run all tests + +- name: deploy + help: Deploy code + flags: + - long: --build + help: Build before deploying + - long: --test + help: Test before deploying diff --git a/examples/internal-run/src/build_command.sh b/examples/internal-run/src/build_command.sh new file mode 100644 index 00000000..3ef442f3 --- /dev/null +++ b/examples/internal-run/src/build_command.sh @@ -0,0 +1,3 @@ +echo "BUILD complete" +inspect_args +echo \ No newline at end of file diff --git a/examples/internal-run/src/deploy_command.sh b/examples/internal-run/src/deploy_command.sh new file mode 100644 index 00000000..aebadb99 --- /dev/null +++ b/examples/internal-run/src/deploy_command.sh @@ -0,0 +1,13 @@ +# We must record the `args` array to our local variables, due to the fact +# that calling `run` will reset it. +build=${args['--build']} +test=${args['--test']} + +# Call other commands in the same way they would be called in the CLI. +[[ $build ]] && run build production +[[ $test ]] && run test --full + +# Perform the purpose of this command. +echo "DEPLOY complete" +inspect_args + diff --git a/examples/internal-run/src/test_command.sh b/examples/internal-run/src/test_command.sh new file mode 100644 index 00000000..f74299ed --- /dev/null +++ b/examples/internal-run/src/test_command.sh @@ -0,0 +1,3 @@ +echo "TEST complete" +inspect_args +echo \ No newline at end of file diff --git a/examples/internal-run/test.sh b/examples/internal-run/test.sh new file mode 100644 index 00000000..d596d1a3 --- /dev/null +++ b/examples/internal-run/test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -x + +bundle exec bashly generate + +### Try Me ### + +./cli -h +./cli deploy -h +./cli deploy --build --test diff --git a/lib/bashly/views/command/globals.gtx b/lib/bashly/views/command/globals.gtx index 1b98bbbd..7db9bc71 100644 --- a/lib/bashly/views/command/globals.gtx +++ b/lib/bashly/views/command/globals.gtx @@ -1,5 +1,5 @@ = view_marker - +> declare -g long_usage='' > declare -g -A args=() if catch_all_used_anywhere? diff --git a/lib/bashly/views/command/initialize.gtx b/lib/bashly/views/command/initialize.gtx index 3d1502f2..272b0bc1 100644 --- a/lib/bashly/views/command/initialize.gtx +++ b/lib/bashly/views/command/initialize.gtx @@ -1,8 +1,7 @@ = view_marker > initialize() { -> version="<%= version %>" -> long_usage='' +> declare -g version="<%= version %>" > {{ Settings.strict_string }} if root_command? @@ -11,8 +10,6 @@ if root_command? = render(:environment_variables_default).indent 2 end -= render(:globals).indent(2) - if user_file_exist?('initialize') > = load_user_file('initialize').indent 2 diff --git a/lib/bashly/views/command/run.gtx b/lib/bashly/views/command/run.gtx index c026597b..975e49ed 100644 --- a/lib/bashly/views/command/run.gtx +++ b/lib/bashly/views/command/run.gtx @@ -1,6 +1,8 @@ = view_marker > run() { += render(:globals).indent(2) +> > normalize_input "$@" > parse_requirements "${input[@]}" diff --git a/spec/approvals/examples/internal-run b/spec/approvals/examples/internal-run new file mode 100644 index 00000000..96ae75b9 --- /dev/null +++ b/spec/approvals/examples/internal-run @@ -0,0 +1,56 @@ ++ bundle exec bashly generate +creating user files in src +skipped src/build_command.sh (exists) +skipped src/test_command.sh (exists) +skipped src/deploy_command.sh (exists) +created ./cli +run ./cli --help to test your bash script ++ ./cli -h +cli - Internal run test + +Usage: + cli COMMAND + cli [COMMAND] --help | -h + cli --version | -v + +Commands: + build Build code + test Test code + deploy Deploy code + +Options: + --help, -h + Show this help + + --version, -v + Show version number + ++ ./cli deploy -h +cli deploy - Deploy code + +Usage: + cli deploy [OPTIONS] + cli deploy --help | -h + +Options: + --build + Build before deploying + + --test + Test before deploying + + --help, -h + Show this help + ++ ./cli deploy --build --test +BUILD complete +args: +- ${args[env]} = production + +TEST complete +args: +- ${args[--full]} = 1 + +DEPLOY complete +args: +- ${args[--full]} = 1