-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (71 loc) · 1.62 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/cloudfoundry/cli/plugin"
)
type mapevent struct{}
const eventUrl = "http://hottopic.apps.bogata.cf-app.com/map"
type mapEventPost struct {
App string `json:"app"`
Topic string `json:"topic"`
}
func (m *mapevent) Run(cliConnection plugin.CliConnection, args []string) {
if len(args) > 0 && args[0] == "CLI-MESSAGE-UNINSTALL" {
return
}
if len(args) != 3 {
fmt.Println("Must supply APP_NAME and TOPIC_NAME")
os.Exit(1)
}
appName := args[1]
topicName := args[2]
postBody := mapEventPost{App: appName, Topic: topicName}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(postBody)
if err != nil {
fmt.Printf("error: %v", err)
os.Exit(1)
}
res, err := http.Post(eventUrl, "application/json", b)
if err != nil {
fmt.Printf("error: %v", err)
os.Exit(1)
}
if res.StatusCode == http.StatusCreated {
fmt.Printf("Successfully mapped event topic %s to app %s\n", topicName, appName)
} else {
fmt.Println("this is hackday project, this shouldnt have happened")
}
}
func (m *mapevent) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "map-event",
Version: plugin.VersionType{
Major: 0,
Minor: 0,
Build: 1,
},
MinCliVersion: plugin.VersionType{
Major: 0,
Minor: 0,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "map-event",
HelpText: "map an event topic to an app",
UsageDetails: plugin.Usage{
Usage: "cf map-event APP_NAME TOPIC_NAME",
Options: map[string]string{},
},
},
},
}
}
func main() {
plugin.Start(new(mapevent))
}