Skip to content

Commit b1e6944

Browse files
committed
updated gitApi to simplify repoDo()
1 parent afb6d15 commit b1e6944

21 files changed

+85
-134
lines changed

cmd/repo.go

Lines changed: 19 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ THE SOFTWARE.
2222
package cmd
2323

2424
import (
25-
"encoding/json"
2625
"sync"
2726

2827
"github.com/J-Siu/go-gitapi"
@@ -32,12 +31,6 @@ import (
3231
"github.com/spf13/cobra"
3332
)
3433

35-
type ErrMsg struct {
36-
Errors string `json:"errors"`
37-
Message string `json:"message"`
38-
Url string `json:"url"`
39-
}
40-
4134
// repoCmd represents the repo command
4235
var repoCmd = &cobra.Command{
4336
Use: "repository",
@@ -49,91 +42,36 @@ func init() {
4942
rootCmd.AddCommand(repoCmd)
5043
}
5144

52-
func repoDelFunc(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
45+
func repoDo(gitApi *gitapi.GitApi, wg *sync.WaitGroup, statusOnly bool) {
5346
if wg != nil {
5447
defer wg.Done()
5548
}
56-
gitApi.Del()
5749
var title string
5850
if !lib.Flag.NoTitle {
5951
title = gitApi.Repo + "(" + gitApi.Name + ")"
6052
}
61-
helper.ReportStatus(gitApi.Res.Ok(), title, true)
62-
}
63-
64-
func repoGetFunc(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
65-
if wg != nil {
66-
defer wg.Done()
67-
}
6853

69-
var title string
70-
if !lib.Flag.NoTitle {
71-
title = gitApi.Repo + "(" + gitApi.Name + ")"
72-
}
73-
74-
// var success bool = gitApi.Get().Res.Ok()
75-
// if success {
76-
if gitApi.Get().Res.Ok() {
77-
// API GET OK
78-
var singleLine bool
79-
switch *gitApi.Res.Output {
80-
case "true", "false", "public", "private":
81-
singleLine = true
82-
default:
83-
singleLine = false
84-
}
85-
helper.Report(gitApi.Res.Output, title, !lib.Flag.NoSkip, singleLine)
86-
} else {
87-
// API GET failed, try to extract error message
88-
var info ErrMsg
89-
err := json.Unmarshal([]byte(*gitApi.Res.Output), &info)
90-
if err == nil {
91-
helper.Report(info.Message, title, true, true)
54+
status := gitApi.Do().Ok()
55+
if status {
56+
if statusOnly {
57+
helper.ReportStatus(status, title, true)
9258
} else {
93-
helper.Report(gitApi.Res.Output, title, true, false)
59+
singleLine := false
60+
output := gitApi.Output()
61+
switch *output {
62+
case "true", "false", "public", "private":
63+
singleLine = true
64+
default:
65+
singleLine = false
66+
}
67+
helper.Report(output, title, !lib.Flag.NoSkip, singleLine)
9468
}
69+
} else {
70+
// API or HTTP GET failed, try to extract error message
71+
helper.Report(gitApi.Err(), title, true, true)
9572
}
9673
}
9774

98-
func repoPatchFunc(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
99-
if wg != nil {
100-
defer wg.Done()
101-
}
102-
103-
var title string
104-
if !lib.Flag.NoTitle {
105-
title = gitApi.Repo + "(" + gitApi.Name + ")"
106-
}
107-
108-
helper.ReportStatus(gitApi.Patch().Res.Ok(), title, true)
109-
}
110-
111-
func repoPostFunc(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
112-
if wg != nil {
113-
defer wg.Done()
114-
}
115-
116-
var title string
117-
if !lib.Flag.NoTitle {
118-
title = gitApi.Repo + "(" + gitApi.Name + ")"
119-
}
120-
121-
helper.ReportStatus(gitApi.Post().Res.Ok(), title, true)
122-
}
123-
124-
func repoPutFunc(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
125-
if wg != nil {
126-
defer wg.Done()
127-
}
128-
129-
var title string
130-
if !lib.Flag.NoTitle {
131-
title = gitApi.Repo + "(" + gitApi.Name + ")"
132-
}
133-
134-
helper.ReportStatus(gitApi.Put().Res.Ok(), title, true)
135-
}
136-
13775
func repoUnarchiveGithub(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
13876
if wg != nil {
13977
defer wg.Done()
@@ -150,15 +88,8 @@ func repoUnarchiveGithub(gitApi *gitapi.GitApi, wg *sync.WaitGroup) {
15088
gitApi.Info = &info
15189
// Cannot use repoGetFunc(gitApi, nil), as we don't want print out here
15290
gitApi.Get()
153-
if !gitApi.Res.Ok() {
154-
// API GET failed, try to extract error message
155-
var errMsg ErrMsg
156-
err := json.Unmarshal([]byte(*gitApi.Res.Output), &errMsg)
157-
if err == nil {
158-
helper.Report(errMsg.Message, title, true, true)
159-
} else {
160-
helper.Report(gitApi.Res.Output, title, true, false)
161-
}
91+
if !gitApi.Ok() {
92+
helper.Report(gitApi.Err(), title, true, true)
16293
return
16394
}
16495

cmd/repoDelRepo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ var repoDelRepoCmd = &cobra.Command{
4848
wg.Add(1)
4949
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, gitapi.Nil())
5050
gitApi.EndpointRepos()
51+
gitApi.SetDel()
5152
if lib.Flag.NoParallel {
52-
repoDelFunc(gitApi, &wg)
53+
repoDo(gitApi, &wg, true)
5354
} else {
54-
go repoDelFunc(gitApi, &wg)
55+
go repoDo(gitApi, &wg, true)
5556
}
5657
}
5758
}

cmd/repoDelSecret.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ var repoDelSecretCmd = &cobra.Command{
5959
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, gitapi.Nil())
6060
gitApi.EndpointReposSecrets()
6161
gitApi.Req.Endpoint = path.Join(gitApi.Req.Endpoint, secret)
62+
gitApi.SetDel()
6263
if lib.Flag.NoParallel {
63-
repoDelFunc(gitApi, &wg)
64+
repoDo(gitApi, &wg, true)
6465
} else {
65-
go repoDelFunc(gitApi, &wg)
66+
go repoDo(gitApi, &wg, true)
6667
}
6768
}
6869
}

cmd/repoGetArchived.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var repoGetArchivedCmd = &cobra.Command{
4646
var info RepoArchived
4747
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
4848
gitApi.EndpointRepos()
49+
gitApi.SetGet()
4950
if lib.Flag.NoParallel {
50-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5152
} else {
52-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5354
}
5455
}
5556
}

cmd/repoGetDescription.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var repoGetDescriptionCmd = &cobra.Command{
4646
var info gitapi.RepoDescription
4747
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
4848
gitApi.EndpointRepos()
49+
gitApi.SetGet()
4950
if lib.Flag.NoParallel {
50-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5152
} else {
52-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5354
}
5455
}
5556
}

cmd/repoGetInfo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ var repoGetInfoCmd = &cobra.Command{
4545
wg.Add(1)
4646
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, gitapi.Nil())
4747
gitApi.EndpointRepos()
48+
gitApi.SetGet()
4849
if lib.Flag.NoParallel {
49-
repoGetFunc(gitApi, &wg)
50+
repoDo(gitApi, &wg, false)
5051
} else {
51-
go repoGetFunc(gitApi, &wg)
52+
go repoDo(gitApi, &wg, false)
5253
}
5354
}
5455
}

cmd/repoGetPrivate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var repoGetPrivateCmd = &cobra.Command{
4646
var info gitapi.RepoPrivate
4747
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
4848
gitApi.EndpointRepos()
49+
gitApi.SetGet()
4950
if lib.Flag.NoParallel {
50-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5152
} else {
52-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5354
}
5455
}
5556
}

