-
Notifications
You must be signed in to change notification settings - Fork 73
/
helpers.go
148 lines (128 loc) · 3.38 KB
/
helpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package diagnose
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"math/bits"
"net/http"
"os"
"os/exec"
"path"
"runtime"
"github.com/fatih/color"
"github.com/newrelic/newrelic-cli/internal/install/execution"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/install/ux"
"github.com/newrelic/newrelic-cli/internal/utils"
log "github.com/sirupsen/logrus"
)
func downloadBinary() error {
log.Info("Determining OS...")
var executable string
if bits.UintSize == 64 {
executable = "nrdiag_x64"
} else {
executable = "nrdiag"
}
var subdir string
if runtime.GOOS == "windows" {
subdir = "win"
executable = executable + ".exe"
} else if runtime.GOOS == "darwin" {
subdir = "mac"
} else if runtime.GOOS == "linux" {
subdir = "linux"
} else {
return fmt.Errorf("unknown operating system: %s", runtime.GOOS)
}
log.Infof("Downloading %s", downloadURL)
resp, err := http.Get(downloadURL)
if err != nil {
log.Warnf("failed to download the latest nrdiag: %s", err)
home, _ := utils.GetDefaultConfigDirectory()
log.Infof("If this problem persists, you can download the zip file from https://download.newrelic.com/nrdiag/nrdiag_latest.zip and place the appropriate binary for your system in %s/bin, then try again.", home)
return err
}
defer resp.Body.Close()
tmpFile, err := ioutil.TempFile(os.TempDir(), "nrdiag-")
if err != nil {
return err
}
defer os.Remove(tmpFile.Name())
_, err = io.Copy(tmpFile, resp.Body)
if err != nil {
return err
}
tmpFile.Close()
zipReader, err := zip.OpenReader(tmpFile.Name())
if err != nil {
return err
}
defer zipReader.Close()
targetPath := path.Join("nrdiag", subdir, executable)
var zipped *zip.File
for _, f := range zipReader.File {
if f.Name == targetPath {
zipped = f
break
}
}
if zipped == nil {
return fmt.Errorf("executable %s not found in zip file", targetPath)
}
log.Info("Extracting... ")
out, err := os.OpenFile(getBinaryPath(), os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
defer out.Close()
r, err := zipped.Open()
if err != nil {
return err
}
_, err = io.Copy(out, r)
if err != nil {
return err
}
return nil
}
func ensureBinaryExists() error {
destination := getBinaryPath()
err := os.MkdirAll(path.Dir(destination), 0777)
if err != nil {
return err
}
if _, err = os.Stat(destination); os.IsNotExist(err) {
log.Infof("nrdiag binary not found in %s", destination)
return downloadBinary()
}
return nil
}
func runDiagnostics(args ...string) error {
err := ensureBinaryExists()
if err != nil {
return err
}
diagnostics := exec.Command(getBinaryPath())
diagnostics.Stdout = os.Stdout
diagnostics.Stderr = os.Stderr
diagnostics.Env = append(diagnostics.Env, "NEWRELIC_CLI_SUBPROCESS=true")
diagnostics.Args = append(diagnostics.Args, args...)
return diagnostics.Run()
}
func getBinaryPath() string {
configDirectory, err := utils.GetDefaultConfigDirectory()
if err != nil {
log.Fatal(err)
}
return path.Join(configDirectory, "bin", "nrdiag")
}
const downloadURL = "https://download.newrelic.com/nrdiag/nrdiag_latest.zip"
func PrintPaymentRequiredErrorMessage() {
fmt.Println()
fmt.Println(color.YellowString("! Data limit exceeded"))
fmt.Println(types.PaymentRequiredExceptionMessage)
fmt.Printf("\n %s %s", color.GreenString(ux.IconArrowRight), execution.GetAccountPlanManagementURL())
fmt.Print("\n\n")
}