github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/pkg/cmd/root/root.go (about) 1 package root 2 3 import ( 4 "net/http" 5 6 "github.com/MakeNowJust/heredoc" 7 "github.com/abdfnx/gh-api/api" 8 "github.com/abdfnx/gh-api/context" 9 "github.com/abdfnx/gh-api/internal/ghrepo" 10 authCmd "github.com/abdfnx/gh-api/pkg/cmd/auth" 11 "github.com/abdfnx/gh-api/pkg/cmd/factory" 12 repoCmd "github.com/abdfnx/gh-api/pkg/cmd/repo" 13 versionCmd "github.com/abdfnx/gh-api/pkg/cmd/version" 14 "github.com/abdfnx/gh-api/pkg/cmdutil" 15 "github.com/spf13/cobra" 16 ) 17 18 func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { 19 cmd := &cobra.Command{ 20 Use: "secman <command> <subcommand> [flags]", 21 Short: "Secman CLI", 22 Long: `Work seamlessly with GitHub from the command line.`, 23 24 SilenceErrors: true, 25 SilenceUsage: true, 26 Example: heredoc.Doc(` 27 $ secman auth login 28 $ secman repo clone abdfnx/gh-api 29 `), 30 Annotations: map[string]string{ 31 "help:feedback": heredoc.Doc(` 32 Open an issue using at https://github.com/abdfnx/gh-api/issues 33 `), 34 }, 35 } 36 37 cmd.SetOut(f.IOStreams.Out) 38 cmd.SetErr(f.IOStreams.ErrOut) 39 40 cs := f.IOStreams.ColorScheme() 41 42 helpHelper := func(command *cobra.Command, args []string) { 43 rootHelpFunc(cs, command, args) 44 } 45 46 cmd.PersistentFlags().Bool("help", false, "Show help for command") 47 cmd.SetHelpFunc(helpHelper) 48 cmd.SetUsageFunc(rootUsageFunc) 49 cmd.SetFlagErrorFunc(rootFlagErrorFunc) 50 51 formattedVersion := versionCmd.Format(version, buildDate) 52 cmd.SetVersionTemplate(formattedVersion) 53 cmd.Version = formattedVersion 54 cmd.Flags().Bool("version", false, "Show secman version") 55 56 // Child commands 57 cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate)) 58 cmd.AddCommand(authCmd.NewCmdAuth(f)) 59 60 // the `api` command should not inherit any extra HTTP headers 61 bareHTTPCmdFactory := *f 62 bareHTTPCmdFactory.HttpClient = bareHTTPClient(f, version) 63 64 // below here at the commands that require the "intelligent" BaseRepo resolver 65 repoResolvingCmdFactory := *f 66 repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo(f) 67 68 cmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory)) 69 70 // Help topics 71 cmd.AddCommand(NewHelpTopic("environment")) 72 referenceCmd := NewHelpTopic("reference") 73 referenceCmd.SetHelpFunc(referenceHelpFn(f.IOStreams)) 74 cmd.AddCommand(referenceCmd) 75 76 cmdutil.DisableAuthCheck(cmd) 77 78 // this needs to appear last: 79 referenceCmd.Long = referenceLong(cmd) 80 return cmd 81 } 82 83 func bareHTTPClient(f *cmdutil.Factory, version string) func() (*http.Client, error) { 84 return func() (*http.Client, error) { 85 cfg, err := f.Config() 86 if err != nil { 87 return nil, err 88 } 89 return factory.NewHTTPClient(f.IOStreams, cfg, version, false), nil 90 } 91 } 92 93 func resolvedBaseRepo(f *cmdutil.Factory) func() (ghrepo.Interface, error) { 94 return func() (ghrepo.Interface, error) { 95 httpClient, err := f.HttpClient() 96 if err != nil { 97 return nil, err 98 } 99 100 apiClient := api.NewClientFromHTTP(httpClient) 101 102 remotes, err := f.Remotes() 103 if err != nil { 104 return nil, err 105 } 106 repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "") 107 if err != nil { 108 return nil, err 109 } 110 baseRepo, err := repoContext.BaseRepo(f.IOStreams) 111 if err != nil { 112 return nil, err 113 } 114 115 return baseRepo, nil 116 } 117 }