forked from newrelic/newrelic-diagnostics-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreHelper.go
117 lines (96 loc) · 2.81 KB
/
coreHelper.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
package main
import (
"bufio"
"fmt"
"net"
"net/http"
"os"
"time"
"strings"
"github.com/google/uuid"
"github.com/newrelic/newrelic-diagnostics-cli/config"
log "github.com/newrelic/newrelic-diagnostics-cli/logger"
"github.com/newrelic/newrelic-diagnostics-cli/tasks"
)
type override struct {
tasks.Identifier
key string
value string
}
func parseOverrides(overrides string) []override {
var sliceOverrides []override
//First we need to split on a comma to get each override provided
splitOverrides := strings.Split(overrides, ",")
if len(splitOverrides[0]) != 0 { //Check for an empty override
for _, eachOverride := range splitOverrides {
log.Debug("pre-parsing override is :", eachOverride)
//Now split on equals to get the value
taskValue := strings.Split(eachOverride, "=")
if len(taskValue) > 1 {
//And again to get the identifier and key
taskKey := strings.Split(taskValue[0], ".")
if len(taskKey) > 1 {
log.Debug("Override Identifier is ", tasks.IdentifierFromString(taskKey[0]))
log.Debug("Override Key is ", taskKey[1])
log.Debug("Override Value is", taskValue[1])
overridee := override{tasks.IdentifierFromString(taskKey[0]), taskKey[1], taskValue[1]}
//Now add it to the list
sliceOverrides = append(sliceOverrides, overridee)
}
}
}
}
return sliceOverrides
}
func ProxyParseNSet() (set bool) {
//This sets the default
var DefaultDialer = &net.Dialer{Timeout: 1000 * time.Millisecond}
http.DefaultTransport = &http.Transport{Dial: DefaultDialer.Dial, Proxy: http.ProxyFromEnvironment}
return true
}
// posString returns the first index of element in slice.
// If slice does not contain element, returns -1.
func posString(slice []string, element string) int {
for index, elem := range slice {
if elem == element {
return index
}
}
return -1
}
// containsString returns true iff slice contains element
func containsString(slice []string, element string) bool {
return !(posString(slice, element) == -1)
}
//similar to tasks - This takes the input string as the query to the end users and waits for a response
func promptUser(msg string) bool {
if config.Flags.YesToAll {
return true
}
prompt := "Choose 'y' or 'n', then press enter: "
yesResponses := []string{"y", "yes"}
noResponses := []string{"n", "no"}
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(msg)
fmt.Print(prompt)
for scanner.Scan() {
userInput := strings.ToLower(scanner.Text())
if tasks.ContainsString(noResponses, userInput) {
return false
}
if tasks.ContainsString(yesResponses, userInput) {
return true
}
//Repeat prompt if invalid input is provided
fmt.Printf("%s", prompt)
}
return false
}
func generateRunID() string {
uuid, err := uuid.NewUUID()
if err != nil {
log.Debug("Error generating UUID", err)
return ""
}
return uuid.String()
}