seqeralabs / tower-cli

Nextflow Tower CLI tool
Apache License 2.0
42 stars 9 forks source link

Add examples of using jq for parsing JSON output #251

Open drpatelh opened 2 years ago

drpatelh commented 2 years ago

The CLI is able to output standard JSON for CLI commands. This makes it relatively easy to store infrastructure as code as well as to retrieve settings/status for Tower entities. jq is a great tool for this purpose. It allows you to retrieve specific entries within the JSON schema dumped by the CLI on the CLI.

For example if you use -o json for any command in the Tower CLI it will dump the output in JSON format. The command below is fetching various information about a run in the community/showcase workspace on Tower:

$ tw -o json runs view --id 5tMT2PJXCp1hz2 --workspace=community/showcase
{
  "general" : {
    "id" : "5tMT2PJXCp1hz2",
    "operationId" : "592f3c8f-c3d8-4055-9332-b3918d7XXXXX",
    "runName" : "silly_torvalds",
    "startingDate" : "2022-08-30T05:18:42Z",
    "commitId" : "959967364c7c0105b5b271acf0441fa9290e0d4c",
    "sessionId" : "f51221d8-dc76-4162-b47c-588ca03XXXXX",
    "username" : "brett-calcott",
    "workdir" : "s3://<MY_BUCKET>/scratch/5tMT2PJXCp1hz2",
    "container" : "",
    "executors" : "awsbatch",
    "computeEnv" : "AWS_Batch_Ireland",
    "nextflowVersion" : "22.06.1.edge",
    "status" : "SUCCEEDED"
  }
}

We can simply pipe this command to jq to get specific entries within the JSON output.

$ tw -o json runs view --id 5tMT2PJXCp1hz2 --workspace=community/showcase | jq -r '.[].status'
SUCCEEDED

Please consult the jq docs for more extensive patterns and usage.

jordeu commented 1 year ago

Now we have a small tip at usage: https://github.com/seqeralabs/tower-cli/blob/6ddd97f63f8f64090984ec45aa12bdb8dfde49a1/USAGE.md?plain=1#L11

Do you think that this is enough?