From d92c80b560bcd1aa22696c8d8bddf5bb02c44a3d Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 23 Jul 2020 11:58:22 -0500 Subject: [PATCH] just swap BaseRepo implementation --- command/root.go | 20 ------------------ pkg/cmd/repo/view/view.go | 38 +++++++++++++++++++++++++++------- pkg/cmd/repo/view/view_test.go | 10 ++++----- pkg/cmdutil/factory.go | 7 +++---- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/command/root.go b/command/root.go index 98399ccf7..b988fddfb 100644 --- a/command/root.go +++ b/command/root.go @@ -90,26 +90,6 @@ func init() { } return httpClient(token), nil }, - ResolvedBaseRepo: func(client *http.Client) (ghrepo.Interface, error) { - // TODO this may be abandoned when we stop trying to be magical about picking base repos for - // users. We can merge this with BaseRepo at that point. - apiClient := api.NewClientFromHTTP(client) - ctx := context.New() - remotes, err := ctx.Remotes() - if err != nil { - return nil, err - } - repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "") - if err != nil { - return nil, err - } - baseRepo, err := repoContext.BaseRepo() - if err != nil { - return nil, err - } - - return baseRepo, nil - }, BaseRepo: func() (ghrepo.Interface, error) { // TODO: decouple from `context` ctx := context.New() diff --git a/pkg/cmd/repo/view/view.go b/pkg/cmd/repo/view/view.go index eed90a9ad..6e8e5b8cf 100644 --- a/pkg/cmd/repo/view/view.go +++ b/pkg/cmd/repo/view/view.go @@ -9,6 +9,7 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" + "github.com/cli/cli/context" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" @@ -17,9 +18,9 @@ import ( ) type ViewOptions struct { - HttpClient func() (*http.Client, error) - IO *iostreams.IOStreams - ResolvedBaseRepo func(*http.Client) (ghrepo.Interface, error) + HttpClient func() (*http.Client, error) + IO *iostreams.IOStreams + BaseRepo func() (ghrepo.Interface, error) RepoArg string Web bool @@ -27,9 +28,32 @@ type ViewOptions struct { func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { opts := ViewOptions{ - IO: f.IOStreams, - HttpClient: f.HttpClient, - ResolvedBaseRepo: f.ResolvedBaseRepo, + IO: f.IOStreams, + HttpClient: f.HttpClient, + BaseRepo: func() (ghrepo.Interface, error) { + httpClient, err := f.HttpClient() + if err != nil { + return nil, err + } + + apiClient := api.NewClientFromHTTP(httpClient) + + ctx := context.New() + remotes, err := ctx.Remotes() + if err != nil { + return nil, err + } + repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "") + if err != nil { + return nil, err + } + baseRepo, err := repoContext.BaseRepo() + if err != nil { + return nil, err + } + + return baseRepo, nil + }, } cmd := &cobra.Command{ @@ -66,7 +90,7 @@ func viewRun(opts *ViewOptions) error { var toView ghrepo.Interface if opts.RepoArg == "" { var err error - toView, err = opts.ResolvedBaseRepo(httpClient) + toView, err = opts.BaseRepo() if err != nil { return err } diff --git a/pkg/cmd/repo/view/view_test.go b/pkg/cmd/repo/view/view_test.go index 810e6b573..5a6d08702 100644 --- a/pkg/cmd/repo/view/view_test.go +++ b/pkg/cmd/repo/view/view_test.go @@ -113,7 +113,7 @@ func Test_RepoView_Web(t *testing.T) { HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }, - ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) { + BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }, } @@ -225,7 +225,7 @@ func Test_ViewRun(t *testing.T) { tt.repoName = "OWNER/REPO" } - tt.opts.ResolvedBaseRepo = func(_ *http.Client) (ghrepo.Interface, error) { + tt.opts.BaseRepo = func() (ghrepo.Interface, error) { repo, _ := ghrepo.FromFullName(tt.repoName) return repo, nil } @@ -312,7 +312,7 @@ func Test_ViewRun_NonMarkdownReadme(t *testing.T) { HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }, - ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) { + BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }, } @@ -378,7 +378,7 @@ func Test_ViewRun_NoReadme(t *testing.T) { HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }, - ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) { + BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }, } @@ -448,7 +448,7 @@ func Test_ViewRun_NoDescription(t *testing.T) { HttpClient: func() (*http.Client, error) { return &http.Client{Transport: reg}, nil }, - ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) { + BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil }, } diff --git a/pkg/cmdutil/factory.go b/pkg/cmdutil/factory.go index 0cdc2915a..ad7162415 100644 --- a/pkg/cmdutil/factory.go +++ b/pkg/cmdutil/factory.go @@ -8,8 +8,7 @@ import ( ) type Factory struct { - IOStreams *iostreams.IOStreams - HttpClient func() (*http.Client, error) - ResolvedBaseRepo func(*http.Client) (ghrepo.Interface, error) - BaseRepo func() (ghrepo.Interface, error) + IOStreams *iostreams.IOStreams + HttpClient func() (*http.Client, error) + BaseRepo func() (ghrepo.Interface, error) }