|
| 1 | +# Internal Run Example |
| 2 | + |
| 3 | +This example demonstrates how to use the `run` function to call commands |
| 4 | +internally, using the exact same syntax as a user would in the CLI. This |
| 5 | +approach can be useful for chaining commands or reusing logic without |
| 6 | +duplicating code. |
| 7 | + |
| 8 | +This example was generated with: |
| 9 | + |
| 10 | +```bash |
| 11 | +$ bashly init |
| 12 | +# ... now edit src/bashly.yml to match the example ... |
| 13 | +$ bashly generate |
| 14 | +# ... now edit the generated command files to match the example ... |
| 15 | +$ bashly generate |
| 16 | +``` |
| 17 | + |
| 18 | +Note that while this pattern of calling `run` internally is supported, you may |
| 19 | +want to consider using [lib functions] |
| 20 | +instead, for a more robust codebase. |
| 21 | + |
| 22 | +While this pattern of calling run internally is supported, you may want to |
| 23 | +consider using [lib functions](https://bashly.dannyb.co/usage/writing-your-scripts/#adding-common-functions) |
| 24 | +instead. lib functions can help create a more robust and maintainable codebase |
| 25 | +by centralizing reusable logic. |
| 26 | + |
| 27 | +<!-- include: src/build_command.sh --> |
| 28 | +<!-- include: src/test_command.sh --> |
| 29 | +<!-- include: src/deploy_command.sh --> |
| 30 | + |
| 31 | +----- |
| 32 | + |
| 33 | +## `bashly.yml` |
| 34 | + |
| 35 | +````yaml |
| 36 | +name: cli |
| 37 | +help: Internal run test |
| 38 | +version: 0.1.0 |
| 39 | + |
| 40 | +commands: |
| 41 | +- name: build |
| 42 | + help: Build code |
| 43 | + args: |
| 44 | + - name: env |
| 45 | + help: Build environment |
| 46 | + default: development |
| 47 | + allowed: [development, production] |
| 48 | + |
| 49 | +- name: test |
| 50 | + help: Test code |
| 51 | + flags: |
| 52 | + - long: --full |
| 53 | + help: Run all tests |
| 54 | + |
| 55 | +- name: deploy |
| 56 | + help: Deploy code |
| 57 | + flags: |
| 58 | + - long: --build |
| 59 | + help: Build before deploying |
| 60 | + - long: --test |
| 61 | + help: Test before deploying |
| 62 | +```` |
| 63 | + |
| 64 | +## `src/build_command.sh` |
| 65 | + |
| 66 | +````bash |
| 67 | +echo "BUILD complete" |
| 68 | +inspect_args |
| 69 | +echo |
| 70 | +```` |
| 71 | + |
| 72 | + |
| 73 | +## Output |
| 74 | + |
| 75 | +### `$ ./cli -h` |
| 76 | + |
| 77 | +````shell |
| 78 | +cli - Internal run test |
| 79 | + |
| 80 | +Usage: |
| 81 | + cli COMMAND |
| 82 | + cli [COMMAND] --help | -h |
| 83 | + cli --version | -v |
| 84 | + |
| 85 | +Commands: |
| 86 | + build Build code |
| 87 | + test Test code |
| 88 | + deploy Deploy code |
| 89 | + |
| 90 | +Options: |
| 91 | + --help, -h |
| 92 | + Show this help |
| 93 | + |
| 94 | + --version, -v |
| 95 | + Show version number |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +```` |
| 100 | + |
| 101 | +### `$ ./cli deploy -h` |
| 102 | + |
| 103 | +````shell |
| 104 | +cli deploy - Deploy code |
| 105 | + |
| 106 | +Usage: |
| 107 | + cli deploy [OPTIONS] |
| 108 | + cli deploy --help | -h |
| 109 | + |
| 110 | +Options: |
| 111 | + --build |
| 112 | + Build before deploying |
| 113 | + |
| 114 | + --test |
| 115 | + Test before deploying |
| 116 | + |
| 117 | + --help, -h |
| 118 | + Show this help |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +```` |
| 123 | + |
| 124 | +### `$ ./cli deploy --build --test` |
| 125 | + |
| 126 | +````shell |
| 127 | +BUILD complete |
| 128 | +args: |
| 129 | +- ${args[env]} = production |
| 130 | + |
| 131 | +TEST complete |
| 132 | +args: |
| 133 | +- ${args[--full]} = 1 |
| 134 | + |
| 135 | +DEPLOY complete |
| 136 | +args: |
| 137 | +- ${args[--full]} = 1 |
| 138 | + |
| 139 | + |
| 140 | +```` |
| 141 | + |
| 142 | + |
| 143 | + |
0 commit comments