Skip to content

Commit

Permalink
cli: diff sections mantain the order given by the server
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Aug 22, 2022
1 parent ac2be4b commit 96b49d4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 55 deletions.
27 changes: 22 additions & 5 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"reanahub/reana-client-go/pkg/datautils"
"reanahub/reana-client-go/pkg/displayer"

"github.com/iancoleman/orderedmap"

"github.com/jedib0t/go-pretty/v6/text"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -100,20 +102,35 @@ func (o *diffOptions) run(cmd *cobra.Command) error {

func displayDiffPayload(cmd *cobra.Command, p *operations.GetWorkflowDiffOKBody) error {
if p.ReanaSpecification != "" {
var specificationDiff map[string][]string
specificationDiff := orderedmap.New()
err := json.Unmarshal([]byte(p.ReanaSpecification), &specificationDiff)
if err != nil {
return err
}

// Rename section workflow to specification
val, hasWorkflow := specificationDiff["workflow"]
val, hasWorkflow := specificationDiff.Get("workflow")
if hasWorkflow {
specificationDiff["specification"] = val
delete(specificationDiff, "workflow")
specificationDiff.Set("specification", val)
specificationDiff.Delete("workflow")
}
equalSpecification := true
for section, lines := range specificationDiff {
for _, section := range specificationDiff.Keys() {
// Convert diff to a slice of strings
sectionDiffs, _ := specificationDiff.Get(section)
linesInterface, ok := sectionDiffs.([]any)
if !ok {
return fmt.Errorf("expected diff to be an array, got %v", sectionDiffs)
}
lines := make([]string, 0, len(linesInterface))
for _, line := range linesInterface {
lineString, ok := line.(string)
if !ok {
return fmt.Errorf("expected diff line to be a string, got %v", line)
}
lines = append(lines, lineString)
}

if len(lines) != 0 {
equalSpecification = false
displayer.PrintColorable(
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/go-openapi/loads v0.21.1 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand Down
Loading

0 comments on commit 96b49d4

Please sign in to comment.