diff --git a/diagnostic/client.go b/diagnostic/client.go index 4c12f613..0e82a4c6 100644 --- a/diagnostic/client.go +++ b/diagnostic/client.go @@ -159,7 +159,7 @@ func (client *httpClient) GetTunnelConfiguration(ctx context.Context, writer io. return err } - return copyToWriter(response, writer) + return copyJSONToWriter(response, writer) } func (client *httpClient) GetCliConfiguration(ctx context.Context, writer io.Writer) error { @@ -168,7 +168,7 @@ func (client *httpClient) GetCliConfiguration(ctx context.Context, writer io.Wri return err } - return copyToWriter(response, writer) + return copyJSONToWriter(response, writer) } func copyToWriter(response *http.Response, writer io.Writer) error { @@ -176,7 +176,29 @@ func copyToWriter(response *http.Response, writer io.Writer) error { _, err := io.Copy(writer, response.Body) if err != nil { - return fmt.Errorf("error writing metrics: %w", err) + return fmt.Errorf("error writing response: %w", err) + } + + return nil +} + +func copyJSONToWriter(response *http.Response, writer io.Writer) error { + defer response.Body.Close() + + var data interface{} + + decoder := json.NewDecoder(response.Body) + + err := decoder.Decode(&data) + if err != nil { + return fmt.Errorf("diagnostic client error whilst reading response: %w", err) + } + + encoder := newFormattedEncoder(writer) + + err = encoder.Encode(data) + if err != nil { + return fmt.Errorf("diagnostic client error whilst writing json: %w", err) } return nil diff --git a/diagnostic/diagnostic.go b/diagnostic/diagnostic.go index 0d23a305..5b4e68d2 100644 --- a/diagnostic/diagnostic.go +++ b/diagnostic/diagnostic.go @@ -40,6 +40,17 @@ type taskResult struct { path string } +func (result taskResult) MarshalJSON() ([]byte, error) { + s := map[string]string{ + "result": result.Result, + } + if result.Err != nil { + s["error"] = result.Err.Error() + } + + return json.Marshal(s) +} + // Struct used to hold the results of different routines executing the network collection. type networkCollectionResult struct { name string @@ -261,7 +272,9 @@ func jsonNetworkInformationWriter(resultMap map[string]networkCollectionResult) defer networkDumpHandle.Close() - err = json.NewEncoder(networkDumpHandle).Encode(jsonMap) + encoder := newFormattedEncoder(networkDumpHandle) + + err = encoder.Encode(jsonMap) if err != nil { return "", fmt.Errorf("error encoding network information results: %w", err) } @@ -279,7 +292,7 @@ func collectFromEndpointAdapter(collect collectToWriterFunc, fileName string) co err = collect(ctx, dumpHandle) if err != nil { - return "", ErrCreatingTemporaryFile + return "", fmt.Errorf("error running collector: %w", err) } return dumpHandle.Name(), nil @@ -300,7 +313,7 @@ func tunnelStateCollectEndpointAdapter(client HTTPClient, tunnel *TunnelState, f tunnel = tunnelResponse } - encoder := json.NewEncoder(writer) + encoder := newFormattedEncoder(writer) err := encoder.Encode(tunnel) @@ -421,7 +434,9 @@ func createTaskReport(taskReport map[string]taskResult) (string, error) { } defer dumpHandle.Close() - err = json.NewEncoder(dumpHandle).Encode(taskReport) + encoder := newFormattedEncoder(dumpHandle) + + err = encoder.Encode(taskReport) if err != nil { return "", fmt.Errorf("error encoding task results: %w", err) } diff --git a/diagnostic/diagnostic_utils.go b/diagnostic/diagnostic_utils.go index bc811eae..f760994b 100644 --- a/diagnostic/diagnostic_utils.go +++ b/diagnostic/diagnostic_utils.go @@ -3,6 +3,7 @@ package diagnostic import ( "archive/zip" "context" + "encoding/json" "fmt" "io" "net/url" @@ -138,3 +139,10 @@ func FindMetricsServer( return nil, instances, ErrMultipleMetricsServerFound } + +// newFormattedEncoder return a JSON encoder with identation +func newFormattedEncoder(w io.Writer) *json.Encoder { + encoder := json.NewEncoder(w) + encoder.SetIndent("", " ") + return encoder +}