Skip to content

Commit

Permalink
Merge pull request #603 from DannyBen/revert/9167c49
Browse files Browse the repository at this point in the history
Revert 9167c49 to allow calling `run ...` internally
  • Loading branch information
DannyBen authored Jan 12, 2025
2 parents f33c663 + bbfe15b commit cdf17b6
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions examples/dependencies-alt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


````
Expand Down
1 change: 1 addition & 0 deletions examples/internal-run/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
143 changes: 143 additions & 0 deletions examples/internal-run/README.md
Original file line number Diff line number Diff line change
@@ -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.

<!-- include: src/build_command.sh -->
<!-- include: src/test_command.sh -->
<!-- include: src/deploy_command.sh -->

-----

## `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


````



26 changes: 26 additions & 0 deletions examples/internal-run/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/internal-run/src/build_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "BUILD complete"
inspect_args
echo
13 changes: 13 additions & 0 deletions examples/internal-run/src/deploy_command.sh
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions examples/internal-run/src/test_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "TEST complete"
inspect_args
echo
11 changes: 11 additions & 0 deletions examples/internal-run/test.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/bashly/views/command/globals.gtx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= view_marker

> declare -g long_usage=''
> declare -g -A args=()

if catch_all_used_anywhere?
Expand Down
5 changes: 1 addition & 4 deletions lib/bashly/views/command/initialize.gtx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
= view_marker

> initialize() {
> version="<%= version %>"
> long_usage=''
> declare -g version="<%= version %>"
> {{ Settings.strict_string }}

if root_command?
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/bashly/views/command/run.gtx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
= view_marker

> run() {
= render(:globals).indent(2)
>
> normalize_input "$@"
> parse_requirements "${input[@]}"

Expand Down
56 changes: 56 additions & 0 deletions spec/approvals/examples/internal-run
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cdf17b6

Please sign in to comment.