-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (124 loc) · 4.18 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
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
// go-sentry-cli
//
// Simple application for creating http request to Sentry API over library
// go-sentry-api (more here: https://github.com/atlassian/go-sentry-api).
// Supported easy functional for check connection, create organization / project,
// print client keys of each project in the organization.
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/atlassian/go-sentry-api"
)
var (
InfoLogger *log.Logger
ErrorLogger *log.Logger
debug bool
)
// Get info about organization if exists and return true, organization pointer
// or false, nil
func getOrganization(sentryClient *sentry.Client, orgSLug string) (bool, *sentry.Organization) {
logger("info", fmt.Sprintf("Checking the organization <%s> for existence", orgSLug))
org, err := sentryClient.GetOrganization(orgSLug)
if err == nil {
return true, &org
}
logger("error", fmt.Sprintf("Get organization <%s> info failed: %s", orgSLug, err))
return false, nil
}
// Create organization and return true, organization pointer if success
// else false, nil
func createOrganization(sentryClient *sentry.Client, orgSlug string) (bool, *sentry.Organization) {
logger("info", fmt.Sprintf("Creating a non-existent organization <%s>", orgSlug))
org, err := sentryClient.CreateOrganization(orgSlug)
if err == nil {
return true, &org
}
logger("error", fmt.Sprintf("Creating organization <%s> failed: %s", orgSlug, err))
return false, nil
}
func getProject(sentryClient *sentry.Client, org *sentry.Organization, projSlug string) (bool, *sentry.Project) {
logger("info", fmt.Sprintf("Checking the existence of the project <%s> in the organization <%s> (id: %d)", projSlug, org.Name, org.ID))
proj, err := sentryClient.GetProject(*org, projSlug)
if err == nil {
return true, &proj
}
logger("error", fmt.Sprintf("Get <%s> project info from organization <%s> failed: %s", projSlug, *org.Slug, err))
return false, nil
}
func createProject(sentryClient *sentry.Client, org *sentry.Organization, projSlug string) (bool, *sentry.Project) {
logger("info", fmt.Sprintf("Creating a non-existent project <%s>", projSlug))
team := sentry.Team{
Slug: org.Slug,
Name: org.Name,
}
proj, err := sentryClient.CreateProject(*org, team, projSlug, &projSlug)
if err == nil {
return true, &proj
}
logger("error", fmt.Sprintf("Create <%s> project in organization <%s> failed: %s", projSlug, *org.Slug, err))
return false, nil
}
func logger(level string, msg string) {
if debug {
switch level {
case "error":
ErrorLogger.Println(msg)
case "info":
InfoLogger.Println(msg)
}
}
}
func init() {
InfoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime)
ErrorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime)
}
func main() {
var (
host string
apiToken string
timeout int
endpoint string
tail []string
)
flag.BoolVar(&debug, "debug", false, "Enable debug mode")
flag.StringVar(&host, "host", "", "* Host of your Sentry instance including the protocol \n" +
" Example: https://sentry.io")
flag.StringVar(&apiToken, "token", "", "* Personal access token generated by your Sentry instance")
flag.IntVar(&timeout, "timeout", 10, "Connection timeout during a HTTP request")
flag.Parse()
if host == "" || apiToken == "" {
flag.PrintDefaults()
os.Exit(1)
}
endpoint = fmt.Sprintf("%s/api/0/", host)
sentryClient, _ := sentry.NewClient(apiToken, &endpoint, &timeout)
logger("info", "Check connection with provided parameters")
if _, _, err := sentryClient.GetOrganizations(); err != nil {
logger("error", fmt.Sprintf("Connection failed: %s", err))
os.Exit(2)
}
logger("info", "Connection success!")
if len(flag.Args()) < 2 {
logger("info", "No more parameters were passed. No more actions are required")
os.Exit(0)
}
tail = flag.Args()
orgSlug := strings.ToLower(tail[0])
projSlug := strings.ToLower(tail[1])
eO, org := getOrganization(sentryClient, orgSlug)
if !eO {
_, org = createOrganization(sentryClient, orgSlug)
}
eP, proj := getProject(sentryClient, org, projSlug)
if !eP {
_, proj = createProject(sentryClient, org, projSlug)
}
clientKeys, _ := sentryClient.GetClientKeys(*org, *proj)
for _, key := range clientKeys {
fmt.Println(key.DSN.Public)
}
}