Skip to content

Commit

Permalink
Added the ability to have collectors list output marshalled or marsha…
Browse files Browse the repository at this point in the history
…lled indented (#60)

* Added the ability to output JSON unformatted for sumocli collectors list command (this will be rolled out to other commands in a later release)
  • Loading branch information
wizedkyle authored May 4, 2021
1 parent 5d8f4c9 commit 0d04398
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/cmd/collectors/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,30 @@ import (

func NewCmdCollectorList() *cobra.Command {
var (
filter string
limit int
offset string
offline bool
filter string
limit int
offset string
offline bool
jsonFormat bool
)

cmd := &cobra.Command{
Use: "list",
Short: "Lists Sumo Logic collectors",
Run: func(cmd *cobra.Command, args []string) {
listCollectors(filter, limit, offset, offline)
listCollectors(filter, limit, offset, offline, jsonFormat)
},
}

cmd.Flags().StringVar(&filter, "filter", "", "Filters the collectors returned using either installed, hosted, dead or alive")
cmd.Flags().IntVar(&limit, "limit", 1000, "Maximum number of collectors returned")
cmd.Flags().StringVar(&offset, "offset", "", "Offset into the list of collectors")
cmd.Flags().BoolVar(&offline, "offline", false, "Lists offline collectors")
cmd.Flags().BoolVar(&jsonFormat, "jsonFormat", false, "Set to true if you want the output to be formatted JSON")
return cmd
}

func listCollectors(filter string, limit int, offset string, offline bool) {
func listCollectors(filter string, limit int, offset string, offline bool, jsonFormat bool) {
log := logging.GetConsoleLogger()
var collectorInfo api.Collectors
var requestUrl string
Expand Down Expand Up @@ -76,11 +78,21 @@ func listCollectors(filter string, limit int, offset string, offline bool) {
log.Error().Err(jsonErr).Msg("error unmarshalling response body")
}

collectorInfoJson, err := json.MarshalIndent(collectorInfo.Data, "", " ")

if response.StatusCode != 200 {
factory.HttpError(response.StatusCode, responseBody, log)
} else {
fmt.Print(string(collectorInfoJson))
if jsonFormat == true {
collectorInfoJson, err := json.MarshalIndent(collectorInfo.Data, "", " ")
if err != nil {
log.Error().Err(err).Msg("failed to marshal response")
}
fmt.Print(string(collectorInfoJson))
} else {
collectorInfoJson, err := json.Marshal(collectorInfo.Data)
if err != nil {
log.Error().Err(err).Msg("failed to marshal response")
}
fmt.Println(string(collectorInfoJson))
}
}
}

0 comments on commit 0d04398

Please sign in to comment.