-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
220 lines (182 loc) · 5.28 KB
/
config.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package glu
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"github.com/get-glu/glu/internal/git"
"github.com/get-glu/glu/internal/oci"
"github.com/get-glu/glu/pkg/config"
"github.com/get-glu/glu/pkg/containers"
"github.com/get-glu/glu/pkg/credentials"
"github.com/get-glu/glu/pkg/kv/bolt"
srcgit "github.com/get-glu/glu/pkg/phases/git"
"github.com/get-glu/glu/pkg/scm/github"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
giturls "github.com/whilp/git-urls"
)
// Config is a utility for extracting configured sources by their name
// derived from glu's conventional configuration format.
type Config struct {
ctx context.Context
conf *config.Config
creds *credentials.CredentialSource
cache struct {
oci map[string]*oci.Repository
repo map[string]*git.Repository
proposer map[string]srcgit.Proposer
bolt map[string]*bolt.DB
}
}
func newConfigSource(ctx context.Context, conf *config.Config) *Config {
c := &Config{
ctx: ctx,
conf: conf,
creds: credentials.New(conf.Credentials),
}
c.cache.oci = map[string]*oci.Repository{}
c.cache.repo = map[string]*git.Repository{}
c.cache.proposer = map[string]srcgit.Proposer{}
c.cache.bolt = map[string]*bolt.DB{}
return c
}
// GitRepository constructs and configures an instance of a *git.Repository
// using the name to lookup the relevant configuration.
// It caches built instances and returns the same instance for subsequent
// calls with the same name.
func (c *Config) GitRepository(name string) (_ *git.Repository, proposer srcgit.Proposer, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("git %q: %w", name, err)
}
}()
// check cache for previously built repository
if repo, ok := c.cache.repo[name]; ok {
return repo, c.cache.proposer[name], nil
}
conf, ok := c.conf.Sources.Git[name]
if !ok {
return nil, nil, errors.New("configuration not found")
}
var (
method transport.AuthMethod
srcOpts = []containers.Option[git.Repository]{
git.WithDefaultBranch(conf.DefaultBranch),
}
)
if conf.Path != "" {
srcOpts = append(srcOpts, git.WithFilesystemStorage(conf.Path))
}
if conf.Remote != nil {
slog.Debug("configuring remote", "remote", conf.Remote.Name)
srcOpts = append(srcOpts,
git.WithRemote(conf.Remote.Name, conf.Remote.URL),
git.WithInterval(conf.Remote.Interval),
)
if conf.Remote.Credential != "" {
creds, err := c.creds.Get(conf.Remote.Credential)
if err != nil {
return nil, nil, fmt.Errorf("repository %q: %w", name, err)
}
method, err = creds.GitAuthentication()
if err != nil {
return nil, nil, fmt.Errorf("repository %q: %w", name, err)
}
}
}
if method == nil {
method, err = ssh.DefaultAuthBuilder("git")
if err != nil {
return nil, nil, err
}
}
repo, err := git.NewRepository(c.ctx, slog.Default(), append(srcOpts, git.WithAuth(method))...)
if err != nil {
return nil, nil, err
}
if conf.Proposals != nil {
repoURL, err := giturls.Parse(conf.Remote.URL)
if err != nil {
return nil, nil, err
}
parts := strings.SplitN(strings.TrimPrefix(repoURL.Path, "/"), "/", 2)
if len(parts) < 2 {
return nil, nil, fmt.Errorf("unexpected repository URL path: %q", repoURL.Path)
}
var (
repoOwner = parts[0]
repoName = strings.TrimSuffix(parts[1], ".git")
)
var proposalsEnabled bool
if proposalsEnabled = conf.Proposals.Credential != ""; proposalsEnabled {
creds, err := c.creds.Get(conf.Proposals.Credential)
if err != nil {
return nil, nil, err
}
client, err := creds.GitHubClient(c.ctx)
if err != nil {
return nil, nil, err
}
proposer = github.New(
client,
repoOwner,
repoName,
)
}
slog.Debug("configured scm proposer",
slog.String("owner", repoOwner),
slog.String("name", repoName),
slog.Bool("proposals_enabled", proposalsEnabled),
)
}
c.cache.repo[name] = repo
c.cache.proposer[name] = proposer
return repo, proposer, nil
}
// OCIRepository constructs and configures an instance of a *oci.Repository
// using the name to lookup the relevant configuration.
// It caches built instances and returns the same instance for subsequent
// calls with the same name.
func (c *Config) OCIRepository(name string) (_ *oci.Repository, err error) {
// check cache for previously built repository
if repo, ok := c.cache.oci[name]; ok {
return repo, nil
}
conf, ok := c.conf.Sources.OCI[name]
if !ok {
return nil, fmt.Errorf("oci %q: configuration not found", name)
}
var cred *credentials.Credential
if conf.Credential != "" {
cred, err = c.creds.Get(conf.Credential)
if err != nil {
return nil, err
}
}
repo, err := oci.New(conf.Reference, cred)
if err != nil {
return nil, err
}
c.cache.oci[name] = repo
return repo, nil
}
// FileDB constructs and configures a boltdb instance from configuration.
// It caches built instances and returns the same instance for subsequent
// calls with the same name.
func (c *Config) FileDB(name string) (*bolt.DB, error) {
if db, ok := c.cache.bolt[name]; ok {
return db, nil
}
conf, ok := c.conf.History.File[name]
if !ok {
return nil, fmt.Errorf("file db %q: configuration not found", name)
}
db, err := bolt.Open(conf.Path, 0666, nil)
if err != nil {
return nil, err
}
c.cache.bolt[name] = db
return db, nil
}