canonical / ubuntu-pro-client

Ubuntu Pro Client for offerings from Canonical
https://canonical-ubuntu-pro-client.readthedocs-hosted.com/en/latest/
GNU General Public License v3.0
52 stars 73 forks source link

Feature: offer boolean CLI command for knowing if machine is attached #3287

Open cjdcordeiro opened 3 weeks ago

cjdcordeiro commented 3 weeks ago

Please describe the scenario where the new feature would be useful

In situations where we programmatically use the pro client, it would be much easier if there was a dedicated CLI command or options that would simply return a boolean response to whether the machine is attached or not. Today we have to do this by parsing the output of pro status or pro api u.pro.status.is_attached.v1 which may introduce an additional dependency (like jq) and forces the users/programs to know exactly which key provides the value of

Describe the solution you'd like

Ideally a new command like pro is-attached or maybe an option pro status --is-attached.

Current behavior

Currently need to pro api u.pro.status.is_attached.v1 | jq .is_attached.

orndorffgrant commented 3 weeks ago

Are you restricted to using bash or a language without json parsing available?

As a workaround, you can do the following, because if pro is available then python3 must also be available.

pro api u.pro.status.is_attached.v1 | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['is_attached'])"

or alternatively

python3 -c "from uaclient.api.u.pro.status.is_attached.v1 import is_attached; print(is_attached().is_attached)"
rpocase commented 4 days ago

@orndorffgrant your options are a good start. many systems have jq installed by default as well which can give a non-zero exit code directly.

pro api u.pro.status.is_attached.v1 | jq --exit-status .data.attributes.is_attached
cjdcordeiro commented 4 days ago

No restrictions here, at least for now (I could speculate some chiselled/distroless cases where utilities might be limited).

This is a suggestion for improvement, as it feels like other programs using the Pro client should rely on it to just provide a yes/no answer on this "status" question (e.g. something like pebble health)