Skip to content

Commit b931d8f

Browse files
authored
Remove usage of os.Exit (#175)
1 parent c4d4a8c commit b931d8f

File tree

7 files changed

+136
-91
lines changed

7 files changed

+136
-91
lines changed

branchrestrictions.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package bitbucket
22

33
import (
44
"encoding/json"
5-
"os"
65

7-
"github.com/k0kubun/pp"
86
"github.com/mitchellh/mapstructure"
97
)
108

@@ -23,7 +21,10 @@ func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, e
2321
}
2422

2523
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
26-
data := b.buildBranchRestrictionsBody(bo)
24+
data, err := b.buildBranchRestrictionsBody(bo)
25+
if err != nil {
26+
return nil, err
27+
}
2728
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
2829
response, err := b.c.execute("POST", urlStr, data)
2930
if err != nil {
@@ -44,7 +45,10 @@ func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (*BranchRestrict
4445
}
4546

4647
func (b *BranchRestrictions) Update(bo *BranchRestrictionsOptions) (interface{}, error) {
47-
data := b.buildBranchRestrictionsBody(bo)
48+
data, err := b.buildBranchRestrictionsBody(bo)
49+
if err != nil {
50+
return nil, err
51+
}
4852
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
4953
response, err := b.c.execute("PUT", urlStr, data)
5054
if err != nil {
@@ -115,8 +119,7 @@ type branchRestrictionsBodyUser struct {
115119
} `json:"links"`
116120
}
117121

118-
func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsOptions) string {
119-
122+
func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsOptions) (string, error) {
120123
var users []branchRestrictionsBodyUser
121124
var groups []branchRestrictionsBodyGroup
122125
for _, u := range bo.Users {
@@ -142,11 +145,10 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO
142145

143146
data, err := json.Marshal(body)
144147
if err != nil {
145-
pp.Println(err)
146-
os.Exit(9)
148+
return "", err
147149
}
148150

149-
return string(data)
151+
return string(data), nil
150152
}
151153

152154
func decodeBranchRestriction(branchResponse interface{}) (*BranchRestrictions, error) {

deploykeys.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package bitbucket
22

33
import (
44
"encoding/json"
5-
"os"
65

7-
"github.com/k0kubun/pp"
86
"github.com/mitchellh/mapstructure"
97
)
108

@@ -35,22 +33,24 @@ func decodeDeployKey(response interface{}) (*DeployKey, error) {
3533
return deployKey, nil
3634
}
3735

38-
func buildDeployKeysBody(opt *DeployKeyOptions) string {
36+
func buildDeployKeysBody(opt *DeployKeyOptions) (string, error) {
3937
body := map[string]interface{}{}
4038
body["label"] = opt.Label
4139
body["key"] = opt.Key
4240

4341
data, err := json.Marshal(body)
4442
if err != nil {
45-
_, _ = pp.Println(err)
46-
os.Exit(9)
43+
return "", err
4744
}
4845

49-
return string(data)
46+
return string(data), nil
5047
}
5148

5249
func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
53-
data := buildDeployKeysBody(opt)
50+
data, err := buildDeployKeysBody(opt)
51+
if err != nil {
52+
return nil, err
53+
}
5454
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
5555
response, err := dk.c.execute("POST", urlStr, data)
5656
if err != nil {

issues.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/url"
7-
"os"
87
"strings"
9-
10-
"github.com/k0kubun/pp"
118
)
129

1310
type Issues struct {
@@ -54,13 +51,19 @@ func (p *Issues) Delete(io *IssuesOptions) (interface{}, error) {
5451
}
5552

5653
func (p *Issues) Update(io *IssuesOptions) (interface{}, error) {
57-
data := p.buildIssueBody(io)
54+
data, err := p.buildIssueBody(io)
55+
if err != nil {
56+
return nil, err
57+
}
5858
urlStr := p.c.requestUrl("/repositories/%s/%s/issues/%s", io.Owner, io.RepoSlug, io.ID)
5959
return p.c.execute("PUT", urlStr, data)
6060
}
6161

6262
func (p *Issues) Create(io *IssuesOptions) (interface{}, error) {
63-
data := p.buildIssueBody(io)
63+
data, err := p.buildIssueBody(io)
64+
if err != nil {
65+
return nil, err
66+
}
6467
urlStr := p.c.requestUrl("/repositories/%s/%s/issues", io.Owner, io.RepoSlug)
6568
return p.c.execute("POST", urlStr, data)
6669
}
@@ -109,7 +112,7 @@ func (p *Issues) DeleteWatch(io *IssuesOptions) error {
109112
return err
110113
}
111114

112-
func (p *Issues) buildIssueBody(io *IssuesOptions) string {
115+
func (p *Issues) buildIssueBody(io *IssuesOptions) (string, error) {
113116
body := map[string]interface{}{}
114117

115118
// This feld is required
@@ -160,11 +163,10 @@ func (p *Issues) buildIssueBody(io *IssuesOptions) string {
160163

161164
data, err := json.Marshal(body)
162165
if err != nil {
163-
pp.Println(err)
164-
os.Exit(9)
166+
return "", err
165167
}
166168

167-
return string(data)
169+
return string(data), nil
168170
}
169171

170172
func (p *Issues) GetComments(ico *IssueCommentsOptions) (interface{}, error) {

project.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package bitbucket
22

33
import (
44
"encoding/json"
5-
"os"
65

7-
"github.com/k0kubun/pp"
86
"github.com/mitchellh/mapstructure"
97
)
108

@@ -38,7 +36,10 @@ func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
3836
}
3937

4038
func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
41-
data := t.buildProjectBody(opt)
39+
data, err := t.buildProjectBody(opt)
40+
if err != nil {
41+
return nil, err
42+
}
4243
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
4344
response, err := t.c.execute("POST", urlStr, data)
4445
if err != nil {
@@ -54,7 +55,10 @@ func (t *Workspace) DeleteProject(opt *ProjectOptions) (interface{}, error) {
5455
}
5556

5657
func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
57-
data := t.buildProjectBody(opt)
58+
data, err := t.buildProjectBody(opt)
59+
if err != nil {
60+
return nil, err
61+
}
5862
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
5963
response, err := t.c.execute("PUT", urlStr, data)
6064
if err != nil {
@@ -64,17 +68,16 @@ func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
6468
return decodeProject(response)
6569
}
6670

67-
func (t *Workspace) buildJsonBody(body map[string]interface{}) string {
71+
func (t *Workspace) buildJsonBody(body map[string]interface{}) (string, error) {
6872
data, err := json.Marshal(body)
6973
if err != nil {
70-
pp.Println(err)
71-
os.Exit(9)
74+
return "", err
7275
}
7376

74-
return string(data)
77+
return string(data), nil
7578
}
7679

77-
func (t *Workspace) buildProjectBody(opts *ProjectOptions) string {
80+
func (t *Workspace) buildProjectBody(opts *ProjectOptions) (string, error) {
7881
body := map[string]interface{}{}
7982

8083
if opts.Description != "" {

pullrequests.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ package bitbucket
33
import (
44
"encoding/json"
55
"net/url"
6-
"os"
7-
8-
"github.com/k0kubun/pp"
96
)
107

118
type PullRequests struct {
129
c *Client
1310
}
1411

1512
func (p *PullRequests) Create(po *PullRequestsOptions) (interface{}, error) {
16-
data := p.buildPullRequestBody(po)
13+
data, err := p.buildPullRequestBody(po)
14+
if err != nil {
15+
return nil, err
16+
}
1717
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/", po.Owner, po.RepoSlug)
1818
return p.c.execute("POST", urlStr, data)
1919
}
2020

2121
func (p *PullRequests) Update(po *PullRequestsOptions) (interface{}, error) {
22-
data := p.buildPullRequestBody(po)
22+
data, err := p.buildPullRequestBody(po)
23+
if err != nil {
24+
return nil, err
25+
}
2326
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID
2427
return p.c.execute("PUT", urlStr, data)
2528
}
@@ -96,13 +99,19 @@ func (p *PullRequests) Diff(po *PullRequestsOptions) (interface{}, error) {
9699
}
97100

98101
func (p *PullRequests) Merge(po *PullRequestsOptions) (interface{}, error) {
99-
data := p.buildPullRequestBody(po)
102+
data, err := p.buildPullRequestBody(po)
103+
if err != nil {
104+
return nil, err
105+
}
100106
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/merge"
101107
return p.c.execute("POST", urlStr, data)
102108
}
103109

104110
func (p *PullRequests) Decline(po *PullRequestsOptions) (interface{}, error) {
105-
data := p.buildPullRequestBody(po)
111+
data, err := p.buildPullRequestBody(po)
112+
if err != nil {
113+
return nil, err
114+
}
106115
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/decline"
107116
return p.c.execute("POST", urlStr, data)
108117
}
@@ -173,8 +182,7 @@ func (p *PullRequests) Statuses(po *PullRequestsOptions) (interface{}, error) {
173182
return p.c.execute("GET", urlStr, "")
174183
}
175184

176-
func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) string {
177-
185+
func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) (string, error) {
178186
body := map[string]interface{}{}
179187
body["source"] = map[string]interface{}{}
180188
body["destination"] = map[string]interface{}{}
@@ -225,11 +233,10 @@ func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) string {
225233

226234
data, err := json.Marshal(body)
227235
if err != nil {
228-
pp.Println(err)
229-
os.Exit(9)
236+
return "", err
230237
}
231238

232-
return string(data)
239+
return string(data), nil
233240
}
234241

235242
func (p *PullRequests) buildPullRequestCommentBody(co *PullRequestCommentOptions) (string, error) {

0 commit comments

Comments
 (0)