-
Notifications
You must be signed in to change notification settings - Fork 20
/
cf_deployment_tracker.go
82 lines (67 loc) · 1.63 KB
/
cf_deployment_tracker.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
package cf_deployment_tracker
import (
"encoding/json"
"fmt"
"github.com/parnurzeal/gorequest"
"io/ioutil"
"os"
"time"
)
var deploymentTrackerURL = "https://deployment-tracker.mybluemix.net/api/v1/track"
type Repository struct {
Url string
}
type Package struct {
Name string
Version string
Repository Repository
}
type Event struct {
DateSent string `json:"date_sent"`
CodeVersion string `json:"code_version"`
RepositoryURL string `json:"repository_url"`
ApplicationName string `json:"application_name"`
SpaceID string `json:"space_id"`
ApplicationVersion string `json:"application_version"`
ApplicatonURIs []string `json:"application_uris"`
}
func Track() (errs []error) {
content, err := ioutil.ReadFile("package.json")
//exit early if we cant read the file
if err != nil {
return
}
var info Package
err = json.Unmarshal(content, &info)
//exit early if we can't parse the file
if err != nil {
return
}
vcapApplication := os.Getenv("VCAP_APPLICATION")
var event Event
err = json.Unmarshal([]byte(vcapApplication), &event)
//exit early if we are not running in CF
if err != nil {
return
}
if info.Repository.Url != "" {
event.RepositoryURL = info.Repository.Url
}
if info.Name != "" {
event.ApplicationName = info.Name
}
if info.Version != "" {
event.CodeVersion = info.Version
}
now := time.Now()
event.DateSent = now.UTC().Format("2006-01-02T15:04:05.999Z")
request := gorequest.New()
resp, _, errs := request.Post(deploymentTrackerURL).
Send(event).
End()
if errs != nil {
return errs
}
fmt.Println(resp.Status)
return nil
}