-j flag now suppresses formatted output

This commit is contained in:
Andrew Martinez
2020-02-26 13:00:31 -05:00
parent a4076608f0
commit a9dff5dd3d
6 changed files with 90 additions and 71 deletions
+2 -2
View File
@@ -32,7 +32,7 @@ func mapNameToID(entityType string, val string) (string, error) {
// Allow UUID formatted names to be recognized with a name: prefix
name := strings.TrimPrefix(val, "name:")
list, err := filterEntitiesOfType(entityType, fmt.Sprintf("name=\"%s\"", name), false)
list, err := filterEntitiesOfType(entityType, fmt.Sprintf("name=\"%s\"", name), false, nil)
if err != nil {
return "", err
}
@@ -62,7 +62,7 @@ func mapNamesToIDs(entityType string, list ...string) ([]string, error) {
} else {
// Allow UUID formatted names to be recognized with a name: prefix
name := strings.TrimPrefix(val, "name:")
list, err := filterEntitiesOfType(entityType, fmt.Sprintf("name=\"%s\"", name), false)
list, err := filterEntitiesOfType(entityType, fmt.Sprintf("name=\"%s\"", name), false, nil)
if err != nil {
return nil, err
}
@@ -91,7 +91,7 @@ func runCreateEdgeRouter(o *createEdgeRouterOptions) error {
}
func getEdgeRouterJwt(o *createEdgeRouterOptions, id string) error {
list, err := listEntitiesOfType("edge-routers", nil, o.OutputJSONResponse)
list, err := listEntitiesOfType("edge-routers", nil, o.OutputJSONResponse, o.Out)
if err != nil {
return err
}
@@ -118,7 +118,7 @@ func runCreateIdentity(idType string, o *createIdentityOptions) error {
}
func getIdentityJwt(o *createIdentityOptions, id string) error {
list, err := listEntitiesOfType("identities", nil, o.OutputJSONResponse)
list, err := listEntitiesOfType("identities", nil, o.OutputJSONResponse, o.Out)
if err != nil {
return err
}
+61 -9
View File
@@ -198,17 +198,17 @@ func listEntitiesWithOptions(entityType string, options *commonOptions) ([]*gabs
if len(options.Args) > 0 {
params.Add("filter", options.Args[0])
}
return listEntitiesOfType(entityType, nil, options.OutputJSONResponse)
return listEntitiesOfType(entityType, params, options.OutputJSONResponse, options.Out)
}
func filterEntitiesOfType(entityType string, filter string, outputJson bool) ([]*gabs.Container, error) {
func filterEntitiesOfType(entityType string, filter string, logJSON bool, out io.Writer) ([]*gabs.Container, error) {
params := url.Values{}
params.Add("filter", filter)
return listEntitiesOfType(entityType, params, outputJson)
return listEntitiesOfType(entityType, params, logJSON, out)
}
// listEntitiesOfType queries the Ziti Controller for entities of the given type
func listEntitiesOfType(entityType string, params url.Values, outputJSON bool) ([]*gabs.Container, error) {
func listEntitiesOfType(entityType string, params url.Values, logJSON bool, out io.Writer) ([]*gabs.Container, error) {
session := &session{}
err := session.Load()
@@ -220,7 +220,7 @@ func listEntitiesOfType(entityType string, params url.Values, outputJSON bool) (
return nil, fmt.Errorf("host not specififed in cli config file. Exiting")
}
jsonParsed, err := util.EdgeControllerList(session, entityType, params, outputJSON)
jsonParsed, err := util.EdgeControllerList(session, entityType, params, logJSON, out)
if err != nil {
return nil, err
@@ -230,7 +230,7 @@ func listEntitiesOfType(entityType string, params url.Values, outputJSON bool) (
}
// listEntitiesOfType queries the Ziti Controller for entities of the given type
func filterSubEntitiesOfType(entityType, subType, entityId, filter string, outputJSON bool) ([]*gabs.Container, error) {
func filterSubEntitiesOfType(entityType, subType, entityId, filter string, o *commonOptions) ([]*gabs.Container, error) {
session := &session{}
err := session.Load()
@@ -242,7 +242,7 @@ func filterSubEntitiesOfType(entityType, subType, entityId, filter string, outpu
return nil, fmt.Errorf("host not specififed in cli config file. Exiting")
}
jsonParsed, err := util.EdgeControllerListSubEntities(session, entityType, subType, entityId, filter, outputJSON)
jsonParsed, err := util.EdgeControllerListSubEntities(session, entityType, subType, entityId, filter, o.OutputJSONResponse, o.Out)
if err != nil {
return nil, err
@@ -256,10 +256,16 @@ func runListEdgeRouters(o *commonOptions) error {
if err != nil {
return err
}
return outputEdgeRouters(o, children)
}
func outputEdgeRouters(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -280,6 +286,10 @@ func runListEdgeRouterPolicies(o *commonOptions) error {
}
func outputEdgeRouterPolicies(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -304,7 +314,7 @@ func runListServices(asIdentity string, configTypes string, options *commonOptio
if configTypes != "" {
params.Add("configTypes", configTypes)
}
children, err := listEntitiesOfType("services", params, options.OutputJSONResponse)
children, err := listEntitiesOfType("services", params, options.OutputJSONResponse, options.Out)
if err != nil {
return err
}
@@ -312,6 +322,10 @@ func runListServices(asIdentity string, configTypes string, options *commonOptio
}
func outputServices(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -334,6 +348,10 @@ func runListServiceEdgeRouterPolices(o *commonOptions) error {
}
func outputServiceEdgeRouterPolicies(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -356,6 +374,10 @@ func runListServicePolices(o *commonOptions) error {
}
func outputServicePolicies(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -381,6 +403,10 @@ func runListIdentities(o *commonOptions) error {
// outputIdentities implements the command to list identities
func outputIdentities(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -400,6 +426,10 @@ func runListCAs(o *commonOptions) error {
return err
}
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -418,6 +448,10 @@ func runListConfigTypes(o *commonOptions) error {
return err
}
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -438,6 +472,11 @@ func runListConfigs(o *commonOptions) error {
}
func outputConfigs(o *commonOptions, children []*gabs.Container) error {
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
name, _ := entity.Path("name").Data().(string)
@@ -461,6 +500,10 @@ func runListApiSessions(o *commonOptions) error {
return err
}
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
sessionToken, _ := entity.Path("token").Data().(string)
@@ -480,6 +523,10 @@ func runListSessions(o *commonOptions) error {
return err
}
if o.OutputJSONResponse {
return nil
}
for _, entity := range children {
id, _ := entity.Path("id").Data().(string)
sessionId, _ := entity.Path("apiSession.id").Data().(string)
@@ -500,9 +547,14 @@ func runListChilden(parentType, childType string, o *commonOptions, outputF outp
return err
}
children, err := filterSubEntitiesOfType(parentType, childType, serviceId, "", o.OutputJSONResponse)
children, err := filterSubEntitiesOfType(parentType, childType, serviceId, "", o)
if err != nil {
return err
}
if o.OutputJSONResponse {
return nil
}
return outputF(o, children)
}
+3 -1
View File
@@ -111,7 +111,9 @@ func (o *loginOptions) Run() error {
return fmt.Errorf("session token returned from login request to %v is not in the expected format. Received: %v", host, jsonParsed.String())
}
fmt.Printf("Token: %v\n", token)
if !o.OutputJSONResponse {
fmt.Printf("Token: %v\n", token)
}
absCertPath, err := filepath.Abs(o.Cert)
if err == nil {
+22 -57
View File
@@ -412,16 +412,7 @@ func EdgeControllerLogin(url string, cert string, authentication string, out io.
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", resp.Body()); err != nil {
panic(err)
}
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())
@@ -432,6 +423,19 @@ func EdgeControllerLogin(url string, cert string, authentication string, out io.
return jsonParsed, nil
}
func outputJson(out io.Writer, data []byte) {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, data, "", " "); err == nil {
if _, err := fmt.Fprint(out, prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprint(out, data); err != nil {
panic(err)
}
}
}
type Session interface {
GetBaseUrl() string
GetCert() string
@@ -439,16 +443,16 @@ type Session interface {
}
// EdgeControllerListSubEntities will list entities of the given type in the given Edge Controller
func EdgeControllerListSubEntities(session Session, entityType, subType, entityId string, filter string, logJSON bool) (*gabs.Container, error) {
func EdgeControllerListSubEntities(session Session, entityType, subType, entityId string, filter string, logJSON bool, out io.Writer) (*gabs.Container, error) {
params := url.Values{}
if filter != "" {
params.Add("filter", filter)
}
return EdgeControllerList(session, entityType+"/"+entityId+"/"+subType, params, logJSON)
return EdgeControllerList(session, entityType+"/"+entityId+"/"+subType, params, logJSON, out)
}
// EdgeControllerList will list entities of the given type in the given Edge Controller
func EdgeControllerList(session Session, path string, params url.Values, logJSON bool) (*gabs.Container, error) {
func EdgeControllerList(session Session, path string, params url.Values, logJSON bool, out io.Writer) (*gabs.Container, error) {
client := newClient()
if session.GetCert() != "" {
@@ -477,10 +481,7 @@ func EdgeControllerList(session Session, path string, params url.Values, logJSON
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
fmt.Printf("Result:\n%s\n", prettyJSON.String())
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())
@@ -517,16 +518,7 @@ func EdgeControllerCreate(url string, cert string, token string, entityType stri
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", resp.Body()); err != nil {
panic(err)
}
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())
@@ -564,16 +556,7 @@ func EdgeControllerDelete(baseUrl string, cert string, token string, entityType
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", resp.Body()); err != nil {
panic(err)
}
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())
@@ -617,16 +600,7 @@ func EdgeControllerUpdate(url string, cert string, token string, entityType stri
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", resp.Body()); err != nil {
panic(err)
}
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())
@@ -662,16 +636,7 @@ func EdgeControllerRequest(url string, cert string, token string, entityType str
}
if logJSON {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, resp.Body(), "", " "); err == nil {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", prettyJSON.String()); err != nil {
panic(err)
}
} else {
if _, err := fmt.Fprintf(out, "Result:\n%s\n", resp.Body()); err != nil {
panic(err)
}
}
outputJson(out, resp.Body())
}
jsonParsed, err := gabs.ParseJSON(resp.Body())