Skip to content

Commit

Permalink
Add JSON output support for remaining commands of zms-cli (#1397)
Browse files Browse the repository at this point in the history
* Add JSON output support for remaining commands of zms-cli

Signed-off-by: Abhishek Patra <[email protected]>

* Move output format-deciding function to cli.go

Signed-off-by: Abhishek Patra <[email protected]>

Co-authored-by: Abhishek Patra <[email protected]>
  • Loading branch information
patrasap0908 and Abhishek Patra authored Mar 24, 2021
1 parent e448b5f commit 87015b7
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 191 deletions.
7 changes: 3 additions & 4 deletions libs/go/zmscli/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (cli Zms) ShowAccess(dn string, action string, resource string, altIdent *s
if !access.Granted {
s = "access: denied"
}
return &s, nil
return cli.switchOverFormats(s)
}

// ShowAccessExt returns access indicator as string:
Expand All @@ -75,7 +75,7 @@ func (cli Zms) ShowAccessExt(dn string, action string, resource string, altIdent
if !access.Granted {
s = "access: denied"
}
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) ShowResourceAccess(principal string, action string) (*string, error) {
Expand All @@ -93,6 +93,5 @@ func (cli Zms) ShowResourceAccess(principal string, action string) (*string, err
cli.dumpAssertion(&buf, assertion, "", indent2)
}
}
s := buf.String()
return &s, nil
return cli.switchOverFormats(rsrcAccessList, buf.String())
}
41 changes: 36 additions & 5 deletions libs/go/zmscli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
Expand Down Expand Up @@ -50,6 +51,36 @@ type StandardJSONMessage struct {
Message string `json:"message,required"`
}

func (cli Zms) buildJSONOutput(res interface{}) (*string, error) {
jsonOutput, err := json.MarshalIndent(res, "", indentLevel1)
if err != nil {
return nil, fmt.Errorf("failed to produce JSON output: %v", err)
}
output := string(jsonOutput)
return &output, nil
}

func (cli Zms) switchOverFormats(res interface{}, msg ...string) (*string, error) {
var op string
if msg == nil {
op = res.(string)
}
switch cli.OutputFormat {
case JSONOutputFormat:
if msg == nil {
return cli.buildJSONOutput(&StandardJSONMessage{Message: op})
}
return cli.buildJSONOutput(res)
case DefaultOutputFormat:
if msg == nil {
return &op, nil
}
return &msg[0], nil
default:
return nil, fmt.Errorf(ErrInvalidOutputFormat, cli.OutputFormat)
}
}

