Skip to content

Commit 77b99cf

Browse files
committed
TUN-8784: Set JSON encoder options to print formatted JSON when writing diag files
## Summary The initial implementation produced correct JSON however it was not formatted which would make it harder to read the file by an user. Closes TUN-8784
1 parent d74ca97 commit 77b99cf

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

diagnostic/client.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (client *httpClient) GetTunnelConfiguration(ctx context.Context, writer io.
159159
return err
160160
}
161161

162-
return copyToWriter(response, writer)
162+
return copyJSONToWriter(response, writer)
163163
}
164164

165165
func (client *httpClient) GetCliConfiguration(ctx context.Context, writer io.Writer) error {
@@ -168,15 +168,37 @@ func (client *httpClient) GetCliConfiguration(ctx context.Context, writer io.Wri
168168
return err
169169
}
170170

171-
return copyToWriter(response, writer)
171+
return copyJSONToWriter(response, writer)
172172
}
173173

174174
func copyToWriter(response *http.Response, writer io.Writer) error {
175175
defer response.Body.Close()
176176

177177
_, err := io.Copy(writer, response.Body)
178178
if err != nil {
179-
return fmt.Errorf("error writing metrics: %w", err)
179+
return fmt.Errorf("error writing response: %w", err)
180+
}
181+
182+
return nil
183+
}
184+
185+
func copyJSONToWriter(response *http.Response, writer io.Writer) error {
186+
defer response.Body.Close()
187+
188+
var data interface{}
189+
190+
decoder := json.NewDecoder(response.Body)
191+
192+
err := decoder.Decode(&data)
193+
if err != nil {
194+
return fmt.Errorf("diagnostic client error whilst reading response: %w", err)
195+
}
196+
197+
encoder := newFormattedEncoder(writer)
198+
199+
err = encoder.Encode(data)
200+
if err != nil {
201+
return fmt.Errorf("diagnostic client error whilst writing json: %w", err)
180202
}
181203

182204
return nil

diagnostic/diagnostic.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ type taskResult struct {
4040
path string
4141
}
4242

43+
func (result taskResult) MarshalJSON() ([]byte, error) {
44+
s := map[string]string{
45+
"result": result.Result,
46+
}
47+
if result.Err != nil {
48+
s["error"] = result.Err.Error()
49+
}
50+
51+
return json.Marshal(s)
52+
}
53+
4354
// Struct used to hold the results of different routines executing the network collection.
4455
type networkCollectionResult struct {
4556
name string
@@ -261,7 +272,9 @@ func jsonNetworkInformationWriter(resultMap map[string]networkCollectionResult)
261272

262273
defer networkDumpHandle.Close()
263274

264-
err = json.NewEncoder(networkDumpHandle).Encode(jsonMap)
275+
encoder := newFormattedEncoder(networkDumpHandle)
276+
277+
err = encoder.Encode(jsonMap)
265278
if err != nil {
266279
return "", fmt.Errorf("error encoding network information results: %w", err)
267280
}
@@ -279,7 +292,7 @@ func collectFromEndpointAdapter(collect collectToWriterFunc, fileName string) co
279292

280293
err = collect(ctx, dumpHandle)
281294
if err != nil {
282-
return "", ErrCreatingTemporaryFile
295+
return "", fmt.Errorf("error running collector: %w", err)
283296
}
284297

285298
return dumpHandle.Name(), nil
@@ -300,7 +313,7 @@ func tunnelStateCollectEndpointAdapter(client HTTPClient, tunnel *TunnelState, f
300313
tunnel = tunnelResponse
301314
}
302315

303-
encoder := json.NewEncoder(writer)
316+
encoder := newFormattedEncoder(writer)
304317

305318
err := encoder.Encode(tunnel)
306319

@@ -421,7 +434,9 @@ func createTaskReport(taskReport map[string]taskResult) (string, error) {
421434
}
422435
defer dumpHandle.Close()
423436

424-
err = json.NewEncoder(dumpHandle).Encode(taskReport)
437+
encoder := newFormattedEncoder(dumpHandle)
438+
439+
err = encoder.Encode(taskReport)
425440
if err != nil {
426441
return "", fmt.Errorf("error encoding task results: %w", err)
427442
}

diagnostic/diagnostic_utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package diagnostic
33
import (
44
"archive/zip"
55
"context"
6+
"encoding/json"
67
"fmt"
78
"io"
89
"net/url"
@@ -138,3 +139,10 @@ func FindMetricsServer(
138139

139140
return nil, instances, ErrMultipleMetricsServerFound
140141
}
142+
143+
// newFormattedEncoder return a JSON encoder with identation
144+
func newFormattedEncoder(w io.Writer) *json.Encoder {
145+
encoder := json.NewEncoder(w)
146+
encoder.SetIndent("", " ")
147+
return encoder
148+
}

0 commit comments

Comments
 (0)