Skip to content

Commit bf54029

Browse files
committed
chore: extract git operations to separate package
1 parent ed546cd commit bf54029

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

envbuilder.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/coder/envbuilder/devcontainer"
2828
"github.com/coder/envbuilder/internal/ebutil"
2929
"github.com/coder/envbuilder/internal/log"
30+
"github.com/coder/envbuilder/pkg/git"
3031
"github.com/coder/envbuilder/pkg/options"
3132

3233
"github.com/GoogleContainerTools/kaniko/pkg/config"
@@ -204,7 +205,7 @@ func Run(ctx context.Context, opts options.Options) error {
204205
}
205206
}()
206207

207-
cloneOpts := CloneRepoOptions{
208+
cloneOpts := git.CloneRepoOptions{
208209
Path: opts.WorkspaceFolder,
209210
Storage: opts.Filesystem,
210211
Insecure: opts.Insecure,
@@ -214,15 +215,15 @@ func Run(ctx context.Context, opts options.Options) error {
214215
CABundle: caBundle,
215216
}
216217

217-
cloneOpts.RepoAuth = SetupRepoAuth(&opts)
218+
cloneOpts.RepoAuth = git.SetupRepoAuth(&opts)
218219
if opts.GitHTTPProxyURL != "" {
219220
cloneOpts.ProxyOptions = transport.ProxyOptions{
220221
URL: opts.GitHTTPProxyURL,
221222
}
222223
}
223224
cloneOpts.RepoURL = opts.GitURL
224225

225-
cloned, fallbackErr = CloneRepo(ctx, cloneOpts)
226+
cloned, fallbackErr = git.CloneRepo(ctx, cloneOpts)
226227
if fallbackErr == nil {
227228
if cloned {
228229
endStage("📦 Cloned repository!")

git.go renamed to pkg/git/git.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package envbuilder
1+
package git
22

33
import (
44
"context"

git_test.go renamed to pkg/git/git_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package envbuilder_test
1+
package git_test
22

33
import (
44
"context"
55
"crypto/ed25519"
66
"fmt"
7+
"github.com/coder/envbuilder/pkg/git"
78
"io"
89
"net/http/httptest"
910
"net/url"
@@ -14,7 +15,6 @@ import (
1415

1516
"github.com/coder/envbuilder/pkg/options"
1617

17-
"github.com/coder/envbuilder"
1818
"github.com/coder/envbuilder/internal/log"
1919
"github.com/coder/envbuilder/testutil/gittest"
2020
"github.com/coder/envbuilder/testutil/mwtest"
@@ -90,7 +90,7 @@ func TestCloneRepo(t *testing.T) {
9090
clientFS := memfs.New()
9191
// A repo already exists!
9292
_ = gittest.NewRepo(t, clientFS)
93-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
93+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
9494
Path: "/",
9595
RepoURL: srv.URL,
9696
Storage: clientFS,
@@ -108,7 +108,7 @@ func TestCloneRepo(t *testing.T) {
108108
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
109109
clientFS := memfs.New()
110110

111-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
111+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
112112
Path: "/workspace",
113113
RepoURL: srv.URL,
114114
Storage: clientFS,
@@ -145,7 +145,7 @@ func TestCloneRepo(t *testing.T) {
145145
authURL.User = url.UserPassword(tc.username, tc.password)
146146
clientFS := memfs.New()
147147

148-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
148+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
149149
Path: "/workspace",
150150
RepoURL: authURL.String(),
151151
Storage: clientFS,
@@ -184,7 +184,7 @@ func TestCloneRepoSSH(t *testing.T) {
184184
gitURL := tr.String()
185185
clientFS := memfs.New()
186186

187-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
187+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
188188
Path: "/workspace",
189189
RepoURL: gitURL,
190190
Storage: clientFS,
@@ -216,7 +216,7 @@ func TestCloneRepoSSH(t *testing.T) {
216216
clientFS := memfs.New()
217217

218218
anotherKey := randKeygen(t)
219-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
219+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
220220
Path: "/workspace",
221221
RepoURL: gitURL,
222222
Storage: clientFS,
@@ -246,7 +246,7 @@ func TestCloneRepoSSH(t *testing.T) {
246246
gitURL := tr.String()
247247
clientFS := memfs.New()
248248

249-
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
249+
cloned, err := git.CloneRepo(context.Background(), git.CloneRepoOptions{
250250
Path: "/workspace",
251251
RepoURL: gitURL,
252252
Storage: clientFS,
@@ -270,7 +270,7 @@ func TestSetupRepoAuth(t *testing.T) {
270270
opts := &options.Options{
271271
Logger: testLog(t),
272272
}
273-
auth := envbuilder.SetupRepoAuth(opts)
273+
auth := git.SetupRepoAuth(opts)
274274
require.Nil(t, auth)
275275
})
276276

@@ -279,7 +279,7 @@ func TestSetupRepoAuth(t *testing.T) {
279279
GitURL: "http://host.tld/repo",
280280
Logger: testLog(t),
281281
}
282-
auth := envbuilder.SetupRepoAuth(opts)
282+
auth := git.SetupRepoAuth(opts)
283283
require.Nil(t, auth)
284284
})
285285

@@ -290,7 +290,7 @@ func TestSetupRepoAuth(t *testing.T) {
290290
GitPassword: "pass",
291291
Logger: testLog(t),
292292
}
293-
auth := envbuilder.SetupRepoAuth(opts)
293+
auth := git.SetupRepoAuth(opts)
294294
ba, ok := auth.(*githttp.BasicAuth)
295295
require.True(t, ok)
296296
require.Equal(t, opts.GitUsername, ba.Username)
@@ -304,7 +304,7 @@ func TestSetupRepoAuth(t *testing.T) {
304304
GitPassword: "pass",
305305
Logger: testLog(t),
306306
}
307-
auth := envbuilder.SetupRepoAuth(opts)
307+
auth := git.SetupRepoAuth(opts)
308308
ba, ok := auth.(*githttp.BasicAuth)
309309
require.True(t, ok)
310310
require.Equal(t, opts.GitUsername, ba.Username)
@@ -318,7 +318,7 @@ func TestSetupRepoAuth(t *testing.T) {
318318
GitSSHPrivateKeyPath: kPath,
319319
Logger: testLog(t),
320320
}
321-
auth := envbuilder.SetupRepoAuth(opts)
321+
auth := git.SetupRepoAuth(opts)
322322
_, ok := auth.(*gitssh.PublicKeys)
323323
require.True(t, ok)
324324
})
@@ -330,7 +330,7 @@ func TestSetupRepoAuth(t *testing.T) {
330330
GitSSHPrivateKeyPath: kPath,
331331
Logger: testLog(t),
332332
}
333-
auth := envbuilder.SetupRepoAuth(opts)
333+
auth := git.SetupRepoAuth(opts)
334334
_, ok := auth.(*gitssh.PublicKeys)
335335
require.True(t, ok)
336336
})
@@ -343,7 +343,7 @@ func TestSetupRepoAuth(t *testing.T) {
343343
GitSSHPrivateKeyPath: kPath,
344344
Logger: testLog(t),
345345
}
346-
auth := envbuilder.SetupRepoAuth(opts)
346+
auth := git.SetupRepoAuth(opts)
347347
_, ok := auth.(*gitssh.PublicKeys)
348348
require.True(t, ok)
349349
})
@@ -356,7 +356,7 @@ func TestSetupRepoAuth(t *testing.T) {
356356
GitUsername: "user",
357357
Logger: testLog(t),
358358
}
359-
auth := envbuilder.SetupRepoAuth(opts)
359+
auth := git.SetupRepoAuth(opts)
360360
_, ok := auth.(*gitssh.PublicKeys)
361361
require.True(t, ok)
362362
})
@@ -368,7 +368,7 @@ func TestSetupRepoAuth(t *testing.T) {
368368
GitSSHPrivateKeyPath: kPath,
369369
Logger: testLog(t),
370370
}
371-
auth := envbuilder.SetupRepoAuth(opts)
371+
auth := git.SetupRepoAuth(opts)
372372
pk, ok := auth.(*gitssh.PublicKeys)
373373
require.True(t, ok)
374374
require.NotNil(t, pk.Signer)
@@ -382,7 +382,7 @@ func TestSetupRepoAuth(t *testing.T) {
382382
GitURL: "ssh://[email protected]:repo/path",
383383
Logger: testLog(t),
384384
}
385-
auth := envbuilder.SetupRepoAuth(opts)
385+
auth := git.SetupRepoAuth(opts)
386386
require.Nil(t, auth) // TODO: actually test SSH_AUTH_SOCK
387387
})
388388
}

0 commit comments

Comments
 (0)