Skip to content

Commit 658c0ef

Browse files
author
Raphaël Benitte
committed
Add related commands to CLI output
1 parent 39e5bcc commit 658c0ef

File tree

10 files changed

+138
-10
lines changed

10 files changed

+138
-10
lines changed

cli/cmd/add_project_environment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ var addProjectEnvironmentCmd = &cobra.Command{
5555

5656
printMeta(meta, false)
5757

58+
relatedCommands([]*relatedCommand{
59+
newRelatedCommand(lsProjectEnvironmentsCmd, map[string]string{
60+
"project_id": ids["project_id"],
61+
}),
62+
})
63+
5864
return nil
5965
},
6066
}

cli/cmd/add_project_hook.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
out "github.com/plouc/go-gitlab-client/cli/output"
77
"github.com/plouc/go-gitlab-client/gitlab"
88
"github.com/spf13/cobra"
9+
"strconv"
910
)
1011

1112
func init() {
@@ -166,6 +167,16 @@ var addProjectHookCmd = &cobra.Command{
166167

167168
printMeta(meta, false)
168169

170+
relatedCommands([]*relatedCommand{
171+
newRelatedCommand(lsProjectHooksCmd, map[string]string{
172+
"project_id": ids["project_id"],
173+
}),
174+
newRelatedCommand(getProjectHookCmd, map[string]string{
175+
"project_id": ids["project_id"],
176+
"hook_id": strconv.Itoa(createdHook.Id),
177+
}),
178+
})
179+
169180
return nil
170181
},
171182
}

cli/cmd/get_group.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ var getGroupCmd = &cobra.Command{
3737

3838
printMeta(meta, false)
3939

40+
relatedCommands([]*relatedCommand{
41+
newRelatedCommand(lsGroupMergeRequestsCmd, map[string]string{
42+
"group_id": ids["group_id"],
43+
}),
44+
newRelatedCommand(lsGroupVariablesCmd, map[string]string{
45+
"group_id": ids["group_id"],
46+
}),
47+
})
48+
4049
return nil
4150
},
4251
}

cli/cmd/get_project.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,45 @@ var getProjectCmd = &cobra.Command{
3737

3838
printMeta(meta, false)
3939

40+
relatedCommands([]*relatedCommand{
41+
newRelatedCommand(lsProjectBranchesCmd, map[string]string{
42+
"project_id": ids["project_id"],
43+
}),
44+
newRelatedCommand(addProjectBranchCmd, map[string]string{
45+
"project_id": ids["project_id"],
46+
}),
47+
newRelatedCommand(lsProjectHooksCmd, map[string]string{
48+
"project_id": ids["project_id"],
49+
}),
50+
newRelatedCommand(addProjectHookCmd, map[string]string{
51+
"project_id": ids["project_id"],
52+
}),
53+
newRelatedCommand(lsProjectMembersCmd, map[string]string{
54+
"project_id": ids["project_id"],
55+
}),
56+
newRelatedCommand(lsProjectPipelinesCmd, map[string]string{
57+
"project_id": ids["project_id"],
58+
}),
59+
newRelatedCommand(lsProjectJobsCmd, map[string]string{
60+
"project_id": ids["project_id"],
61+
}),
62+
newRelatedCommand(lsProjectMergeRequestsCmd, map[string]string{
63+
"project_id": ids["project_id"],
64+
}),
65+
newRelatedCommand(lsProjectEnvironmentsCmd, map[string]string{
66+
"project_id": ids["project_id"],
67+
}),
68+
newRelatedCommand(addProjectEnvironmentCmd, map[string]string{
69+
"project_id": ids["project_id"],
70+
}),
71+
newRelatedCommand(lsProjectVariablesCmd, map[string]string{
72+
"project_id": ids["project_id"],
73+
}),
74+
newRelatedCommand(addProjectBadgeCmd, map[string]string{
75+
"project_id": ids["project_id"],
76+
}),
77+
})
78+
4079
return nil
4180
},
4281
}

cli/cmd/get_user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ var getUserCmd = &cobra.Command{
3333

3434
printMeta(meta, false)
3535

36+
relatedCommands([]*relatedCommand{
37+
newRelatedCommand(lsUserSshKeysCmd, map[string]string{
38+
"user_id": ids["user_id"],
39+
}),
40+
})
41+
3642
return nil
3743
},
3844
}

