-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcore.go
More file actions
187 lines (157 loc) · 5.68 KB
/
Copy pathcore.go
File metadata and controls
187 lines (157 loc) · 5.68 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"os"
"sync"
"github.com/newrelic/newrelic-diagnostics-cli/config"
"github.com/newrelic/newrelic-diagnostics-cli/internal/haberdasher"
log "github.com/newrelic/newrelic-diagnostics-cli/logger"
"github.com/newrelic/newrelic-diagnostics-cli/output"
"github.com/newrelic/newrelic-diagnostics-cli/output/color"
"github.com/newrelic/newrelic-diagnostics-cli/scriptrunner"
"github.com/newrelic/newrelic-diagnostics-cli/usage"
"github.com/newrelic/newrelic-diagnostics-cli/version"
)
func main() {
runID := generateRunID()
config.ParseFlags()
log.Debug("---------------------------------------------------------------------------------------------")
log.Debugf("Running nrdiag with version: %s and build timestamp %s\n", config.Version, config.BuildTimestamp)
log.Debugf("Run ID: %s\n", runID)
log.Debug("nrdiag was run with options", os.Args)
//Error setting proxy and they specifically included one so let's break out of the program before we attempt any non-proxied calls.
_, err := processHTTPProxy()
if err != nil {
log.Info("Proxy configuration found, but unable to use. \nError: " + err.Error() + "\nExiting program.")
os.Exit(3)
}
options, overrides := processOverrides()
// Setup Haberdasher client
haberdasher.InitializeDefaultClient()
haberdasher.DefaultClient.SetRunID(runID)
haberdasher.DefaultClient.SetUserAgent("Nrdiag_/" + config.Version)
if config.HaberdasherURL == "" && !config.Flags.Quiet {
log.Info("No Haberdasher base URL set. Defaulting to localhost")
} else {
haberdasher.DefaultClient.SetBaseURL(config.HaberdasherURL)
}
if config.Flags.Version && config.Flags.Quiet {
// Support for automated version check by newrelic-cli
current := version.ProcessAutoVersionCheck()
if !current {
os.Exit(1)
} else {
os.Exit(0)
}
}
// Set up script catalog
scriptCatalog := &scriptrunner.Catalog{
Deps: &scriptrunner.CatalogDependencies{},
}
// List available scripts
if config.Flags.ListScripts {
printScriptList(scriptCatalog)
os.Exit(0)
}
var scriptData *scriptrunner.ScriptData
if config.Flags.Script != "" {
scriptData = processScript(scriptCatalog)
// View script
if !config.Flags.Run {
log.Infof("%s\n", string(scriptData.Content))
os.Exit(0)
}
// Run script
log.Infof(color.ColorString(color.White, "\nRunning script: %s %s\n"), scriptData.Path, scriptData.Flags)
srunner := &scriptrunner.Runner{
Deps: &scriptrunner.RunnerDependencies{
CmdLineOptions: options,
},
}
scriptData.Output, err = srunner.Run(scriptData.Content, scriptData.Path, scriptData.Flags)
if err != nil {
// exit if the script fails to run
log.Fatalf("Error while running script: %s", err.Error())
}
scriptData.AddtlFiles = srunner.FindScriptAddtlFiles(scriptData.AddtlFilesPatterns)
if len(scriptData.Output) > 0 {
// Print script output to screen
output.PrintScriptOutput(string(scriptData.Output))
// Write script output to a file
output.WriteScriptOutputFile(scriptData.OutputPath, scriptData.Output, options)
}
}
go processTasksToRun()
// if statements for doing stuff with args
if config.Flags.Help {
processHelp()
} else if config.Flags.Version {
version.ProcessVersion(promptUser)
} else if config.Flags.Interactive {
// do interactive stuff
} else {
// the wait group is way of tracking open threads
// anytime you spawn an async function, increment and pass it in
// ... the called function is responsible for decrementing when done
var wg sync.WaitGroup
// zip file is passed around as a dependency for other functions
zipfile := output.CreateZip()
// create the filelist file, exit if it can't be created
flErr := output.CreateFileList()
if flErr != nil {
log.Info("Error creating filelist", err)
os.Exit(3)
}
// check if any files/directories should be included in the zip
if config.Flags.Include != "" {
output.HandleIncludeFlag(zipfile, config.Flags.Include)
}
wg.Add(1) // run the tasks in goroutine
go processTasks(options, overrides, &wg)
wg.Add(1) // collect files the tasks produce and add them to the zip file
go output.ProcessFilesChannel(zipfile, &wg)
// this is a synchronous function that reads from the results channel
// does not need the wait group since it blocks
outputResults := output.WriteLineResults()
if !config.Flags.Quiet {
// writes to the screen
output.WriteSummary(outputResults)
}
// block on wait group so program does not exit prematurely
log.Info(color.ColorString(color.White, "Creating nrdiag-output.zip"))
wg.Wait()
// creates the output file
output.WriteOutputFile(outputResults, scriptData)
// copy our output file(s) to the zip file
output.CopyOutputToZip(zipfile)
if scriptData != nil {
err := output.CopyScriptOutputsToZip(scriptData, zipfile)
if err != nil {
log.Infof("Failed to copy script outputs to zip: %s\n", err.Error())
}
}
// copy the file list to the zip file last to ensure it's up to date
output.CopyFileListToZip(zipfile)
// ...and close it out
output.CloseZip(zipfile)
// upload any files (zip and json)
processUploads()
// deal with haberdasher data
if !config.Flags.UsageOptOut {
usage.SendUsageData(outputResults, runID)
}
if !config.Flags.SkipVersionCheck {
version.ProcessAutoVersionCheck()
}
if config.Flags.Suites == "" && config.Flags.Tasks == "" {
var command, option string
if config.Flags.InNewRelicCLI {
command = "newrelic diagnose run"
option = "--list-suites"
} else {
command = os.Args[0]
option = "-h suites"
}
log.Infof("\n\nFor better results, run Diagnostics CLI with the 'suites' option to target a New Relic product. To learn how to use this option, run: '%s %s'\n\n", command, option)
}
}
}