-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
151 lines (122 loc) · 4.23 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
149
150
151
package cmd
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
func logAndExitf(message string, a ...interface{}) {
fmt.Printf(message+"\n", a...)
os.Exit(1)
}
func doOctopusRequest(body []byte, uri string, method string) (responsebody []byte, status int) {
httpreq, _ := http.NewRequest(method, uri, nil)
httpreq.Header.Set("X-Octopus-ApiKey", apiKey)
if method == "GET" {
response, err := client.Do(httpreq)
if err != nil {
logAndExitf("Error executing request %s to %s:\n%s", method, uri, err.Error())
}
defer response.Body.Close()
responsebody, err := ioutil.ReadAll(response.Body)
if err != nil {
logAndExitf("Error reading response body of GET to %s:\n%s", uri, err.Error())
}
return responsebody, response.StatusCode
} else if method == "PUT" || method == "POST" {
httpreq, _ := http.NewRequest(method, uri, bytes.NewBuffer(body))
httpreq.Header.Set("X-Octopus-ApiKey", apiKey)
response, err := client.Do(httpreq)
if err != nil {
logAndExitf("Error executing request %s to %s:\n%s", method, uri, err.Error())
}
defer response.Body.Close()
responsebody, err := ioutil.ReadAll(response.Body)
if err != nil {
logAndExitf("Error reading response body of PUT to %s:\n%s", uri, err.Error())
}
return responsebody, response.StatusCode
}
return []byte("Method " + method + " not supported"), 0
}
func verifyVariableType(svtype variable) (vtype string, err error) {
for _, validType := range validVariableTypes {
if validType == svtype.Type {
return svtype.Type, nil
}
}
if svtype.Type == "" {
return "String", nil
}
var errorstring string
for _, vvar := range validVariableTypes {
errorstring = errorstring + vvar + ", "
}
errorstring = strings.TrimSuffix(errorstring, ", ")
return "", errors.New("Variable type '" + svtype.Type + "' for variable '" + svtype.Name + "' is not valid. Valid types are " + errorstring)
}
func verifySyntaxType(satype step) (atype string, err error) {
for _, validType := range validScriptSyntaxTypes {
if validType == satype.Type {
return satype.Type, nil
}
}
var errorstring string
for _, vsyn := range validScriptSyntaxTypes {
errorstring = errorstring + vsyn + ", "
}
errorstring = strings.TrimSuffix(errorstring, ", ")
return "", errors.New("Script syntax type '" + satype.Type + "' for process step '" + satype.Name + "' is not valid. Syntax types are case sensitive. Valid types are " + errorstring)
}
func verifyTenancyType(tp project) (ap string, err error) {
for _, validType := range validTenancyTypes {
if validType == tp.Tenanted {
return tp.Tenanted, nil
}
}
var errorstring string
for _, vsyn := range validTenancyTypes {
errorstring = errorstring + vsyn + ", "
}
errorstring = strings.TrimSuffix(errorstring, ", ")
return "", errors.New("Tenancy type '" + tp.Tenanted + "' for project '" + tp.Name + "' is not valid. Valid tenancy types are " + errorstring)
}
func verifyScopeType(sctype string) (vtype string, err error) {
for _, validType := range validScopeTypes {
if validType == sctype {
return sctype, nil
}
}
var errorstring string
for _, vsc := range validScopeTypes {
errorstring = errorstring + vsc + ", "
}
errorstring = strings.TrimSuffix(errorstring, ", ")
return "", errors.New("Scope type '" + sctype + "' is invalid. Valid scope types are " + errorstring)
}
func getProjectSlug(name string) (slug string) {
name = strings.ToLower(name)
regex := regexp.MustCompile("[^0-9A-Za-z]+")
slugged := regex.ReplaceAll([]byte(name), []byte("-"))
return string(slugged)
}
func getLifecycle(ls []octopusLifecycle, name string, ID string) (l octopusLifecycle, err error) {
for _, l := range ls {
if l.Name == name || l.ID == ID {
return l, nil
}
}
return octopusLifecycle{}, errors.New("Lifecycle with name " + name + " not found")
}
func getProjectGroup(pgs []octopusProjectGroup, name string, ID string) (pg octopusProjectGroup, err error) {
for _, pg := range pgs {
if pg.Name == name || pg.ID == ID {
return pg, nil
}
}
return octopusProjectGroup{}, errors.New("Project group with name " + name + " not found")
}