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
This commit is contained in:
Luis Neto 2024-12-10 13:01:24 -08:00
parent d74ca97b51
commit 77b99cf5fe
3 changed files with 52 additions and 7 deletions

View File

@ -159,7 +159,7 @@ func (client *httpClient) GetTunnelConfiguration(ctx context.Context, writer io.
return err return err
} }
return copyToWriter(response, writer) return copyJSONToWriter(response, writer)
} }
func (client *httpClient) GetCliConfiguration(ctx context.Context, writer io.Writer) error { 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 err
} }
return copyToWriter(response, writer) return copyJSONToWriter(response, writer)
} }
func copyToWriter(response *http.Response, writer io.Writer) error { 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) _, err := io.Copy(writer, response.Body)
if err != nil { 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 return nil

View File

@ -40,6 +40,17 @@ type taskResult struct {
path string 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. // Struct used to hold the results of different routines executing the network collection.
type networkCollectionResult struct { type networkCollectionResult struct {
name string name string
@ -261,7 +272,9 @@ func jsonNetworkInformationWriter(resultMap map[string]networkCollectionResult)
defer networkDumpHandle.Close() defer networkDumpHandle.Close()
err = json.NewEncoder(networkDumpHandle).Encode(jsonMap) encoder := newFormattedEncoder(networkDumpHandle)
err = encoder.Encode(jsonMap)
if err != nil { if err != nil {
return "", fmt.Errorf("error encoding network information results: %w", err) 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) err = collect(ctx, dumpHandle)
if err != nil { if err != nil {
return "", ErrCreatingTemporaryFile return "", fmt.Errorf("error running collector: %w", err)
} }
return dumpHandle.Name(), nil return dumpHandle.Name(), nil
@ -300,7 +313,7 @@ func tunnelStateCollectEndpointAdapter(client HTTPClient, tunnel *TunnelState, f
tunnel = tunnelResponse tunnel = tunnelResponse
} }
encoder := json.NewEncoder(writer) encoder := newFormattedEncoder(writer)
err := encoder.Encode(tunnel) err := encoder.Encode(tunnel)
@ -421,7 +434,9 @@ func createTaskReport(taskReport map[string]taskResult) (string, error) {
} }
defer dumpHandle.Close() defer dumpHandle.Close()
err = json.NewEncoder(dumpHandle).Encode(taskReport) encoder := newFormattedEncoder(dumpHandle)
err = encoder.Encode(taskReport)
if err != nil { if err != nil {
return "", fmt.Errorf("error encoding task results: %w", err) return "", fmt.Errorf("error encoding task results: %w", err)
} }

View File

@ -3,6 +3,7 @@ package diagnostic
import ( import (
"archive/zip" "archive/zip"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
@ -138,3 +139,10 @@ func FindMetricsServer(
return nil, instances, ErrMultipleMetricsServerFound 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
}