cmd/repoGetPublickey.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,16 @@ var repoGetPublickeyCmd = &cobra.Command{
4242
}
4343
for _, workPath := range args {
4444
for _, remote := range lib.Conf.MergedRemotes {
45-
// if remote.Vendor != gitapi.Vendor_Github {
46-
// fmt.Printf("%s:(%s) action secret not supported.\n", remote.Name, remote.Vendor)
47-
// } else {
4845
wg.Add(1)
4946
var info gitapi.RepoPublicKey
5047
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
5148
gitApi.EndpointReposSecretsPubkey()
49+
gitApi.SetGet()
5250
if lib.Flag.NoParallel {
53-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5452
} else {
55-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5654
}
57-
// }
5855
}
5956
wg.Wait()
6057
}

cmd/repoGetTopic.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var repoGetTopicCmd = &cobra.Command{
4646
wg.Add(1)
4747
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
4848
gitApi.EndpointReposTopics()
49+
gitApi.SetGet()
4950
if lib.Flag.NoParallel {
50-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5152
} else {
52-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5354
}
5455
}
5556
}

cmd/repoGetVisibility.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var repoGetVisibilityCmd = &cobra.Command{
4646
var info gitapi.RepoVisibility
4747
var gitApi *gitapi.GitApi = remote.GetGitApi(&workPath, &info)
4848
gitApi.EndpointRepos()
49+
gitApi.SetGet()
4950
if lib.Flag.NoParallel {
50-
repoGetFunc(gitApi, &wg)
51+
repoDo(gitApi, &wg, false)
5152
} else {
52-
go repoGetFunc(gitApi, &wg)
53+
go repoDo(gitApi, &wg, false)
5354
}
5455
}
5556
}

0 commit comments

Comments
 (0)