This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
root.go
132 lines (114 loc) · 4.49 KB
/
root.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package commands
import (
"fmt"
aliasCmd "github.com/profclems/glab/commands/alias"
authCmd "github.com/profclems/glab/commands/auth"
"github.com/profclems/glab/commands/cmdutils"
completionCmd "github.com/profclems/glab/commands/completion"
configCmd "github.com/profclems/glab/commands/config"
"github.com/profclems/glab/commands/help"
issueCmd "github.com/profclems/glab/commands/issue"
labelCmd "github.com/profclems/glab/commands/label"
mrCmd "github.com/profclems/glab/commands/mr"
pipelineCmd "github.com/profclems/glab/commands/pipeline"
projectCmd "github.com/profclems/glab/commands/project"
releaseCmd "github.com/profclems/glab/commands/release"
updateCmd "github.com/profclems/glab/commands/update"
versionCmd "github.com/profclems/glab/commands/version"
"github.com/profclems/glab/internal/glrepo"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// RootCmd is the main root/parent command
func NewCmdRoot(f *cmdutils.Factory, version, buildDate string) *cobra.Command {
var rootCmd = &cobra.Command{
Use: "glab <command> <subcommand> [flags]",
Short: "A GitLab CLI Tool",
Long: `GLab is an open source Gitlab Cli tool bringing GitLab to your command line`,
SilenceErrors: true,
SilenceUsage: true,
Example: heredoc.Doc(`
$ glab issue create
$ glab mr list --all
$ glab mr checkout 123
$ glab pipeline ci view
`),
Annotations: map[string]string{
"help:environment": heredoc.Doc(`
GITLAB_TOKEN: an authentication token for API requests. Setting this avoids being
prompted to authenticate and overrides any previously stored credentials.
Can be set in the config with 'glab config set token xxxxxx'
GITLAB_URI or GITLAB_HOST: specify the url of the gitlab server if self hosted (eg: https://gitlab.example.com). Default is https://gitlab.com.
REMOTE_ALIAS or GIT_REMOTE_URL_VAR: git remote variable or alias that contains the gitlab url.
Can be set in the config with 'glab config set remote_alias origin'
VISUAL, EDITOR (in order of precedence): the editor tool to use for authoring text.
Can be set in the config with 'glab config set editor vim'
BROWSER: the web browser to use for opening links.
Can be set in the config with 'glab config set browser mybrowser'
GLAMOUR_STYLE: environment variable to set your desired markdown renderer style
Available options are (dark|light|notty) or set a custom style
https://github.com/charmbracelet/glamour#styles
`),
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
fmt.Printf("Unknown command: %s\n", args[0])
return cmd.Usage()
} else if ok, _ := cmd.Flags().GetBool("version"); ok {
return versionCmd.NewCmdVersion(version, buildDate).RunE(cmd, args)
}
return cmd.Help()
},
}
rootCmd.SetOut(f.IO.StdOut)
rootCmd.SetErr(f.IO.StdErr)
rootCmd.PersistentFlags().Bool("help", false, "Show help for command")
rootCmd.SetHelpFunc(help.RootHelpFunc)
rootCmd.SetUsageFunc(help.RootUsageFunc)
rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &cmdutils.FlagError{Err: err}
})
// Child commands
rootCmd.AddCommand(aliasCmd.NewCmdAlias(f))
rootCmd.AddCommand(configCmd.NewCmdConfig(f))
rootCmd.AddCommand(completionCmd.NewCmdCompletion(f.IO))
rootCmd.AddCommand(versionCmd.NewCmdVersion(version, buildDate))
rootCmd.AddCommand(updateCmd.NewCheckUpdateCmd(version, buildDate))
rootCmd.AddCommand(authCmd.NewCmdAuth(f))
// the commands below require apiClient and resolved repos
f.BaseRepo = resolvedBaseRepo(f)
cmdutils.HTTPClientFactory(f) // Initialize HTTP Client
rootCmd.AddCommand(issueCmd.NewCmdIssue(f))
rootCmd.AddCommand(labelCmd.NewCmdLabel(f))
rootCmd.AddCommand(mrCmd.NewCmdMR(f))
rootCmd.AddCommand(pipelineCmd.NewCmdPipeline(f))
rootCmd.AddCommand(projectCmd.NewCmdRepo(f))
rootCmd.AddCommand(releaseCmd.NewCmdRelease(f))
rootCmd.Flags().BoolP("version", "v", false, "show glab version information")
return rootCmd
}
func resolvedBaseRepo(f *cmdutils.Factory) func() (glrepo.Interface, error) {
return func() (glrepo.Interface, error) {
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}
remotes, err := f.Remotes()
if err != nil {
return nil, err
}
repoContext, err := glrepo.ResolveRemotesToRepos(remotes, httpClient, "")
if err != nil {
return nil, err
}
baseRepo, err := repoContext.BaseRepo(true)
if err != nil {
return nil, err
}
return baseRepo, nil
}
}