Skip to content

Commit 30dae7a

Browse files
authored
Add json-pretty as an output option (#214)
* Add json-pretty format option
1 parent 23d0c78 commit 30dae7a

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ Results can be output in JSON using the `json` format:
152152
[{"id":3337874,"template_id":0,"name":"test.example.co.uk","type":"A","content":"1.2.3.4","updated_at":"2019-03-19T16:33:55+00:00","ttl":0,"priority":0}]
153153
```
154154

155+
You can also the use the `json-pretty` format to output pretty-printed JSON:
156+
157+
```
158+
> ans safedns zone record show example.co.uk 3337874 --output json-pretty
159+
[
160+
{
161+
"id": 3337874,
162+
"template_id": 0,
163+
"name": "test.example.co.uk",
164+
"type": "A",
165+
"content": "1.2.3.4",
166+
"updated_at": "2019-03-19T16:33:55+00:00",
167+
"ttl": 0,
168+
"priority": 0
169+
}
170+
]
171+
```
172+
155173
### YAML
156174

157175
Results can be output in YAML using the `yaml` format:

internal/pkg/output/output_handler.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func (o *OutputHandler) Output(cmd *cobra.Command, d interface{}) error {
7373

7474
switch format {
7575
case "json":
76-
return o.JSON(d)
76+
return o.JSON(d, false)
77+
case "json-pretty":
78+
return o.JSON(d, true)
7779
case "list":
7880
return o.List(cmd, d)
7981
case "value":
@@ -94,8 +96,14 @@ func (o *OutputHandler) Output(cmd *cobra.Command, d interface{}) error {
9496
}
9597
}
9698

97-
func (o *OutputHandler) JSON(d interface{}) error {
98-
out, err := json.Marshal(d)
99+
func (o *OutputHandler) JSON(d interface{}, pretty bool) error {
100+
var out []byte
101+
var err error
102+
if pretty {
103+
out, err = json.MarshalIndent(d, "", " ")
104+
} else {
105+
out, err = json.Marshal(d)
106+
}
99107
if err != nil {
100108
return fmt.Errorf("failed to marshal json: %s", err)
101109
}

0 commit comments

Comments
 (0)