/
push.go
67 lines (62 loc) · 2.25 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
package cmd
import (
"bytes"
"encoding/json"
"os"
"time"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/api"
"github.com/flanksource/commons/files"
"github.com/flanksource/commons/logger"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
func init() {
check := pkg.Check{}
status := pkg.CheckStatus{}
var details string
var checkID, checkCanaryID string
var Push = &cobra.Command{
Use: "push <servers>",
Short: "Push a check to multiple canary-checker servers",
Run: func(cmd *cobra.Command, servers []string) {
check.ID, _ = uuid.Parse(checkID)
check.CanaryID, _ = uuid.Parse(checkCanaryID)
if details != "" && files.Exists(details) {
detailsContent, err := os.ReadFile(details)
if err != nil {
logger.Fatalf("Failed to read check details: %s", err)
}
status.Detail = string(detailsContent)
}
status.Time = time.Now().UTC().Format(time.RFC3339)
for _, server := range servers {
data := api.QueueData{
Check: check,
Status: status,
}
jsonData, err := json.Marshal(data)
if err != nil {
logger.Errorf("error unmarshalling request body: %v", err)
}
err = api.PostDataToServer(server, bytes.NewBuffer(jsonData))
if err != nil {
logger.Errorf("error pushing data to server: %v", err)
}
}
},
}
Push.Flags().StringVar(&checkID, "id", "", "UUID of check")
Push.Flags().StringVar(&checkCanaryID, "parent-id", "", "UUID of parent canary")
Push.Flags().StringVarP(&check.Name, "name", "n", "", "Name of check")
Push.Flags().StringVarP(&check.Type, "type", "t", "", "Type of check")
Push.Flags().StringVarP(&check.Description, "description", "d", "", "Description of check")
Push.Flags().StringVar(&check.Namespace, "namespace", "default", "Name of canary")
Push.Flags().StringVar(&status.Error, "error", "", "Error of check")
Push.Flags().StringVar(&status.Message, "message", "", "Message of check")
Push.Flags().StringVar(&details, "detail", "", "Detail of check")
Push.Flags().IntVar(&status.Duration, "duration", 0, "Duration of check in milliseconds")
// Push.Flags().StringVar(&status.Time, "time", "", "Time of check")
Push.Flags().BoolVar(&status.Status, "passed", true, "Passed status of check")
Root.AddCommand(Push)
}