Skip to content

Breaking: Rewrite config (INI) library to provide an associative array #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions examples/command-paths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,37 @@ Options:

```

### `$ ./docker download something`
### `$ ./docker container run alpine`

```shell
invalid command: download
# this file is located in 'src/commands/container/run.sh'
# code for 'docker container run' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[image]} = alpine


```

### `$ ls -1 src/*`
### `$ ls -R src`

```shell
src/bashly.yml
src:
bashly.yml
commands

src/commands:
container
image
ps.sh

src/commands/container:
run.sh
stop.sh

src/commands/image:
ls.sh


```

Expand Down
143 changes: 107 additions & 36 deletions examples/config-ini/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ $ bashly generate
$ bashly generate
```

Running the `bashly add config` command simply added the [src/lib/config.sh](src/lib/config.sh) file, which includes functions for reading and writing values from an INI file.
Running the `bashly add config` command simply added the
[src/lib/config.sh](src/lib/config.sh) file, which includes functions for
reading and writing values from an INI file.

See the files in the [src](src) folder for usage examples.

Expand All @@ -29,6 +31,23 @@ help: Sample application that uses the config functions
version: 0.1.0

commands:
- name: list
alias: l
help: Show the entire config file

- name: get
alias: g
help: Read a value from the config file

args:
- name: key
required: true
help: Config key

examples:
- configly get hello
- configly get user.name

- name: set
alias: s
help: Save a value in the config file
Expand All @@ -43,72 +62,81 @@ commands:

examples:
- configly set hello world
- configly set user.email [email protected]

- name: get
alias: g
help: Read a value from the config file
- name: del
alias: d
help: Remove a value from the config file

args:
- name: key
required: true
help: Config key

examples:
- configly set hello

- name: list
alias: l
help: Show the entire config file
- configly del hello
- configly del user.name
```

## `config.ini`

```ini
; comments are allowed
; comments are allowed, sections are optional
hello = world
bashly = works

[options]
name = value for options.name
path = value for options.path

[user]
name = value for user.name
email = value for user.email

```

## `src/get_command.sh`

```bash
# Using the standard library (lib/config.sh) to show a value from the config
config_load config.ini

key="${args[key]}"
if config_has_key "$key"; then
config_get "$key"
value=${config[$key]}

if [[ "$value" ]]; then
echo "$key = $value"
else
echo "No such key: $key"
fi

# Example of how to assign the config value to a variable:
# result=$(config_get "${args[key]}")
# echo $result


```

## `src/list_command.sh`

```bash
# Using the standard library (lib/config.sh) to show the entire config file
config_load config.ini
config_show

# Or to iterate through keys
for key in $(config_keys); do
echo "$key === $(config_get "$key")"
done

## Or to iterate through keys manually
# for key in $(config_keys); do
# echo "$key = ${config[$key]}"
# done
```

## `src/set_command.sh`

```bash
# Using the standard library (lib/config.sh) to store a value to the config
config_set "${args[key]}" "${args[value]}"
echo "saved: ${args[key]} = ${args[value]}"
config_load config.ini

key="${args[key]}"
value="${args[value]}"

config["$key"]="$value"
config_save saved.ini
cat saved.ini

```

Expand All @@ -126,9 +154,10 @@ Usage:
configly --version | -v

Commands:
set Save a value in the config file
get Read a value from the config file
list Show the entire config file
get Read a value from the config file
set Save a value in the config file
del Remove a value from the config file

Options:
--help, -h
Expand All @@ -141,26 +170,52 @@ Options:

```

### `$ ./configly set hello world`
### `$ ./configly set hello WORLD`

```shell
saved: hello = world
bashly = works
hello = WORLD

[options]
name = value for options.name
path = value for options.path

[user]
email = value for user.email
name = value for user.name


```

### `$ ./configly set bashly works`
### `$ ./configly set user.name Megatron`

```shell
saved: bashly = works
bashly = works
hello = world

[options]
name = value for options.name
path = value for options.path

[user]
email = value for user.email
name = Megatron


```

### `$ ./configly get hello`

```shell
world
hello = world


```

### `$ ./configly get user.name`

```shell
user.name = value for user.name


```
Expand All @@ -173,15 +228,31 @@ No such key: invalid_key

```

### `$ ./configly list`
### `$ ./configly del user.email`

```shell
; comments are allowed
hello = world
bashly = works
hello = world

[options]
name = value for options.name
path = value for options.path

[user]
name = value for user.name


hello === world
bashly === works
```

### `$ ./configly list`

```shell
bashly = works
hello = world
options.name = value for options.name
options.path = value for options.path
user.email = value for user.email
user.name = value for user.name


```
Expand Down
9 changes: 8 additions & 1 deletion examples/config-ini/config.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
; comments are allowed
; comments are allowed, sections are optional
hello = world
bashly = works

[options]
name = value for options.name
path = value for options.path

[user]
name = value for user.name
email = value for user.email
9 changes: 9 additions & 0 deletions examples/config-ini/saved.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bashly = works
hello = world

[options]
name = value for options.name
path = value for options.path

[user]
name = value for user.name
31 changes: 23 additions & 8 deletions examples/config-ini/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ help: Sample application that uses the config functions
version: 0.1.0

commands:
- name: list
alias: l
help: Show the entire config file

- name: get
alias: g
help: Read a value from the config file

args:
- name: key
required: true
help: Config key

examples:
- configly get hello
- configly get user.name

- name: set
alias: s
help: Save a value in the config file
Expand All @@ -17,19 +34,17 @@ commands:

examples:
- configly set hello world
- configly set user.email [email protected]

- name: get
alias: g
help: Read a value from the config file
- name: del
alias: d
help: Remove a value from the config file

args:
- name: key
required: true
help: Config key

examples:
- configly set hello

- name: list
alias: l
help: Show the entire config file
- configly del hello
- configly del user.name
9 changes: 9 additions & 0 deletions examples/config-ini/src/del_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Using the standard library (lib/config.sh) to delete a value from the config
config_load config.ini

key="${args[key]}"
unset "config[$key]"

config_save saved.ini
cat saved.ini

12 changes: 5 additions & 7 deletions examples/config-ini/src/get_command.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Using the standard library (lib/config.sh) to show a value from the config
config_load config.ini

key="${args[key]}"
if config_has_key "$key"; then
config_get "$key"
value=${config[$key]}

if [[ "$value" ]]; then
echo "$key = $value"
else
echo "No such key: $key"
fi

# Example of how to assign the config value to a variable:
# result=$(config_get "${args[key]}")
# echo $result

Loading