Skip to content

Commit f06cd9e

Browse files
authored
Merge pull request #172 from replicatedhq/laverya/refactor-byte-array-interfaces
refactor byte array interfaces
2 parents 406e1e6 + 7dc93dd commit f06cd9e

File tree

16 files changed

+102
-330
lines changed

16 files changed

+102
-330
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package cli
22

33
import (
44
"crypto/tls"
5-
"encoding/base64"
6-
"encoding/json"
75
"fmt"
86
"io/ioutil"
97
"net/http"
@@ -262,7 +260,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
262260
}
263261

264262
if result != nil {
265-
err = parseAndSaveCollectorOutput(string(result), bundlePath)
263+
err = saveCollectorOutput(result, bundlePath)
266264
if err != nil {
267265
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
268266
continue
@@ -282,46 +280,17 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog
282280
return filename, nil
283281
}
284282

285-
func parseAndSaveCollectorOutput(output string, bundlePath string) error {
286-
input := make(map[string]interface{})
287-
if err := json.Unmarshal([]byte(output), &input); err != nil {
288-
return errors.Wrap(err, "unmarshal output")
289-
}
290-
291-
for filename, maybeContents := range input {
283+
func saveCollectorOutput(output map[string][]byte, bundlePath string) error {
284+
for filename, maybeContents := range output {
292285
fileDir, fileName := filepath.Split(filename)
293286
outPath := filepath.Join(bundlePath, fileDir)
294287

295288
if err := os.MkdirAll(outPath, 0777); err != nil {
296289
return errors.Wrap(err, "create output file")
297290
}
298291

299-
switch maybeContents.(type) {
300-
case string:
301-
decoded, err := base64.StdEncoding.DecodeString(maybeContents.(string))
302-
if err != nil {
303-
return errors.Wrap(err, "decode collector output")
304-
}
305-
306-
if err := writeFile(filepath.Join(outPath, fileName), decoded); err != nil {
307-
return errors.Wrap(err, "write collector output")
308-
}
309-
310-
case map[string]interface{}:
311-
for k, v := range maybeContents.(map[string]interface{}) {
312-
s, _ := filepath.Split(filepath.Join(outPath, fileName, k))
313-
if err := os.MkdirAll(s, 0777); err != nil {
314-
return errors.Wrap(err, "write output directories")
315-
}
316-
317-
decoded, err := base64.StdEncoding.DecodeString(v.(string))
318-
if err != nil {
319-
return errors.Wrap(err, "decode output")
320-
}
321-
if err := writeFile(filepath.Join(outPath, fileName, k), decoded); err != nil {
322-
return errors.Wrap(err, "write output")
323-
}
324-
}
292+
if err := writeFile(filepath.Join(outPath, fileName), maybeContents); err != nil {
293+
return errors.Wrap(err, "write collector output")
325294
}
326295
}
327296

pkg/collect/cluster_info.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,23 @@ type ClusterInfoOutput struct {
1818
Errors []byte `json:"cluster-info/errors.json,omitempty"`
1919
}
2020

21-
func ClusterInfo(ctx *Context) ([]byte, error) {
21+
func ClusterInfo(ctx *Context) (map[string][]byte, error) {
2222
client, err := kubernetes.NewForConfig(ctx.ClientConfig)
2323
if err != nil {
2424
return nil, errors.Wrap(err, "Failed to create kubernetes clientset")
2525
}
2626

27-
clusterInfoOutput := ClusterInfoOutput{}
27+
clusterInfoOutput := map[string][]byte{}
2828

2929
// cluster version
3030
clusterVersion, clusterErrors := clusterVersion(client)
31-
clusterInfoOutput.ClusterVersion = clusterVersion
32-
clusterInfoOutput.Errors, err = marshalNonNil(clusterErrors)
31+
clusterInfoOutput["cluster-info/cluster_version.json"] = clusterVersion
32+
clusterInfoOutput["cluster-info/errors.json"], err = marshalNonNil(clusterErrors)
3333
if err != nil {
3434
return nil, errors.Wrap(err, "failed to marshal errors")
3535
}
3636

37-
b, err := json.MarshalIndent(clusterInfoOutput, "", " ")
38-
if err != nil {
39-
return nil, errors.Wrap(err, "failed to marshal cluster info")
40-
}
41-
42-
return b, nil
37+
return clusterInfoOutput, nil
4338
}
4439

4540
func clusterVersion(client *kubernetes.Clientset) ([]byte, []string) {

0 commit comments

Comments
 (0)