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:
parent
d74ca97b51
commit
77b99cf5fe
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue