|
| 1 | +# Config Example |
| 2 | + |
| 3 | +Demonstrates how to add functions for reading and writing INI files. |
| 4 | + |
| 5 | +This example was generated with: |
| 6 | + |
| 7 | +```bash |
| 8 | +$ bashly init |
| 9 | +# ... now edit src/bashly.yml to match the example ... |
| 10 | +$ bashly add config |
| 11 | +$ bashly generate |
| 12 | +# ... now edit all files in the src folder ... |
| 13 | +$ bashly generate |
| 14 | +``` |
| 15 | + |
| 16 | +Running the `bashly add config` command simply added the |
| 17 | +[src/lib/config.sh](src/lib/config.sh) file, which includes functions for |
| 18 | +reading and writing values from an INI file. |
| 19 | + |
| 20 | +See the files in the [src](src) folder for usage examples. |
| 21 | + |
| 22 | +<!-- include: config.ini src/get_command.sh src/list_command.sh src/set_command.sh --> |
| 23 | + |
| 24 | +----- |
| 25 | + |
| 26 | +## `bashly.yml` |
| 27 | + |
| 28 | +```yaml |
| 29 | +name: configly |
| 30 | +help: Sample application that uses the config functions |
| 31 | +version: 0.1.0 |
| 32 | + |
| 33 | +commands: |
| 34 | +- name: list |
| 35 | + alias: l |
| 36 | + help: Show the entire config file |
| 37 | + |
| 38 | +- name: get |
| 39 | + alias: g |
| 40 | + help: Read a value from the config file |
| 41 | + |
| 42 | + args: |
| 43 | + - name: key |
| 44 | + required: true |
| 45 | + help: Config key |
| 46 | + |
| 47 | + examples: |
| 48 | + - configly get hello |
| 49 | + - configly get user.name |
| 50 | + |
| 51 | +- name: set |
| 52 | + alias: s |
| 53 | + help: Save a value in the config file |
| 54 | + |
| 55 | + args: |
| 56 | + - name: key |
| 57 | + required: true |
| 58 | + help: Config key |
| 59 | + - name: value |
| 60 | + required: true |
| 61 | + help: Config value |
| 62 | + |
| 63 | + examples: |
| 64 | + - configly set hello world |
| 65 | + - configly set user.email me@example.com |
| 66 | + |
| 67 | +- name: del |
| 68 | + alias: d |
| 69 | + help: Remove a value from the config file |
| 70 | + |
| 71 | + args: |
| 72 | + - name: key |
| 73 | + required: true |
| 74 | + help: Config key |
| 75 | + |
| 76 | + examples: |
| 77 | + - configly del hello |
| 78 | + - configly del user.name |
| 79 | +``` |
| 80 | +
|
| 81 | +## `config.ini` |
| 82 | + |
| 83 | +```ini |
| 84 | +; comments are allowed, sections are optional |
| 85 | +hello = world |
| 86 | +bashly = works |
| 87 | +
|
| 88 | +[options] |
| 89 | +name = value for options.name |
| 90 | +path = value for options.path |
| 91 | +
|
| 92 | +[user] |
| 93 | +name = value for user.name |
| 94 | +email = value for user.email |
| 95 | +
|
| 96 | +``` |
| 97 | + |
| 98 | +## `src/get_command.sh` |
| 99 | + |
| 100 | +```bash |
| 101 | +# Using the standard library (lib/config.sh) to show a value from the config |
| 102 | +config_load config.ini |
| 103 | +
|
| 104 | +key="${args[key]}" |
| 105 | +value=${config[$key]} |
| 106 | +
|
| 107 | +if [[ "$value" ]]; then |
| 108 | + echo "$key = $value" |
| 109 | +else |
| 110 | + echo "No such key: $key" |
| 111 | +fi |
| 112 | +
|
| 113 | +``` |
| 114 | + |
| 115 | +## `src/list_command.sh` |
| 116 | + |
| 117 | +```bash |
| 118 | +# Using the standard library (lib/config.sh) to show the entire config file |
| 119 | +config_load config.ini |
| 120 | +config_show |
| 121 | +
|
| 122 | +## Or to iterate through keys manually |
| 123 | +# for key in $(config_keys); do |
| 124 | +# echo "$key = ${config[$key]}" |
| 125 | +# done |
| 126 | +``` |
| 127 | + |
| 128 | +## `src/set_command.sh` |
| 129 | + |
| 130 | +```bash |
| 131 | +# Using the standard library (lib/config.sh) to store a value to the config |
| 132 | +config_load config.ini |
| 133 | +
|
| 134 | +key="${args[key]}" |
| 135 | +value="${args[value]}" |
| 136 | +
|
| 137 | +config["$key"]="$value" |
| 138 | +config_save saved.ini |
| 139 | +cat saved.ini |
| 140 | +
|
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +## Generated script output |
| 145 | + |
| 146 | +### `$ ./configly -h` |
| 147 | + |
| 148 | +```shell |
| 149 | +configly - Sample application that uses the config functions |
| 150 | +
|
| 151 | +Usage: |
| 152 | + configly COMMAND |
| 153 | + configly [COMMAND] --help | -h |
| 154 | + configly --version | -v |
| 155 | +
|
| 156 | +Commands: |
| 157 | + list Show the entire config file |
| 158 | + get Read a value from the config file |
| 159 | + set Save a value in the config file |
| 160 | + del Remove a value from the config file |
| 161 | +
|
| 162 | +Options: |
| 163 | + --help, -h |
| 164 | + Show this help |
| 165 | +
|
| 166 | + --version, -v |
| 167 | + Show version number |
| 168 | +
|
| 169 | +
|
| 170 | +
|
| 171 | +``` |
| 172 | + |
| 173 | +### `$ ./configly set hello WORLD` |
| 174 | + |
| 175 | +```shell |
| 176 | +bashly = works |
| 177 | +hello = WORLD |
| 178 | +
|
| 179 | +[options] |
| 180 | +name = value for options.name |
| 181 | +path = value for options.path |
| 182 | +
|
| 183 | +[user] |
| 184 | +email = value for user.email |
| 185 | +name = value for user.name |
| 186 | +
|
| 187 | +
|
| 188 | +``` |
| 189 | + |
| 190 | +### `$ ./configly set user.name Megatron` |
| 191 | + |
| 192 | +```shell |
| 193 | +bashly = works |
| 194 | +hello = world |
| 195 | +
|
| 196 | +[options] |
| 197 | +name = value for options.name |
| 198 | +path = value for options.path |
| 199 | +
|
| 200 | +[user] |
| 201 | +email = value for user.email |
| 202 | +name = Megatron |
| 203 | +
|
| 204 | +
|
| 205 | +``` |
| 206 | + |
| 207 | +### `$ ./configly get hello` |
| 208 | + |
| 209 | +```shell |
| 210 | +hello = world |
| 211 | +
|
| 212 | +
|
| 213 | +``` |
| 214 | + |
| 215 | +### `$ ./configly get user.name` |
| 216 | + |
| 217 | +```shell |
| 218 | +user.name = value for user.name |
| 219 | +
|
| 220 | +
|
| 221 | +``` |
| 222 | + |
| 223 | +### `$ ./configly get invalid_key` |
| 224 | + |
| 225 | +```shell |
| 226 | +No such key: invalid_key |
| 227 | +
|
| 228 | +
|
| 229 | +``` |
| 230 | + |
| 231 | +### `$ ./configly del user.email` |
| 232 | + |
| 233 | +```shell |
| 234 | +bashly = works |
| 235 | +hello = world |
| 236 | +
|
| 237 | +[options] |
| 238 | +name = value for options.name |
| 239 | +path = value for options.path |
| 240 | +
|
| 241 | +[user] |
| 242 | +name = value for user.name |
| 243 | +
|
| 244 | +
|
| 245 | +``` |
| 246 | + |
| 247 | +### `$ ./configly list` |
| 248 | + |
| 249 | +```shell |
| 250 | +bashly = works |
| 251 | +hello = world |
| 252 | +options.name = value for options.name |
| 253 | +options.path = value for options.path |
| 254 | +user.email = value for user.email |
| 255 | +user.name = value for user.name |
| 256 | +
|
| 257 | +
|
| 258 | +``` |
| 259 | + |
| 260 | + |
| 261 | + |
0 commit comments