juju / charmstore-client

Client for charmstore.
Other
9 stars 22 forks source link

The `charm list-resources` should have a headerless option for automation. #123

Open mbruzek opened 7 years ago

mbruzek commented 7 years ago

I am trying to create a CI system and want to write a script that returns a list of the resources fo a charm. I want to use the charm list-resources command but it includes a header that is difficult to script away.

$ charm list-resources cs:~containers/kubernetes-e2e
[Service]
RESOURCE    REVISION
e2e_amd64   6
e2e_ppc64el 0
e2e_s390x   0

I have tried other charmstore-client commands but they all involve some pretty hard core bash that will be difficult to maintain or unforgiving to output changes.

$ charm list-resources cs:~containers/kubernetes-e2e | tail -n+3 | tr -s [:blank:] '-'
e2e_amd64-6
e2e_ppc64el-0
e2e_s390x-0

This option is less than ideal if the header ever changes.

$ charm show ~containers/kubernetes-e2e resources --format json | jq -r '.resources[] | [.Name,.Revision|tostring] | join("-")'
e2e_amd64-6
e2e_ppc64el-0
e2e_s390x-0

This option uses jq to parse the json output.

$ charm show cs:~containers/kubernetes-e2e resources | grep -E 'Name:|Revision:' | awk '{print $2}' | paste - - | tr [:blank:] '-'
e2e_amd64-6
e2e_ppc64el-0
e2e_s390x-0

This last option uses charm show but uses multiple pipes and greps to get what I want.

Could we add an option to charm list-resources that excludes the header? Or do you know of a command that will return the resources with the revision numbers that are attached to a charm?

mitechie commented 7 years ago

So I don't think we should remove the heading. It's meant to be consistent with our other tools and tabular format. The machine readable formats are meant for parsing and machine scripting needs such as this.

johnsca commented 7 years ago

@mitechie The [Service] header seems problematic for several reasons:

I do agree that machine parsing should use a machine readable format, but the landed --format=short seems like a reasonable compromise.

jamesbeedy commented 7 years ago

@mbruzek what's wrong with this implementation?

$ charm list-resources cs:~containers/kubernetes-e2e --format=json | jq '.[] | [.Name, .Revision|tostring] | join("-")' | tr -d '"'
e2e_amd64-11
e2e_ppc64el-0
e2e_s390x-0
johnsca commented 7 years ago

I don't think there's anything wrong with it, per se (well, other than that you should use jq -r so that you can drop the tr), but since --format=short was landed, that will be a nice shortcut in the future and it avoids an external dependency (jq).

jamesbeedy commented 7 years ago

@johnsca Ahh I see. Its in master, but hasn't made its way into a release yet.