cmd/member: sort the result before printing
The API call returns a member list in a different order between runs. We should sort it before printing the result in order to ease diff between two runs.
NB: Can be extrapolated to other list commands.
[doc]
Hi, Which file need edit?
Hi @Amirhossein2000, thanks for your interest !
To provide more information, the CLI returns the API result using the printer package (printer). The sort must be done for the printer.Table.
To abstract the things, the printer expects a pointer to a struct or an array of pointers, this is how swagger returns the things. For the table, we first extract the headers (exported struct fields name) and from this headers we extract the associated values.
https://github.com/cycloidio/cycloid-cli/blob/f2b0b56e00101d9a5e70981a666b632ddba0476f/printer/table/printer.go#L164
An output example would be:
headers -> ["ID", "NAME"]
values -> [[1, 3, 2], ["foo", "bar", "foobar"]
and so the result will be printed like
| ID | MEMBER |
|----|--------|
| 1 | foo |
| 3 | bar |
| 2 | foobar |
The issue is that the values are not in the same order between each run, so it's not easy to run deterministic tasks (like diff / sorting) between two outputs.
The easiest solution I have currently in mind, would be to pass (CLI flag with default value for each services ?) to the printer.Options the column on which one we want to sort the output and it would make sense for the next iterations on the printer.Table since we would like to be able to select the columns to display.
https://github.com/cycloidio/cycloid-cli/blob/f2b0b56e00101d9a5e70981a666b632ddba0476f/cmd/cycloid/members/list.go#L63-L66
If you still have an interest in the thing, you can easily test your work using the printer.Table test file. Thanks again and let us know it goes !