juju / charmstore-client

Client for charmstore.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mbruzek opened this issue · comments

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?

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.

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

  • It would need to be [Application] rather than [Service]
  • It's not actually leading a section of applications (or services), but rather charm info
  • The command would only ever show one thing, so having a section header seems superfluous
  • Other commands, such as juju status no longer have [Section] style headers

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

@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

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).

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