This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
/
utils.go
118 lines (102 loc) · 2.71 KB
/
utils.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
package utils
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/client_cloudbreak/v1util"
)
var r *rand.Rand
const randbytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
type utilClient interface {
CheckClientVersion(params *v1util.CheckClientVersionParams) (*v1util.CheckClientVersionOK, error)
}
func CheckClientVersion(client utilClient, version string) {
resp, err := client.CheckClientVersion(v1util.NewCheckClientVersionParams().WithVersion(version))
if err != nil {
LogErrorAndExit(err)
}
valid := resp.Payload.VersionCheckOk
message := resp.Payload.Message
if valid == nil || !*valid {
LogErrorAndExit(errors.New(message))
}
}
func RandStr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = randbytes[rand.Intn(len(randbytes))]
}
return string(b)
}
func SafeInt32Convert(value *int32) int32 {
if value == nil {
return 0
}
return *value
}
func SafeStringConvert(value *string) string {
if value == nil {
return ""
}
return *value
}
func EscapeStringToJson(input string) string {
return strings.Replace(strings.Replace(input, "\\", "\\\\", -1), "\"", "\\\"", -1)
}
func ReadFile(fileLocation string) []byte {
log.Infof("[readFile] read content from file: %s", fileLocation)
content, err := ioutil.ReadFile(fileLocation)
if err != nil {
LogErrorAndExit(err)
}
return content
}
func ReadContentFromURL(urlLocation string, client *http.Client) []byte {
log.Infof("[readFile] read content from URL: %s", urlLocation)
resp, err := client.Get(urlLocation)
if err != nil {
LogErrorAndExit(err)
}
defer func() {
if err = resp.Body.Close(); err != nil {
LogErrorAndExit(err)
}
}()
if resp.StatusCode != 200 {
LogErrorMessageAndExit(fmt.Sprintf("Couldn't download content from URL, response code is %d, expected: 200.", resp.StatusCode))
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
LogErrorAndExit(err)
}
return content
}
// Returns a slice of strings. If the string is empty it will return an empty slice.
func DelimitedStringToArray(text, delimiter string) []string {
if len(text) == 0 {
return make([]string, 0)
}
return strings.Split(text, delimiter)
}
func CopyToByTargets(sourceParameters map[string]interface{}, parameters ...string) map[string]interface{} {
destinationParameters := map[string]interface{}{}
for _, actual := range parameters {
destinationParameters[actual] = sourceParameters[actual]
}
return destinationParameters
}
func SafeStringTypeAssert(value interface{}) string {
strValue, ok := value.(string)
if ok {
return strValue
}
return ""
}