This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
factory.go
137 lines (123 loc) · 3.43 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Forked from https://github.com/cli/cli/blob/929e082c13909044e2585af292ae952c9ca6f25c/pkg/cmd/factory/default.go
package cmdutils
import (
"fmt"
"strings"
"github.com/profclems/glab/api"
"github.com/profclems/glab/internal/config"
"github.com/profclems/glab/internal/glrepo"
"github.com/profclems/glab/pkg/git"
"github.com/profclems/glab/pkg/glinstance"
"github.com/profclems/glab/pkg/iostreams"
"github.com/xanzy/go-gitlab"
)
var (
CachedConfig config.Config
ConfigError error
)
type Factory struct {
HttpClient func() (*gitlab.Client, error)
BaseRepo func() (glrepo.Interface, error)
Remotes func() (glrepo.Remotes, error)
Config func() (config.Config, error)
Branch func() (string, error)
IO *iostreams.IOStreams
}
func (f *Factory) RepoOverride(repo string) error {
f.BaseRepo = func() (glrepo.Interface, error) {
return glrepo.FromFullName(repo)
}
newRepo, err := f.BaseRepo()
if err != nil {
return err
}
// Initialise new http client for new repo host
cfg, err := f.Config()
if err == nil {
OverrideAPIProtocol(cfg, newRepo)
}
f.HttpClient = func() (*gitlab.Client, error) {
return LabClientFunc(newRepo.RepoHost(), cfg, false)
}
return nil
}
func LabClientFunc(repoHost string, cfg config.Config, isGraphQL bool) (*gitlab.Client, error) {
c, err := api.NewClientWithCfg(repoHost, cfg, isGraphQL)
if err != nil {
return nil, err
}
return c.Lab(), nil
}
func remotesFunc() (glrepo.Remotes, error) {
hostOverride := ""
if !strings.EqualFold(glinstance.Default(), glinstance.OverridableDefault()) {
hostOverride = glinstance.OverridableDefault()
}
rr := &remoteResolver{
readRemotes: git.Remotes,
getConfig: configFunc,
}
fn := rr.Resolver(hostOverride)
return fn()
}
func configFunc() (config.Config, error) {
if CachedConfig != nil || ConfigError != nil {
return CachedConfig, ConfigError
}
CachedConfig, ConfigError = initConfig()
return CachedConfig, ConfigError
}
func baseRepoFunc() (glrepo.Interface, error) {
remotes, err := remotesFunc()
if err != nil {
return nil, err
}
return remotes[0], nil
}
// OverrideAPIProtocol sets api protocol for host to initialize http client
func OverrideAPIProtocol(cfg config.Config, repo glrepo.Interface) {
protocol, _ := cfg.Get(repo.RepoHost(), "api_protocol")
api.SetProtocol(protocol)
}
func HTTPClientFactory(f *Factory) {
f.HttpClient = func() (*gitlab.Client, error) {
cfg, err := configFunc()
if err != nil {
return nil, err
}
repo, err := baseRepoFunc()
if err != nil {
// use default hostname if remote resolver fails
repo = glrepo.NewWithHost("", "", glinstance.OverridableDefault())
}
OverrideAPIProtocol(cfg, repo)
return LabClientFunc(repo.RepoHost(), cfg, false)
}
}
func NewFactory() *Factory {
return &Factory{
Config: configFunc,
Remotes: remotesFunc,
HttpClient: func() (*gitlab.Client, error) {
// do not initialize httpclient since it may not be required by
// some commands like version, help, etc...
// It should be explicitly initialize with HTTPClientFactory()
return nil, nil
},
BaseRepo: baseRepoFunc,
Branch: func() (string, error) {
currentBranch, err := git.CurrentBranch()
if err != nil {
return "", fmt.Errorf("could not determine current branch: %w", err)
}
return currentBranch, nil
},
IO: iostreams.Init(),
}
}
func initConfig() (config.Config, error) {
if err := config.MigrateOldConfig(); err != nil {
return nil, err
}
return config.Init()
}