thin-edge.io
thin-edge.io copied to clipboard
easily add/remove values to existing tedge config properties of an array type
Is your feature request related to a problem? Please describe.
Editing tedge config values which are arrays is difficult to managed for users as it involves checking the initial value, and adding the new value only if it is not already included in the list. Most of the time the "thing" adding the new value does not care about the existing values, and just wants to make sure the value it cares about is added to the list without breaking any other components.
There are currently the following tedge config settings which expect an array:
$ tedge --config-dir /opt/homebrew/etc/tedge config list --all | grep '\['
c8y.smartrest.templates=["collection1", "collection2"]
c8y.topics=["te/+/+/+/+", "te/+/+/+/+/twin/+", "te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]
az.topics=["te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]
aws.topics=["te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]
The problem can be demonstrated via the tedge config c8y.smartrest.templates
setting.
-
Package 1 includes an operation handler which requires subscribing to a SmartREST template called
collection1
tedge config set c8y.smartrest.templates "collection1"
After adding the
collection1
template, the configuration get be retrieved using:$ tedge config get c8y.smartrest.templates ["collection1"]
-
Package 2 includes another operation handlers which requires subscribing to a different SmartREST template called
collection2
The package can't just run the following command, as it would overwrite the existing value from the previous step, so it requires more complex logic using jq to splice in a new value (below adds the new values, and then removes any duplicated template names)
SMARTREST_TEMPLATE_NAME=collection2 NEW_VALUE=$(tedge config get c8y.smartrest.templates | jq -r ". += [\"$SMARTREST_TEMPLATE_NAME\"]| unique | @tsv" | tr '\t' ',') tedge config set c8y.smartrest.templates "$NEW_VALUE"
Note If the user would not have
jq
then the logic was be more complex (and naive):SMARTREST_TEMPLATE_NAME=collection2 EXISTING_VALUES=$(tedge --config-dir /opt/homebrew/etc/tedge config get c8y.smartrest.templates | tr -d '[]"' | sed 's/, /,/g') if [ -n "$EXISTING_VALUES" ]; then NEW_VALUE=$(echo "$EXISTING_VALUES,$SMARTREST_TEMPLATE_NAME" | sort | uniq) else NEW_VALUE="$SMARTREST_TEMPLATE_NAME" fi tedge config set c8y.smartrest.templates "$NEW_VALUE"
Describe the solution you'd like
Add new subcommands for tedge config
to "add" and "remove" values from an array.
Below shows an example of some of the commands (and the result of the setting afterwards)
tedge config set c8y.smartrest.templates collection1
# Afterwards: ["collection1"]
# Add new value to the existing array
tedge config add c8y.smartrest.templates collection2
# Afterwards: ["collection1","collection2"]
# Only add the value if it does not already exist
tedge config add c8y.smartrest.templates collection2
# Afterwards: ["collection1","collection2"]
# Add multiple new values to the existing array
tedge config add c8y.smartrest.templates collection3,collection4
# Afterwards: ["collection1","collection2","collection3","collection4"]
# Remove value from existing template array
tedge config remove c8y.smartrest.templates collection3
# Afterwards: ["collection1","collection2","collection4"]
tedge config add {prop} {value}
- Append the given value to the existing items in the array (and if the value does not already existing in the array)
- If the value is already in the array, then do nothing and exit with a status code 0 (as the command goal is already achieved)
- If used on a setting which is not an array, replace the value (e.g. on non-array properties, it should behave like
tedge config set
)
tedge config remove {prop} {value}
- Remove the given value to the existing items in the array (if the value occurs more than once, then all values should be removed)
- If the value is NOT in the array, then do nothing and exit with a status code 0 (as the command goal is already achieved)
- If used on a setting which is not an array, unset the property (e.g. on non-array properties, it should behave like
tedge config unset
)
Describe alternatives you've considered
Additional context
QA troughly check was done during the development/implementation of this feature. Suggested steps to be added in the test case are done properly the review was done in: https://github.com/thin-edge/thin-edge.io/pull/2943