Skip to content

Commit

Permalink
fix(cdsctl): clean ouput for template bulk track no-interactive (#6391)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Dec 15, 2022
1 parent 6a166b7 commit b98dae7
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions cli/cdsctl/template_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,32 +530,44 @@ func templateBulkRun(v cli.Values) error {

if v.GetBool("track") {
var currentDisplay = new(cli.Display)
currentDisplay.Printf("Looking for bulk %d...\n", b.ID)
currentDisplay.Do(context.Background())
if v.GetBool("no-interactive") {
fmt.Printf("Looking for bulk %d...\n", b.ID)
} else {
currentDisplay.Printf("Looking for bulk %d...\n", b.ID)
currentDisplay.Do(context.Background())
}

lastOperations := make(map[string]sdk.WorkflowTemplateBulkOperation)
for {
var out string

res, err = client.TemplateGetBulk(wt.Group.Name, wt.Slug, res.ID)
if err != nil {
return err
}

var out string
operationStatusChanged := make(map[string]bool)
for _, o := range res.Operations {
var status string
switch o.Status {
case sdk.OperationStatusPending:
status = cli.Blue("pending")
case sdk.OperationStatusProcessing:
status = cli.Yellow("processing")
case sdk.OperationStatusDone:
status = cli.Green("done")
case sdk.OperationStatusError:
status = cli.Red("error")
opKey := o.Request.ProjectKey + "/" + o.Request.WorkflowName
if lastOperation, ok := lastOperations[opKey]; !ok || lastOperation.Status != o.Status {
lastOperations[opKey] = o
operationStatusChanged[opKey] = true
}
}

for opKey, changed := range operationStatusChanged {
if v.GetBool("no-interactive") {
if changed {
fmt.Printf("%s -> %s %s\n", opKey, OperationStatusToCLIString(lastOperations[opKey].Status), lastOperations[opKey].Error)
}
} else {
out += fmt.Sprintf("%s -> %s %s\n", opKey, OperationStatusToCLIString(lastOperations[opKey].Status), lastOperations[opKey].Error)
}
out += fmt.Sprintf("%s/%s -> %s %s\n", o.Request.ProjectKey, o.Request.WorkflowName, status, o.Error)
}

currentDisplay.Printf(out)
if !v.GetBool("no-interactive") {
currentDisplay.Printf(out)
}

time.Sleep(500 * time.Millisecond)
if res.IsDone() {
Expand All @@ -566,3 +578,18 @@ func templateBulkRun(v cli.Values) error {

return nil
}

func OperationStatusToCLIString(o sdk.OperationStatus) string {
var status string
switch o {
case sdk.OperationStatusPending:
status = cli.Blue("pending")
case sdk.OperationStatusProcessing:
status = cli.Yellow("processing")
case sdk.OperationStatusDone:
status = cli.Green("done")
case sdk.OperationStatusError:
status = cli.Red("error")
}
return status
}

0 comments on commit b98dae7

Please sign in to comment.