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