thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
211 stars 55 forks source link

easily add/remove values to existing tedge config properties of an array type #2864

Open reubenmiller opened 1 month ago

reubenmiller commented 1 month ago

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.

  1. 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"]
  2. 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}

tedge config remove {prop} {value}

Describe alternatives you've considered

Additional context

gligorisaev commented 3 days ago

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