Skip to content

Commit 2270f59

Browse files
committed
move DefaultWorkspaceFolder to options package
Signed-off-by: Cian Johnston <[email protected]>
1 parent cc0b4c5 commit 2270f59

File tree

2 files changed

+1
-98
lines changed

2 files changed

+1
-98
lines changed

envbuilder.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/GoogleContainerTools/kaniko/pkg/creds"
3333
"github.com/GoogleContainerTools/kaniko/pkg/executor"
3434
"github.com/GoogleContainerTools/kaniko/pkg/util"
35-
giturls "github.com/chainguard-dev/git-urls"
3635
"github.com/coder/envbuilder/devcontainer"
3736
"github.com/coder/envbuilder/internal/ebutil"
3837
"github.com/coder/envbuilder/internal/log"
@@ -95,11 +94,7 @@ func Run(ctx context.Context, opts options.Options) error {
9594
opts.Filesystem = &osfsWithChmod{osfs.New("/")}
9695
}
9796
if opts.WorkspaceFolder == "" {
98-
f, err := DefaultWorkspaceFolder(opts.GitURL)
99-
if err != nil {
100-
return err
101-
}
102-
opts.WorkspaceFolder = f
97+
opts.WorkspaceFolder = options.DefaultWorkspaceFolder(opts.GitURL)
10398
}
10499

105100
stageNumber := 0
@@ -930,25 +925,6 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
930925
return nil
931926
}
932927

933-
// DefaultWorkspaceFolder returns the default workspace folder
934-
// for a given repository URL.
935-
func DefaultWorkspaceFolder(repoURL string) (string, error) {
936-
if repoURL == "" {
937-
return constants.EmptyWorkspaceDir, nil
938-
}
939-
parsed, err := giturls.Parse(repoURL)
940-
if err != nil {
941-
return "", err
942-
}
943-
name := strings.Split(parsed.Path, "/")
944-
hasOwnerAndRepo := len(name) >= 2
945-
if !hasOwnerAndRepo {
946-
return constants.EmptyWorkspaceDir, nil
947-
}
948-
repo := strings.TrimSuffix(name[len(name)-1], ".git")
949-
return fmt.Sprintf("/workspaces/%s", repo), nil
950-
}
951-
952928
type userInfo struct {
953929
uid int
954930
gid int

envbuilder_test.go

-73
Original file line numberDiff line numberDiff line change
@@ -1,74 +1 @@
11
package envbuilder_test
2-
3-
import (
4-
"testing"
5-
6-
"github.com/coder/envbuilder"
7-
"github.com/coder/envbuilder/constants"
8-
9-
"github.com/stretchr/testify/require"
10-
)
11-
12-
func TestDefaultWorkspaceFolder(t *testing.T) {
13-
t.Parallel()
14-
15-
successTests := []struct {
16-
name string
17-
gitURL string
18-
expected string
19-
}{
20-
{
21-
name: "HTTP",
22-
gitURL: "https://github.com/coder/envbuilder.git",
23-
expected: "/workspaces/envbuilder",
24-
},
25-
{
26-
name: "SSH",
27-
gitURL: "[email protected]:coder/envbuilder.git",
28-
expected: "/workspaces/envbuilder",
29-
},
30-
{
31-
name: "username and password",
32-
gitURL: "https://username:[email protected]/coder/envbuilder.git",
33-
expected: "/workspaces/envbuilder",
34-
},
35-
{
36-
name: "fragment",
37-
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
38-
expected: "/workspaces/envbuilder",
39-
},
40-
{
41-
name: "empty",
42-
gitURL: "",
43-
expected: constants.EmptyWorkspaceDir,
44-
},
45-
}
46-
for _, tt := range successTests {
47-
t.Run(tt.name, func(t *testing.T) {
48-
dir, err := envbuilder.DefaultWorkspaceFolder(tt.gitURL)
49-
require.NoError(t, err)
50-
require.Equal(t, tt.expected, dir)
51-
})
52-
}
53-
54-
invalidTests := []struct {
55-
name string
56-
invalidURL string
57-
}{
58-
{
59-
name: "simple text",
60-
invalidURL: "not a valid URL",
61-
},
62-
{
63-
name: "website URL",
64-
invalidURL: "www.google.com",
65-
},
66-
}
67-
for _, tt := range invalidTests {
68-
t.Run(tt.name, func(t *testing.T) {
69-
dir, err := envbuilder.DefaultWorkspaceFolder(tt.invalidURL)
70-
require.NoError(t, err)
71-
require.Equal(t, constants.EmptyWorkspaceDir, dir)
72-
})
73-
}
74-
}

0 commit comments

Comments
 (0)