cli/cmd/ls_group_vars.go renamed to cli/cmd/ls_group_variables.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
)
99

1010
func init() {
11-
lsCmd.AddCommand(lsGroupVarsCmd)
11+
lsCmd.AddCommand(lsGroupVariablesCmd)
1212
}
1313

14-
var lsGroupVarsCmd = &cobra.Command{
15-
Use: resourceCmd("group-vars", "group"),
16-
Aliases: []string{"gv"},
14+
var lsGroupVariablesCmd = &cobra.Command{
15+
Use: resourceCmd("group-variables", "group"),
16+
Aliases: []string{"group-vars", "gv"},
1717
Short: "Get list of a group's variables",
1818
RunE: func(cmd *cobra.Command, args []string) error {
1919
ids, err := config.aliasIdsOrArgs(currentAlias, "group", args)

cli/cmd/ls_project_vars.go renamed to cli/cmd/ls_project_variables.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
)
1111

1212
func init() {
13-
lsCmd.AddCommand(lsProjectVarsCmd)
13+
lsCmd.AddCommand(lsProjectVariablesCmd)
1414
}
1515

16-
var lsProjectVarsCmd = &cobra.Command{
17-
Use: resourceCmd("project-vars", "project"),
18-
Aliases: []string{"pv"},
16+
var lsProjectVariablesCmd = &cobra.Command{
17+
Use: resourceCmd("project-variables", "project"),
18+
Aliases: []string{"project-vars", "pv"},
1919
Short: "Get list of a project's variables",
2020
RunE: func(cmd *cobra.Command, args []string) error {
2121
ids, err := config.aliasIdsOrArgs(currentAlias, "project", args)

cli/cmd/related.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/fatih/color"
7+
"github.com/spf13/cobra"
8+
"strings"
9+
)
10+
11+
type relatedCommand struct {
12+
cmd *cobra.Command
13+
args map[string]string
14+
}
15+
16+
func newRelatedCommand(cmd *cobra.Command, args map[string]string) *relatedCommand {
17+
return &relatedCommand{cmd, args}
18+
}
19+
20+
func getFullCommand(path string, cmd *cobra.Command) string {
21+
var p string
22+
if path == "" {
23+
p = fmt.Sprintf("%s %s", cmd.Use, path)
24+
} else {
25+
parts := strings.Split(cmd.Use, " ")
26+
p = fmt.Sprintf("%s %s", parts[0], path)
27+
}
28+
29+
parent := cmd.Parent()
30+
if parent != nil {
31+
return getFullCommand(p, parent)
32+
}
33+
34+
return p
35+
}
36+
37+
func relatedCommands(cmds []*relatedCommand) {
38+
if !verbose {
39+
return
40+
}
41+
42+
color.Yellow("Related commands:")
43+
fmt.Println("")
44+
for _, r := range cmds {
45+
full := getFullCommand("", r.cmd)
46+
for k, v := range r.args {
47+
full = strings.Replace(full, strings.ToUpper(k), v, -1)
48+
}
49+
50+
fmt.Printf("- %-42s %s\n", r.cmd.Short, color.YellowString(full))
51+
}
52+
fmt.Println("")
53+
}

integration/snapshots/get_user_verbose.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ Response meta
2727
Request id
2828
Runtime 0.000000
2929

30+
Related commands:
31+
32+
- List specific user ssh keys glc ls user-ssh-keys 1
33+

integration/snapshots/ls_help.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Usage:
66
Available Commands:
77
aliases List resource aliases
88
group-merge-requests List group merge requests
9-
group-vars Get list of a group's variables
9+
group-variables Get list of a group's variables
1010
groups List groups
1111
merge-requests List merge requests
1212
namespaces List namespaces
@@ -20,7 +20,7 @@ Available Commands:
2020
project-pipeline-jobs List project pipeline jobs
2121
project-pipelines List project pipelines
2222
project-protected-branches List project protected branches
23-
project-vars Get list of a project's variables
23+
project-variables Get list of a project's variables
2424
projects List projects
2525
runners List runners
2626
ssh-keys List current user ssh keys

0 commit comments

Comments
 (0)