Skip to content

Commit 58c88f6

Browse files
author
Tobias Meinhardt
committed
Outsource current branch detection
1 parent 4a927fc commit 58c88f6

File tree

4 files changed

+18
-34
lines changed

4 files changed

+18
-34
lines changed

pkg/cli/cmd/close.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"github.com/meinto/glow"
54
l "github.com/meinto/glow/logging"
65
"github.com/meinto/glow/pkg/cli/cmd/internal/command"
76
"github.com/meinto/glow/pkg/cli/cmd/internal/util"
@@ -28,16 +27,7 @@ func SetupCloseCommand(parent command.Service) command.Service {
2827
Short: "close a branch",
2928
},
3029
Run: func(cmd command.Service, args []string) {
31-
var currentBranch glow.Branch
32-
if RootCmdOptions.CI {
33-
cb, err := cmd.GitProvider().GetCIBranch()
34-
util.ExitOnError(err)
35-
currentBranch = cb
36-
} else {
37-
cb, _, _, err := cmd.GitClient().CurrentBranch()
38-
util.ExitOnError(err)
39-
currentBranch = cb
40-
}
30+
currentBranch := cmd.CurrentBranch(RootCmdOptions.CI)
4131

4232
err := cmd.GitProvider().Close(currentBranch)
4333
if !util.MergeRequestFlags.Gracefully {

pkg/cli/cmd/internal/command/command.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"reflect"
55

6+
"github.com/meinto/glow"
67
"github.com/meinto/glow/git"
78
"github.com/meinto/glow/gitprovider"
89
"github.com/meinto/glow/pkg"
@@ -29,6 +30,8 @@ type Service interface {
2930

3031
Patch() Service
3132
PatchRun(fieldName string, run func(cmd Service, args []string))
33+
34+
CurrentBranch(ci bool) glow.Branch
3235
}
3336

3437
func Setup(cmd Service, parent Service) Service {
@@ -134,3 +137,15 @@ func (c *Command) PatchRun(fieldName string, run func(cmd Service, args []string
134137
func (c *Command) Add(cmd Service) {
135138
c.Command.AddCommand(cmd.Cmd())
136139
}
140+
141+
func (c *Command) CurrentBranch(ci bool) glow.Branch {
142+
if ci {
143+
cb, err := c.GitProvider().GetCIBranch()
144+
util.ExitOnError(err)
145+
return cb
146+
} else {
147+
cb, _, _, err := c.GitClient().CurrentBranch()
148+
util.ExitOnError(err)
149+
return cb
150+
}
151+
}

pkg/cli/cmd/publish.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"github.com/meinto/glow"
54
l "github.com/meinto/glow/logging"
65
"github.com/meinto/glow/pkg/cli/cmd/internal/command"
76
"github.com/meinto/glow/pkg/cli/cmd/internal/util"
@@ -28,16 +27,7 @@ func SetupPublishCommand(parent command.Service) command.Service {
2827
Short: "publish a release branch",
2928
},
3029
Run: func(cmd command.Service, args []string) {
31-
var currentBranch glow.Branch
32-
if RootCmdOptions.CI {
33-
cb, err := cmd.GitProvider().GetCIBranch()
34-
util.ExitOnError(err)
35-
currentBranch = cb
36-
} else {
37-
cb, _, _, err := cmd.GitClient().CurrentBranch()
38-
util.ExitOnError(err)
39-
currentBranch = cb
40-
}
30+
currentBranch := cmd.CurrentBranch(RootCmdOptions.CI)
4131

4232
err := cmd.GitProvider().Publish(currentBranch)
4333
if !util.MergeRequestFlags.Gracefully {

pkg/cli/cmd/push.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"github.com/meinto/glow"
54
"github.com/meinto/glow/pkg/cli/cmd/internal/command"
65
"github.com/meinto/glow/pkg/cli/cmd/internal/util"
76
"github.com/spf13/cobra"
@@ -34,17 +33,7 @@ func SetupPushCommand(parent command.Service) command.Service {
3433
Short: "push changes",
3534
},
3635
Run: func(cmd command.Service, args []string) {
37-
38-
var currentBranch glow.Branch
39-
if RootCmdOptions.CI {
40-
cb, err := cmd.GitProvider().GetCIBranch()
41-
util.ExitOnError(err)
42-
currentBranch = cb
43-
} else {
44-
cb, _, _, err := cmd.GitClient().CurrentBranch()
45-
util.ExitOnError(err)
46-
currentBranch = cb
47-
}
36+
currentBranch := cmd.CurrentBranch(RootCmdOptions.CI)
4837

4938
if pushCmdOptions.AddAll {
5039
util.ExitOnError(cmd.GitClient().AddAll())

0 commit comments

Comments
 (0)