/
node_diagnostics_download.go
63 lines (55 loc) · 1.21 KB
/
node_diagnostics_download.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package node
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
func newCmdNodeDiagnosticsDownload() *cobra.Command {
var location string
cmd := &cobra.Command{
Use: "download <bundle>",
Short: "Download a diagnostics bundle",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if location == "" {
var err error
location, err = os.Getwd()
if err != nil {
return err
}
}
client := diagnosticsClient()
isBundleFound := false
list, err := client.List()
if err != nil {
return err
}
for _, bundles := range list {
for _, bundle := range bundles {
if filepath.Base(bundle.File) == args[0] {
isBundleFound = true
}
}
}
if !isBundleFound {
return fmt.Errorf("Unable to find bundle '%s'", args[0])
}
out, err := os.Create(filepath.Join(location, args[0]))
if err != nil {
return err
}
defer out.Close()
resp, err := client.Get(args[0])
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
return err
},
}
cmd.Flags().StringVar(&location, "location", "", "Where to download the diagnostics bundle")
return cmd
}