-
Notifications
You must be signed in to change notification settings - Fork 11
/
add.go
63 lines (58 loc) · 1.8 KB
/
add.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
type AddCommand struct {
StartTime int64 `short:"s" long:"start_time" description:"Start time for the marker in unix time (seconds since the epoch)"`
EndTime int64 `short:"e" long:"end_time" hidden:"true" description:"End time for the marker in unix time (seconds since the epoch)"`
Message string `short:"m" long:"msg" description:"Message describing this specific marker"`
URL string `short:"u" long:"url" description:"URL associated with this marker"`
Type string `short:"t" long:"type" description:"Identifies marker type"`
}
func (a *AddCommand) Execute(args []string) error {
checkRequiredFlags()
blob, err := json.Marshal(marker{
StartTime: a.StartTime,
EndTime: a.EndTime,
Message: a.Message,
Type: a.Type,
URL: a.URL,
})
if err != nil {
return err
}
postURL, err := url.Parse(options.APIHost)
if err != nil {
errMsg := fmt.Sprintf("Failed to parse URL %s", options.APIHost)
return errors.New(errMsg)
}
postURL.Path = "/1/markers/" + options.Dataset
req, _ := http.NewRequest("POST", postURL.String(), bytes.NewBuffer(blob))
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("X-Honeycomb-Team", options.WriteKey)
if options.AuthorizationHeader != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", options.AuthorizationHeader))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
errMsg := fmt.Sprintf("Failed with %d and message: %s", resp.StatusCode, body)
return errors.New(errMsg)
}
fmt.Println(string(body))
return nil
}