MCPcopy Create free account
hub / github.com/cli/cli / NewCmdRoot

Function NewCmdRoot

pkg/cmd/root/root.go:65–265  ·  view source on GitHub ↗
(f *cmdutil.Factory, telemetry ghtelemetry.InvocationRecorder, version, buildDate string)

Source from the content-addressed store, hash-verified

63}
64
65func NewCmdRoot(f *cmdutil.Factory, telemetry ghtelemetry.InvocationRecorder, version, buildDate string) (*cobra.Command, error) {
66 io := f.IOStreams
67 cfg, err := f.Config()
68 if err != nil {
69 return nil, fmt.Errorf("failed to read configuration: %s\n", err)
70 }
71
72 cmd := &cobra.Command{
73 Use: "gh <command> <subcommand> [flags]",
74 Short: "GitHub CLI",
75 Long: `Work seamlessly with GitHub from the command line.`,
76 Example: heredoc.Doc(`
77 $ gh issue create
78 $ gh repo clone cli/cli
79 $ gh pr checkout 321
80 `),
81 Annotations: map[string]string{
82 "versionInfo": versionCmd.Format(version, buildDate),
83 },
84 PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
85 // require that the user is authenticated before running most commands
86 if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) {
87 parent := cmd.Parent()
88 if parent != nil && parent.Use == "codespace" {
89 fmt.Fprintln(io.ErrOut, "To get started with GitHub CLI, please run: gh auth login -s codespace")
90 } else {
91 fmt.Fprint(io.ErrOut, authHelp())
92 }
93 return &AuthError{}
94 }
95
96 return nil
97 },
98 }
99
100 // cmd.SetOut(f.IOStreams.Out) // can't use due to https://github.com/spf13/cobra/issues/1708
101 // cmd.SetErr(f.IOStreams.ErrOut) // just let it default to os.Stderr instead
102
103 cmd.PersistentFlags().Bool("help", false, "Show help for command")
104
105 // override Cobra's default behaviors unless an opt-out has been set
106 if os.Getenv("GH_COBRA") == "" {
107 cmd.SilenceErrors = true
108 cmd.SilenceUsage = true
109
110 // this --version flag is checked in rootHelpFunc
111 cmd.Flags().Bool("version", false, "Show gh version")
112
113 cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
114 rootHelpFunc(f, c, args)
115 })
116 cmd.SetUsageFunc(func(c *cobra.Command) error {
117 return rootUsageFunc(f.IOStreams.ErrOut, c)
118 })
119 cmd.SetFlagErrorFunc(rootFlagErrorFunc)
120 }
121
122 cmd.AddGroup(&cobra.Group{

Callers 4

MainFunction · 0.92
runFunction · 0.92

Calls 15

IsAuthCheckEnabledFunction · 0.92
CheckAuthFunction · 0.92
SmartBaseRepoFuncFunction · 0.92
ValidAliasNameFuncFunction · 0.92
ValidAliasExpansionFuncFunction · 0.92
DisableAuthCheckFunction · 0.92
authHelpFunction · 0.85
rootHelpFuncFunction · 0.85
rootUsageFuncFunction · 0.85
NewCmdHelpTopicFunction · 0.85
NewCmdShellAliasFunction · 0.85

Tested by 2