cli icon indicating copy to clipboard operation
cli copied to clipboard

Some commands emit non-JSON freeform text with --json

Open dandavison opened this issue 2 years ago • 6 comments

temporal workflow start --json should emit a valid JSON document on stdout, but it is additionally including the freeform text "Running execution:":

$ temporal workflow start --task-queue=MyTaskQueue --type=MyWorkflow --output json 2> /dev/null
Running execution:
[
  {
    "WorkflowId": "10a4ba0a-df39-4a1d-b9d2-5c81b751174f",
    "RunId": "391014ec-fbdd-4f1f-bb94-cad89bfa02df",
    "Type": "MyWorkflow",
    "Namespace": "default",
    "TaskQueue": "MyTaskQueue",
    "Args": "[]"
  }
]

This is due to the following line in StartWorkflow():

https://github.com/temporalio/cli/blob/c3514da2a3f3ea375ecd0781a92273fcd317abd4/workflow/workflow_commands.go#L158

Fixing this is a breaking change, but I think we need to fix it. Hopefully most people are using something like grep -v rather than sed 1d as a workaround.

From a quick look, the way that I would suggest fixing this is to make getOutputFormat public in tctl-kit, and then call it in StartWorkflow to determine whether we are outputting JSON or not.

This is inelegant, because it means we would be calling GetOutputFormat again shortly afterwards in PrintItems from tctl-kit. However, PrintItems is used in many places and I suspect that accepting the inelegance is the best option. I can do this, but seeing as what I'm suggesting isn't particularly beautiful, involves two repos, and I'm new to the codebase, I'd prefer to get approval for that change first.

dandavison avatar Aug 06 '23 02:08 dandavison

Concur, or at the very least support a --quiet option that only prints out the programmatic data.

I should be able to pass the results of this call to jq. And that goes for every call here. We should support JSON output for everything, and people should be able to rely on the output actually being JSON, not just part of it.

cretz avatar Aug 07 '23 13:08 cretz

We had a similar issue with temporal workflow show, I would expect any command with --output json to only output JSON.

bergundy avatar Aug 07 '23 18:08 bergundy

the way that I would suggest fixing this is to make getOutputFormat public in tctl-kit

We can write a helper that will check for the output format and replace fmt.Printxx instances. Maybe keep getOutputFormat private and expose the helper from tctl-kit

feedmeapples avatar Aug 07 '23 22:08 feedmeapples

We can write a helper that will check for the output format and replace fmt.Printxx instances. Maybe keep getOutputFormat private and expose the helper from tctl-kit

This sounds good.

dandavison avatar Aug 08 '23 17:08 dandavison

We have this problem with batch commands too; examples:

  • https://github.com/temporalio/cli/blob/4a9a176f1ff0002b5ab902d0d898203d83527072/batch/batch_commands.go#L175
  • https://github.com/temporalio/cli/blob/4a9a176f1ff0002b5ab902d0d898203d83527072/batch/batch_commands.go#L193
  • https://github.com/temporalio/cli/blob/4a9a176f1ff0002b5ab902d0d898203d83527072/batch/batch_commands.go#L220

josh-berry avatar Sep 18 '23 22:09 josh-berry

Another command with a similar issue, as reported in #307 (in this case, there is no output instead of an empty array or similar): temporal schedule list

josh-berry avatar Sep 29 '23 17:09 josh-berry

This should be done in 0.12.

josh-berry avatar May 07 '24 23:05 josh-berry