func (cli *Zms) SetClient(tr *http.Transport, authHeader, ntoken *string) {
cli.Zms = zms.NewClient(cli.ZmsUrl, tr)
cli.Zms.AddCredentials(*authHeader, *ntoken)
Expand Down Expand Up @@ -160,7 +191,7 @@ func (cli *Zms) EvalCommand(params []string) (*string, error) {
cli.Domain = ""
s = "[not using any domain]"
}
return cli.switchOverFormats(&StandardJSONMessage{Message: s}, s)
return cli.switchOverFormats(s)
case "show-domain":
if argc == 1 {
//override the default domain, this command can show any of them
Expand All @@ -182,11 +213,11 @@ func (cli *Zms) EvalCommand(params []string) (*string, error) {
case "export-domain":
if argc == 1 || argc == 2 {
dn = args[0]
yamlfile := "-"
filename := "-"
if argc == 2 {
yamlfile = args[1]
filename = args[1]
}
return cli.ExportDomain(dn, yamlfile)
return cli.ExportDomain(dn, filename)
}
return cli.helpCommand(params)
case "import-domain":
Expand Down Expand Up @@ -1236,7 +1267,7 @@ func (cli Zms) HelpSpecificCommand(interactive bool, cmd string) string {
buf.WriteString(" import-domain coretech coretech.yaml " + cli.UserDomain + ".john\n")
case "export-domain":
buf.WriteString(" syntax:\n")
buf.WriteString(" [-o json] export-domain domain [file.yaml] - no file means stdout\n")
buf.WriteString(" [-o json] export-domain domain [file.yaml or file.json] - no file means stdout\n")
buf.WriteString(" parameters:\n")
buf.WriteString(" domain : name of the domain to be exported\n")
buf.WriteString(" file.yaml : filename where the domain data is stored\n")
Expand Down
64 changes: 15 additions & 49 deletions libs/go/zmscli/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package zmscli

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand All @@ -17,36 +16,6 @@ import (
"gopkg.in/yaml.v2"
)

func (cli Zms) buildJSONOutput(res interface{}) (*string, error) {
jsonOutput, err := json.MarshalIndent(res, "", indentLevel1)
if err != nil {
return nil, fmt.Errorf("failed to produce JSON output: %v", err)
}
output := string(jsonOutput)
return &output, nil
}

func (cli Zms) switchOverFormats(res interface{}, msg ...string) (*string, error) {
var op string
if msg == nil {
op = res.(string)
}
switch cli.OutputFormat {
case JSONOutputFormat:
if msg == nil {
return cli.buildJSONOutput(&StandardJSONMessage{Message: op})
}
return cli.buildJSONOutput(res)
case DefaultOutputFormat:
if msg == nil {
return &op, nil
}
return &msg[0], nil
default:
return nil, fmt.Errorf(ErrInvalidOutputFormat, cli.OutputFormat)
}
}

// DeleteDomain deletes the given ZMS domain.
func (cli Zms) DeleteDomain(dn string) (*string, error) {
_, err := cli.Zms.GetDomain(zms.DomainName(dn))
Expand Down Expand Up @@ -214,7 +183,7 @@ func (cli Zms) UpdateDomain(dn string, filename string) (*string, error) {
}
}
s := "[updated domain '" + dn + "' successfully]"
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) ExportDomain(dn string, filename string) (*string, error) {
Expand All @@ -224,6 +193,10 @@ func (cli Zms) ExportDomain(dn string, filename string) (*string, error) {
cli.Verbose = verbose
if err == nil && data != nil {
s := *data
msg, err := cli.switchOverFormats(*data)
if err == nil {
s = *msg
}
if filename == "-" {
fmt.Println(s)
} else {
Expand Down Expand Up @@ -251,7 +224,7 @@ func (cli Zms) SystemBackup(dir string) (*string, error) {
}
cli.Verbose = verbose
s := "[exported " + strconv.Itoa(len(res.Names)) + " domains to " + dir + " directory]"
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) AddDomain(dn string, productID *int32, addSelf bool, admins []string) (*string, error) {
Expand Down Expand Up @@ -335,8 +308,7 @@ func (cli Zms) LookupDomainByBusinessService(businessService string) (*string, e
for _, name := range res.Names {
buf.WriteString(indentLevel1Dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
return cli.switchOverFormats(res, buf.String())
}
return nil, err
}
Expand All @@ -362,8 +334,7 @@ func (cli Zms) LookupDomainByTag(tagKey string, tagValue string) (*string, error
for _, name := range res.Names {
buf.WriteString(indentLevel1Dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
return cli.switchOverFormats(res, buf.String())
}
return nil, err
}
Expand Down Expand Up @@ -394,8 +365,7 @@ func (cli Zms) GetSignedDomains(dn string, matchingTag string) (*string, error)
cli.dumpSignedDomain(&buf, domain, false)
}
}
s := buf.String()
return cli.switchOverFormats(res, s)
return cli.switchOverFormats(res, buf.String())
}

func (cli Zms) ShowOverdueReview(dn string) (*string, error) {
Expand All @@ -412,9 +382,7 @@ func (cli Zms) ShowOverdueReview(dn string) (*string, error) {
}

cli.dumpDomainRoleMembers(&buf, domainRoleMembers, true)
s := buf.String()

return cli.switchOverFormats(domainRoleMembers, s)
return cli.switchOverFormats(domainRoleMembers, buf.String())
}

func (cli Zms) ShowDomain(dn string) (*string, error) {
Expand Down Expand Up @@ -709,7 +677,7 @@ func (cli Zms) AddDomainTags(dn string, tagKey string, tagValues []string) (*str
time.Sleep(500 * time.Millisecond)
output, err = cli.showDomainTags(dn)
}
return &output, err
return cli.switchOverFormats(output)

}

Expand Down Expand Up @@ -754,7 +722,7 @@ func (cli Zms) DeleteDomainTags(dn string, tagKey string, tagValue string) (*str
time.Sleep(500 * time.Millisecond)
output, err = cli.showDomainTags(dn)
}
return &output, err
return cli.switchOverFormats(output)
}

func (cli Zms) SetDomainAccount(dn string, account string) (*string, error) {
Expand Down Expand Up @@ -828,7 +796,7 @@ func (cli Zms) SetDomainBusinessService(dn string, businessService string) (*str
return nil, err
}
s := "[domain " + dn + " business-service successfully updated]\n"
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) SetDomainCertDnsDomain(dn string, dnsDomain string) (*string, error) {
Expand All @@ -851,7 +819,7 @@ func (cli Zms) SetDefaultAdmins(dn string, admins []string) (*string, error) {
return nil, err
}
s := "[domain " + dn + " administrators successfully set]\n"
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) ListPendingDomainRoleMembers(principal string) (*string, error) {
Expand All @@ -864,7 +832,5 @@ func (cli Zms) ListPendingDomainRoleMembers(principal string) (*string, error) {
for _, domainRoleMembers := range domainMembership.DomainRoleMembersList {
cli.dumpDomainRoleMembers(&buf, domainRoleMembers, true)
}
s := buf.String()

return &s, nil
return cli.switchOverFormats(domainMembership, buf.String())
}
12 changes: 5 additions & 7 deletions libs/go/zmscli/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

"github.com/ardielle/ardielle-go/rdl"
"github.com/AthenZ/athenz/clients/go/zms"
"github.com/ardielle/ardielle-go/rdl"
)

func (cli Zms) ShowEntity(dn string, en string) (*string, error) {
Expand All @@ -20,8 +20,7 @@ func (cli Zms) ShowEntity(dn string, en string) (*string, error) {
var buf bytes.Buffer
buf.WriteString("entity:\n")
cli.dumpEntity(&buf, *entity, indentLevel1Dash, indentLevel1DashLvl)
s := buf.String()
return &s, nil
return cli.switchOverFormats(entity, buf.String())
}

func (cli Zms) AddEntity(dn string, en string, values []string) (*string, error) {
Expand All @@ -48,7 +47,7 @@ func (cli Zms) AddEntity(dn string, en string, values []string) (*string, error)
time.Sleep(500 * time.Millisecond)
output, err = cli.ShowEntity(dn, en)
}
return output, err
return cli.switchOverFormats(*output)
}

func (cli Zms) DeleteEntity(dn string, en string) (*string, error) {
Expand All @@ -57,7 +56,7 @@ func (cli Zms) DeleteEntity(dn string, en string) (*string, error) {
return nil, err
}
s := "[Deleted entity: " + dn + "." + en + "]"
return &s, nil
return cli.switchOverFormats(s)
}

func (cli Zms) entityNames(dn string) ([]string, error) {
Expand All @@ -80,6 +79,5 @@ func (cli Zms) ListEntities(dn string) (*string, error) {
}
buf.WriteString("entities:\n")
cli.dumpObjectList(&buf, entities, dn, "entity")
s := buf.String()
return &s, nil
return cli.switchOverFormats(entities, buf.String())
}
Loading

0 comments on commit 87015b7

Please sign in to comment.