Skip to content

Commit 2ee0a11

Browse files
committed
new_branch: change the command pattern and refactor flag assigment
1 parent bf4a710 commit 2ee0a11

4 files changed

Lines changed: 22 additions & 34 deletions

File tree

cmd/flags_assigners.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//
1010
// The above copyright notice and this permission notice shall be included in
1111
// all copies or substantial portions of the Software.
12-
//
1312
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1413
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1514
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -628,13 +627,3 @@ func assignAddSSHKeyOptions(cmd *cobra.Command) (*gitlab.AddSSHKeyOptions, error
628627
opts.Key = gitlab.String(string(b))
629628
return opts, nil
630629
}
631-
632-
// assignCreateBranchOptions assigns the flags' values to gitlab.AddSSHKeyOptions fields.
633-
// If a flag's default value is not changed by the caller,
634-
// it's value will not be assigned to the associated gitlab.AddSSHKeyOptions field.
635-
func assignCreateBranchOptions(cmd *cobra.Command) *gitlab.CreateBranchOptions {
636-
opts := new(gitlab.CreateBranchOptions)
637-
opts.Branch = gitlab.String(getFlagString(cmd, "name"))
638-
opts.Ref = gitlab.String(getFlagString(cmd, "ref"))
639-
return opts
640-
}

cmd/flags_definers.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,8 @@ func addGetProjectsFlags(cmd *cobra.Command) {
129129
"Limit by enabled merge requests feature")
130130
}
131131

132-
func addNewBranchFlags(cmd *cobra.Command) {
133-
addBranchName(cmd)
134-
cmd.Flags().StringP("ref", "r", "",
135-
"The branch name or commit SHA to create branch from")
136-
verifyMarkFlagRequired(cmd, "ref")
137-
}
138-
139-
func addBranchName(cmd *cobra.Command) {
140-
cmd.Flags().StringP("name", "n", "", "The name of the branch")
141-
verifyMarkFlagRequired(cmd, "name")
132+
func addProjectFlag(cmd *cobra.Command) {
133+
cmd.Flags().StringP("project", "p", "", "The name or ID of the project")
142134
}
143135

144136
// addNewGroupFlags add the required flags for creating a new group

cmd/new_branch.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
)
2626

2727
var newBranchCmd = &cobra.Command{
28-
Use: "branch",
29-
Aliases: []string{"b"},
30-
Short: "Create a new branch for a specified project",
31-
Example: `gitlabctl new branch project23 --name=release-x --ref=master`,
28+
Use: "branch",
29+
Aliases: []string{"b"},
30+
Short: "Create a new branch for a specified project",
31+
Example: `# create a develop branch from master branch for project groupx/myapp
32+
gitlabctl new branch develop --project=groupx/myapp --ref=master`,
3233
SilenceErrors: true,
3334
SilenceUsage: true,
3435
Args: cobra.ExactArgs(1),
@@ -39,16 +40,22 @@ var newBranchCmd = &cobra.Command{
3940

4041
func init() {
4142
newCmd.AddCommand(newBranchCmd)
42-
addNewBranchFlags(newBranchCmd)
43+
addProjectFlag(newBranchCmd)
44+
verifyMarkFlagRequired(newBranchCmd, "project")
45+
newBranchCmd.Flags().StringP("ref", "r", "",
46+
"The branch name or commit SHA to create branch from")
47+
verifyMarkFlagRequired(newBranchCmd, "ref")
4348
}
4449

45-
func runNewBranch(cmd *cobra.Command, project string) error {
46-
opts := assignCreateBranchOptions(cmd)
47-
branch, err := newBranch(project, opts)
50+
func runNewBranch(cmd *cobra.Command, branch string) error {
51+
opts := new(gitlab.CreateBranchOptions)
52+
opts.Ref = gitlab.String(getFlagString(cmd, "ref"))
53+
opts.Branch = gitlab.String(branch)
54+
createdBranch, err := newBranch(getFlagString(cmd, "project"), opts)
4855
if err != nil {
4956
return err
5057
}
51-
printBranchOut(cmd, branch)
58+
printBranchOut(cmd, createdBranch)
5259
return nil
5360
}
5461

cmd/new_branch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func TestNewBranch(t *testing.T) {
3737
{
3838
name: "create a new branch",
3939
flagsMap: map[string]string{
40-
"name": "release-1",
41-
"ref": "master",
40+
"project": "Group2/project12",
41+
"ref": "master",
4242
},
43-
args: []string{"Group2/project12"},
43+
args: []string{"release-x"},
4444
expect: pass,
4545
},
4646
{
4747
name: "flags must be required",
48-
args: []string{"Group2/project12"},
48+
args: []string{"master"},
4949
expect: fail,
5050
},
5151
}

0 commit comments

Comments
 (0)