-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
251 lines (204 loc) · 6.98 KB
/
push.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright © 2024 Patrick Hermann patrick.hermann@sva.de
*/
package cmd
import (
"bytes"
"fmt"
"html/template"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/stuttgart-things/machineshop/internal"
sthingsCli "github.com/stuttgart-things/sthingsCli"
"github.com/spf13/cobra"
)
type Message struct {
Title string `json:"title,omitempty"` // if empty: info
Message string `json:"info,omitempty"` // if empty: title
Severity string `json:"severity,omitempty"` // default: info
Author string `json:"author,omitempty"` // default: unknown
Timestamp string `json:"timestamp,omitempty"` // generate timestamp func
System string `json:"system,omitempty"` // default: unknown
Tags string `json:"tags,omitempty"` // empty
AssigneeAddress string `json:"assigneeaddress,omitempty"` // empty
AssigneeName string `json:"assigneename,omitempty"` // empty
Artifacts string `json:"artifacts,omitempty"` // empty
Url string `json:"url,omitempty"` // empty
}
var (
contentType = "application/json"
// url = "https://homerun.homerun-dev.sthings-vsphere.labul.sva.de/generic"
minioLocation = "us-east-1"
colors = map[string]string{
"red": "#FF0000",
"green": "#00FF00",
"blue": "#0000FF",
"orange": "#DF813D",
"purple": "#726EAD",
// Add more colors as needed
}
token = "IhrGeheimerToken"
homeRunBodyData = `{
"Title": "{{ .Title }}",
"Message": "{{ .Message }}",
"Severity": "{{ .Severity }}",
"Author": "{{ .Author }}",
"Timestamp": "{{ .Timestamp }}",
"System": "{{ .System }}",
"Tags": "{{ .Tags }}",
"AssigneeAddress": "{{ .AssigneeAddress }}",
"AssigneeName": "{{ .AssigneeName }}",
"Artifacts": "{{ .Artifacts }}",
"Url": "{{ .Url }}"
}`
)
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
Short: "push things",
Long: `push things to external systems`,
Run: func(cmd *cobra.Command, args []string) {
// FLAGS
target, _ := cmd.LocalFlags().GetString("target")
source, _ := cmd.LocalFlags().GetString("source")
color, _ := cmd.LocalFlags().GetString("color")
title, _ := cmd.LocalFlags().GetString("title")
body, _ := cmd.LocalFlags().GetString("message")
author, _ := cmd.LocalFlags().GetString("author")
severity, _ := cmd.LocalFlags().GetString("severity")
system, _ := cmd.LocalFlags().GetString("system")
destination, _ := cmd.LocalFlags().GetString("destination")
tags, _ := cmd.LocalFlags().GetString("tags")
assignee, _ := cmd.LocalFlags().GetString("assignee")
assigneeUrl, _ := cmd.LocalFlags().GetString("assigneeUrl")
artifacts, _ := cmd.LocalFlags().GetString("artifacts")
url, _ := cmd.LocalFlags().GetString("url")
if destination != "" {
switch target {
case "homerun":
// GETTING TIMESTAMP
dt := time.Now()
timestamp := dt.Format("01-02-2006 15:04:05")
if author == "" {
author = "machineshop"
}
if system == "" {
system = "machineshop"
}
if title == "" || body == "" {
log.Error("TITILE AND/OR BODY EMPTY. EXITING")
os.Exit(3)
}
log.Info("PUSHING TO HOMERUN")
messageBody := Message{
Title: title,
Message: body,
Severity: severity,
Author: author,
Timestamp: timestamp,
System: system,
Tags: tags,
AssigneeAddress: assigneeUrl,
AssigneeName: assignee,
Artifacts: artifacts,
Url: url,
}
rendered := RenderBody(homeRunBodyData, messageBody)
fmt.Println(rendered)
// CREATE HTTP-Request
req, err := http.NewRequest("POST", destination, bytes.NewBuffer([]byte(rendered)))
if err != nil {
fmt.Println("faiulure at creating requests:", err)
return
}
// ADD HEADER
req.Header.Set("Content-Type", contentType)
req.Header.Set("X-Auth-Token", token)
// CREATE HTTP-Client + SEND REQUEST
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("error at sending request:", err)
return
}
defer resp.Body.Close()
// READ THE ANSWER
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("error reading answer:", err)
return
}
fmt.Println("answer status:", resp.Status)
fmt.Println("answer body:", string(body))
case "teams":
if colors[color] == "" {
target = "orange"
}
log.Info("PUSHING TO MS TEAMS")
log.Info("MESSAGE: ", source)
log.Info("MS TEAMS URL: ", destination)
log.Info("COLOR: ", color)
webhook := sthingsCli.MsTeamsWebhook{Title: "Machineshop", Text: source, Color: colors[color], Url: destination}
sthingsCli.SendWebhookToTeams(webhook)
case "minio":
log.Info("PUSHING TO MINIO S3")
log.Info("MINIO URL: ", os.Getenv("MINIO_ADDR"))
log.Info("SOURCE: ", source)
log.Info("TARGET: ", destination)
// VERIFY IF SOURCE FILE IS EXISTING
internal.ValidateSourceFile(source)
clientCreated, minioClient := sthingsCli.CreateMinioClient()
if !clientCreated {
log.Error("MINIO CLIENT CAN NOT BE CREATED")
os.Exit(3)
} else {
log.Info("MINIO CLIENT CREATED")
destination := strings.Split(destination, ":")
bucket := destination[0]
objectName := destination[1]
log.Info("BUCKET: ", bucket)
log.Info("OBJECT: ", objectName)
sthingsCli.CreateMinioBucket(minioClient, bucket, minioLocation)
uploaded, fileSize := sthingsCli.UploadObjectToMinioBucket(minioClient, bucket, source, objectName)
if uploaded {
log.Info("SUCCESSFULLY UPLOADED OF SIZE: ", fileSize)
}
}
}
} else {
log.Error("DESTINATION PATH SEEMS SO BE EMPTY")
}
},
}
func init() {
rootCmd.AddCommand(pushCmd)
pushCmd.Flags().String("source", "", "source file path")
pushCmd.Flags().String("destination", "", "destination path")
pushCmd.Flags().String("target", "minio", "push target")
pushCmd.Flags().String("color", "orange", "color for webhook message")
pushCmd.Flags().String("title", "", "title of homerun message")
pushCmd.Flags().String("message", "", "homerun message body")
pushCmd.Flags().String("severity", "info", "homerun message severity")
pushCmd.Flags().String("author", "machineShop", "homerun message author")
pushCmd.Flags().String("system", "", "homerun message system")
pushCmd.Flags().String("tags", "", "homerun message tags")
pushCmd.Flags().String("assignee", "", "homerun message assignee")
pushCmd.Flags().String("assigneeUrl", "", "homerun message assignee url")
pushCmd.Flags().String("artifacts", "", "homerun artifacts")
pushCmd.Flags().String("url", "", "homerun message url/link")
}
func RenderBody(templateData string, object interface{}) string {
tmpl, err := template.New("template").Parse(templateData)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, object)
if err != nil {
fmt.Println(err)
}
return buf.String()
}