Skip to content

Commit

Permalink
Add temporal workflow fix-history-json subcommand. (#504)
Browse files Browse the repository at this point in the history
## What was changed
* New command: `temporal workflow fix-history-json`

## Why?
This command reads an event history JSON object using the
`client.HistoryFromJSON` API, then serializes it back out using the
`protojson` API. `HistoryFromJSON` is backward compatible with both the
standard protobuf JSON format and with GoGoProto's format, which differ
in their handling of enum values.

## Checklist
1. Closes internal JIRA tickets SDK-1570 and OSS-1658, which do not seem
to have equivalent issues in the public GitHub.

2. How was this tested:
I downloaded a sample event history JSON from my dev server, then ran it
through a few conversions.

3. Any docs updates needed?
If there's a manual step in publishing changes to `commands.md`, then it
will need to be run.
  • Loading branch information
chronos-tachyon authored Mar 22, 2024
1 parent f9ac669 commit d764241
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
31 changes: 31 additions & 0 deletions temporalcli/commands.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ func NewTemporalWorkflowCommand(cctx *CommandContext, parent *TemporalCommand) *
s.Command.AddCommand(&NewTemporalWorkflowDeleteCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowDescribeCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowExecuteCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowFixHistoryJsonCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowListCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowQueryCommand(cctx, &s).Command)
s.Command.AddCommand(&NewTemporalWorkflowResetCommand(cctx, &s).Command)
Expand Down Expand Up @@ -1457,6 +1458,36 @@ func NewTemporalWorkflowExecuteCommand(cctx *CommandContext, parent *TemporalWor
return &s
}

type TemporalWorkflowFixHistoryJsonCommand struct {
Parent *TemporalWorkflowCommand
Command cobra.Command
Source string
Target string
}

func NewTemporalWorkflowFixHistoryJsonCommand(cctx *CommandContext, parent *TemporalWorkflowCommand) *TemporalWorkflowFixHistoryJsonCommand {
var s TemporalWorkflowFixHistoryJsonCommand
s.Parent = parent
s.Command.DisableFlagsInUseLine = true
s.Command.Use = "fix-history-json [flags]"
s.Command.Short = "Updates an event history JSON file to the current format."
if hasHighlighting {
s.Command.Long = "\x1b[1mtemporal workflow fix-history-json \\\n\t--source original.json \\\n\t--target reserialized.json\x1b[0m\n\nUse the options listed below to change the command's behavior."
} else {
s.Command.Long = "```\ntemporal workflow fix-history-json \\\n\t--source original.json \\\n\t--target reserialized.json\n```\n\nUse the options listed below to change the command's behavior."
}
s.Command.Args = cobra.NoArgs
s.Command.Flags().StringVarP(&s.Source, "source", "s", "", "Path to the input file.")
_ = cobra.MarkFlagRequired(s.Command.Flags(), "source")
s.Command.Flags().StringVarP(&s.Target, "target", "t", "", "Path to the output file, or standard output if not set.")
s.Command.Run = func(c *cobra.Command, args []string) {
if err := s.run(cctx, args); err != nil {
cctx.Options.Fail(err)
}
}
return &s
}

type TemporalWorkflowListCommand struct {
Parent *TemporalWorkflowCommand
Command cobra.Command
Expand Down
37 changes: 37 additions & 0 deletions temporalcli/commands.workflow_fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package temporalcli

import (
"bytes"
"os"

"go.temporal.io/sdk/client"
"google.golang.org/protobuf/encoding/protojson"
)

func (c *TemporalWorkflowFixHistoryJsonCommand) run(cctx *CommandContext, args []string) error {
raw, err := os.ReadFile(c.Source)
if err != nil {
return err
}

hjo := client.HistoryJSONOptions{}
history, err := client.HistoryFromJSON(bytes.NewReader(raw), hjo)
if err != nil {
return err
}

mo := protojson.MarshalOptions{Indent: " "}
raw, err = mo.Marshal(history)
if err != nil {
return err
}

switch c.Target {
case "", "-":
_, err = cctx.Options.Stdout.Write(raw)
return err

default:
return os.WriteFile(c.Target, raw, 0o666)
}
}
15 changes: 15 additions & 0 deletions temporalcli/commandsmd/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,21 @@ temporal workflow execute
Includes options set for [workflow start](#options-set-for-workflow-start).
Includes options set for [payload input](#options-set-for-payload-input).

### temporal workflow fix-history-json: Updates an event history JSON file to the current format.

```
temporal workflow fix-history-json \
--source original.json \
--target reserialized.json
```

Use the options listed below to change the command's behavior.

#### Options

* `--source`, `-s` (string) - Path to the input file. Required.
* `--target`, `-t` (string) - Path to the output file, or standard output if not set.

### temporal workflow list: List Workflow Executions based on a Query.

The `temporal workflow list` command provides a list of [Workflow Executions](/concepts/what-is-a-workflow-execution)
Expand Down

0 comments on commit d764241

Please sign in to comment.