X Tutup
package root import ( "net/http" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" "github.com/cli/cli/context" "github.com/cli/cli/internal/ghrepo" aliasCmd "github.com/cli/cli/pkg/cmd/alias" apiCmd "github.com/cli/cli/pkg/cmd/api" authCmd "github.com/cli/cli/pkg/cmd/auth" completionCmd "github.com/cli/cli/pkg/cmd/completion" configCmd "github.com/cli/cli/pkg/cmd/config" "github.com/cli/cli/pkg/cmd/factory" gistCmd "github.com/cli/cli/pkg/cmd/gist" iapiCmd "github.com/cli/cli/pkg/cmd/iapi" issueCmd "github.com/cli/cli/pkg/cmd/issue" prCmd "github.com/cli/cli/pkg/cmd/pr" releaseCmd "github.com/cli/cli/pkg/cmd/release" repoCmd "github.com/cli/cli/pkg/cmd/repo" creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits" secretCmd "github.com/cli/cli/pkg/cmd/secret" sshKeyCmd "github.com/cli/cli/pkg/cmd/ssh-key" versionCmd "github.com/cli/cli/pkg/cmd/version" "github.com/cli/cli/pkg/cmdutil" "github.com/spf13/cobra" ) func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { cmd := &cobra.Command{ Use: "gh [flags]", Short: "GitHub CLI", Long: `Work seamlessly with GitHub from the command line.`, SilenceErrors: true, SilenceUsage: true, Example: heredoc.Doc(` $ gh issue create $ gh repo clone cli/cli $ gh pr checkout 321 `), Annotations: map[string]string{ "help:feedback": heredoc.Doc(` Open an issue using 'gh issue create -R github.com/cli/cli' `), "help:environment": heredoc.Doc(` See 'gh help environment' for the list of supported environment variables. `), }, } cmd.SetOut(f.IOStreams.Out) cmd.SetErr(f.IOStreams.ErrOut) cs := f.IOStreams.ColorScheme() helpHelper := func(command *cobra.Command, args []string) { rootHelpFunc(cs, command, args) } cmd.PersistentFlags().Bool("help", false, "Show help for command") cmd.SetHelpFunc(helpHelper) cmd.SetUsageFunc(rootUsageFunc) cmd.SetFlagErrorFunc(rootFlagErrorFunc) formattedVersion := versionCmd.Format(version, buildDate) cmd.SetVersionTemplate(formattedVersion) cmd.Version = formattedVersion cmd.Flags().Bool("version", false, "Show gh version") // Child commands cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate)) cmd.AddCommand(aliasCmd.NewCmdAlias(f)) cmd.AddCommand(authCmd.NewCmdAuth(f)) cmd.AddCommand(configCmd.NewCmdConfig(f)) cmd.AddCommand(creditsCmd.NewCmdCredits(f, nil)) cmd.AddCommand(gistCmd.NewCmdGist(f)) cmd.AddCommand(completionCmd.NewCmdCompletion(f.IOStreams)) cmd.AddCommand(secretCmd.NewCmdSecret(f)) cmd.AddCommand(sshKeyCmd.NewCmdSSHKey(f)) // the `api` command should not inherit any extra HTTP headers bareHTTPCmdFactory := *f bareHTTPCmdFactory.HttpClient = bareHTTPClient(f, version) cmd.AddCommand(apiCmd.NewCmdApi(&bareHTTPCmdFactory, nil)) cmd.AddCommand(iapiCmd.NewCmdApi(&bareHTTPCmdFactory, nil)) // below here at the commands that require the "intelligent" BaseRepo resolver repoResolvingCmdFactory := *f repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo(f) cmd.AddCommand(prCmd.NewCmdPR(&repoResolvingCmdFactory)) cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory)) cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory)) cmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory)) // Help topics cmd.AddCommand(NewHelpTopic("environment")) referenceCmd := NewHelpTopic("reference") referenceCmd.SetHelpFunc(referenceHelpFn(f.IOStreams)) cmd.AddCommand(referenceCmd) cmdutil.DisableAuthCheck(cmd) // this needs to appear last: referenceCmd.Long = referenceLong(cmd) return cmd } func bareHTTPClient(f *cmdutil.Factory, version string) func() (*http.Client, error) { return func() (*http.Client, error) { cfg, err := f.Config() if err != nil { return nil, err } return factory.NewHTTPClient(f.IOStreams, cfg, version, false), nil } } func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) { return func() (ghrepo.Interface, error) { httpClient, err := f.HttpClient() if err != nil { return nil, err } apiClient := api.NewClientFromHTTP(httpClient) remotes, err := f.Remotes() if err != nil { return nil, err } repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "") if err != nil { return nil, err } baseRepo, err := repoContext.BaseRepo(f.IOStreams) if err != nil { return nil, err } return baseRepo, nil } }
X Tutup