66 "net/http"
77 "os"
88
9+ "github.com/cli/cli/api"
10+ "github.com/cli/cli/context"
911 "github.com/cli/cli/git"
1012 "github.com/cli/cli/internal/config"
1113 "github.com/cli/cli/internal/ghrepo"
@@ -14,11 +16,93 @@ import (
1416)
1517
1618func New (appVersion string ) * cmdutil.Factory {
17- io := iostreams .System ()
19+ f := & cmdutil.Factory {
20+ IOStreams : iostreams .System (), // No factory dependencies
21+ Config : configFunc (), // No factory dependencies
22+ Branch : branchFunc (), // No factory dependencies
23+ Executable : executable (), // No factory dependencies
24+ }
25+
26+ f .HttpClient = httpClientFunc (f , appVersion ) // Depends on Config, IOStreams, and appVersion
27+ f .Remotes = remotesFunc (f ) // Depends on Config
28+ f .BaseRepo = BaseRepoFunc (f ) // Depends on Remotes
29+ f .Browser = browser (f ) // Depends on IOStreams
30+
31+ return f
32+ }
33+
34+ func BaseRepoFunc (f * cmdutil.Factory ) func () (ghrepo.Interface , error ) {
35+ return func () (ghrepo.Interface , error ) {
36+ remotes , err := f .Remotes ()
37+ if err != nil {
38+ return nil , err
39+ }
40+ return remotes [0 ], nil
41+ }
42+ }
43+
44+ func SmartBaseRepoFunc (f * cmdutil.Factory ) func () (ghrepo.Interface , error ) {
45+ return func () (ghrepo.Interface , error ) {
46+ httpClient , err := f .HttpClient ()
47+ if err != nil {
48+ return nil , err
49+ }
50+
51+ apiClient := api .NewClientFromHTTP (httpClient )
52+
53+ remotes , err := f .Remotes ()
54+ if err != nil {
55+ return nil , err
56+ }
57+ repoContext , err := context .ResolveRemotesToRepos (remotes , apiClient , "" )
58+ if err != nil {
59+ return nil , err
60+ }
61+ baseRepo , err := repoContext .BaseRepo (f .IOStreams )
62+ if err != nil {
63+ return nil , err
64+ }
65+
66+ return baseRepo , nil
67+ }
68+ }
69+
70+ func remotesFunc (f * cmdutil.Factory ) func () (context.Remotes , error ) {
71+ rr := & remoteResolver {
72+ readRemotes : git .Remotes ,
73+ getConfig : f .Config ,
74+ }
75+ return rr .Resolver ()
76+ }
77+
78+ func httpClientFunc (f * cmdutil.Factory , appVersion string ) func () (* http.Client , error ) {
79+ return func () (* http.Client , error ) {
80+ io := f .IOStreams
81+ cfg , err := f .Config ()
82+ if err != nil {
83+ return nil , err
84+ }
85+ return NewHTTPClient (io , cfg , appVersion , true ), nil
86+ }
87+ }
1888
89+ func browser (f * cmdutil.Factory ) cmdutil.Browser {
90+ io := f .IOStreams
91+ return cmdutil .NewBrowser (os .Getenv ("BROWSER" ), io .Out , io .ErrOut )
92+ }
93+
94+ func executable () string {
95+ gh := "gh"
96+ if exe , err := os .Executable (); err == nil {
97+ gh = exe
98+ }
99+ return gh
100+ }
101+
102+ func configFunc () func () (config.Config , error ) {
19103 var cachedConfig config.Config
20104 var configError error
21- configFunc := func () (config.Config , error ) {
105+ return func () (config.Config , error ) {
22106 if cachedConfig != nil || configError != nil {
23107 return cachedConfig , configError
24108 }
@@ -30,45 +114,14 @@ func New(appVersion string) *cmdutil.Factory {
30114 cachedConfig = config .InheritEnv (cachedConfig )
31115 return cachedConfig , configError
32116 }
117+ }
33118
34- rr := & remoteResolver {
35- readRemotes : git .Remotes ,
36- getConfig : configFunc ,
37- }
38- remotesFunc := rr .Resolver ()
39-
40- ghExecutable := "gh"
41- if exe , err := os .Executable (); err == nil {
42- ghExecutable = exe
43- }
44-
45- return & cmdutil.Factory {
46- IOStreams : io ,
47- Config : configFunc ,
48- Remotes : remotesFunc ,
49- HttpClient : func () (* http.Client , error ) {
50- cfg , err := configFunc ()
51- if err != nil {
52- return nil , err
53- }
54-
55- return NewHTTPClient (io , cfg , appVersion , true ), nil
56- },
57- BaseRepo : func () (ghrepo.Interface , error ) {
58- remotes , err := remotesFunc ()
59- if err != nil {
60- return nil , err
61- }
62- return remotes [0 ], nil
63- },
64- Branch : func () (string , error ) {
65- currentBranch , err := git .CurrentBranch ()
66- if err != nil {
67- return "" , fmt .Errorf ("could not determine current branch: %w" , err )
68- }
69- return currentBranch , nil
70- },
71- Executable : ghExecutable ,
72- Browser : cmdutil .NewBrowser (os .Getenv ("BROWSER" ), io .Out , io .ErrOut ),
119+ func branchFunc () func () (string , error ) {
120+ return func () (string , error ) {
121+ currentBranch , err := git .CurrentBranch ()
122+ if err != nil {
123+ return "" , fmt .Errorf ("could not determine current branch: %w" , err )
124+ }
125+ return currentBranch , nil
73126 }
74127}
0 commit comments