ionos-cloud / ionosctl

The IONOS Cloud CLI (ionosctl) gives the ability to manage IONOS Cloud infrastructure directly from Command Line.
Apache License 2.0
30 stars 6 forks source link

bug: `ionosctl` errors with exit code 1 if no images where found #406

Closed hegerdes closed 11 months ago

hegerdes commented 11 months ago

Description

If you search/filter for a name that does not exists - the tool errors:

ionosctl image list --filters name=debian-12-amd64-latest-none-exist,location=txl,location=fra
echo $? # Outputs 1

This is and leads to unexpected behavior. A command should only error if there is anything wrong with the command/network/filesystem - not if the command return its expected outcome. For example in the CI. If there is no image the tool should output nothing and exit with 0. Or at leas there should be a flag for that.

Please apply to standard behavior

Expected behavior

Command returns nothing (or empty array if json output) and exits with code 0

Environment

Go Version:

?

Ionosctl version:

ionosctl version v6.7.1

OS:

Windows 11 Pro 23H2

Shell:

Git Bash

Configuration Files

None

How to Reproduce

If you search/filer for a name that does not exists - the tool errors:

ionosctl image list --filters name=debian-12-amd64-latest-none-exist,location=txl,location=fra
echo $? # Outputs 1

This is and leads to unexpected behavior. A command should only error if there is anything wrong with the command/network/or - not if the command return its expected outcome. For example in the CI. If there is no image the tool should output nothing and exit with 0. Or at leas thee should be a flag for that.

Please apply to standard behavior

Error and Debug Output

ionosctl image list --filters name=debian-12-amd64-latest,location=txl,location=fra
Error: error getting images based on given criteria

Additional Notes

It is Industry standard to use non 0 exit codes if there is anything wrong - hears it behaves as expected. Most other tools also just exit with 0 when used with filter. See docker ps --filter name=none-exist or kubectl

Also if you specify --output json it should also output an empty array/object. In case of an actual error - the output should also be json not text

References

avirtopeanu-ionos commented 11 months ago

Hi, thanks for your contribution!

  1. Non-zero exit codes for empty API items: I fully agree, this is a bug, code 0 should be returned if no images are found. I checked and same thing happens for a handful more resources (requests, etc) for which the same change will be applied. A valid use case should be checking for a zero status code and empty output to guarantee that you have no items of a certain resource.

  2. --output json should output an empty array: Here this would be a big impact / breaking change on scripts - I fully agree though, that this would make the most sense. I'll make a note to include this in the next major for sure, but can't guarantee it will happen too soon...

Thanks for opening this issue!

hegerdes commented 11 months ago

Workaround until this is fixed:

# Good old bash to the rescue...
if ionosctl image list --filters name=${IMG_NAME}-latest,location=txl,location=fra ; then
    export CURRENT_IMG_RUN_IDS=$(ionosctl image list --filters name=${IMG_NAME}-latest,location=txl,location=fra --output json | jq -r ".items[].id")
    echo $CURRENT_IMG_RUN_IDS
    # Do stuff
fi

This does need a second request but that better then a failing CI