Skip to content

Commit

Permalink
Add print json flag to list domains command (cadence-workflow#4134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaddoll authored and yux0 committed May 4, 2021
1 parent a5c7e7b commit ec1ee2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ func newAdminDomainCommands() []cli.Command {
Name: FlagPrintFullyDetailWithAlias,
Usage: "Print full domain detail",
},
cli.BoolFlag{
Name: FlagPrintJSONWithAlias,
Usage: "Print in raw json format",
},
},
Action: func(c *cli.Context) {
newDomainCLI(c, false).ListDomains(c)
Expand Down
34 changes: 25 additions & 9 deletions tools/cli/domainCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cli

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -447,25 +448,40 @@ func (d *domainCLIImpl) ListDomains(c *cli.Context) {
printAll := c.Bool(FlagAll)
printDeprecated := c.Bool(FlagDeprecated)
printFull := c.Bool(FlagPrintFullyDetail)
printJSON := c.Bool(FlagPrintJSON)

if printAll && printDeprecated {
ErrorAndExit(fmt.Sprintf("Cannot specify %s and %s flags at the same time.", FlagAll, FlagDeprecated), nil)
}

domains := d.getAllDomains(c)
var filteredDomains []*types.DescribeDomainResponse
if printAll {
filteredDomains = domains
} else {
filteredDomains = make([]*types.DescribeDomainResponse, 0, len(domains))
for _, domain := range domains {
if printDeprecated && *domain.DomainInfo.Status == types.DomainStatusDeprecated {
filteredDomains = append(filteredDomains, domain)
} else if !printDeprecated && *domain.DomainInfo.Status == types.DomainStatusRegistered {
filteredDomains = append(filteredDomains, domain)
}
}
}

if printJSON {
output, err := json.Marshal(filteredDomains)
if err != nil {
ErrorAndExit("Failed to encode domain results into JSON.", err)
}
fmt.Println(string(output))
return
}

table := createTableForListDomains(printAll, printFull)

currentPageSize := 0
for i, domain := range domains {
if printDeprecated {
if *domain.DomainInfo.Status != types.DomainStatusDeprecated {
continue
}
} else if !printAll && *domain.DomainInfo.Status != types.DomainStatusRegistered {
continue
}

for i, domain := range filteredDomains {
appendDomainToTable(table, domain, printAll, printFull)
currentPageSize++

Expand Down

0 comments on commit ec1ee2c

Please sign in to comment.