Skip to content

Commit fd6ef17

Browse files
committed
extract option defaults logic
Signed-off-by: Cian Johnston <[email protected]>
1 parent 47331e6 commit fd6ef17

File tree

3 files changed

+153
-25
lines changed

3 files changed

+153
-25
lines changed

envbuilder.go

+2-25
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
"github.com/coder/envbuilder/constants"
2828
"github.com/coder/envbuilder/git"
29-
"github.com/coder/envbuilder/internal/chmodfs"
3029
"github.com/coder/envbuilder/options"
3130

3231
"github.com/GoogleContainerTools/kaniko/pkg/config"
@@ -42,7 +41,6 @@ import (
4241
_ "github.com/distribution/distribution/v3/registry/storage/driver/filesystem"
4342
"github.com/docker/cli/cli/config/configfile"
4443
"github.com/fatih/color"
45-
"github.com/go-git/go-billy/v5/osfs"
4644
"github.com/go-git/go-git/v5/plumbing/transport"
4745
v1 "github.com/google/go-containerregistry/pkg/v1"
4846
"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -61,23 +59,8 @@ type DockerConfig configfile.ConfigFile
6159
// Filesystem is the filesystem to use for all operations.
6260
// Defaults to the host filesystem.
6361
func Run(ctx context.Context, opts options.Options) error {
64-
// Temporarily removed these from the default settings to prevent conflicts
65-
// between current and legacy environment variables that add default values.
66-
// Once the legacy environment variables are phased out, this can be
67-
// reinstated to the previous default values.
68-
if len(opts.IgnorePaths) == 0 {
69-
opts.IgnorePaths = []string{
70-
"/var/run",
71-
// KinD adds these paths to pods, so ignore them by default.
72-
"/product_uuid", "/product_name",
73-
}
74-
}
75-
if opts.InitScript == "" {
76-
opts.InitScript = "sleep infinity"
77-
}
78-
if opts.InitCommand == "" {
79-
opts.InitCommand = "/bin/sh"
80-
}
62+
opts.SetDefaults()
63+
8164
if opts.CacheRepo == "" && opts.PushImage {
8265
return fmt.Errorf("--cache-repo must be set when using --push-image")
8366
}
@@ -90,12 +73,6 @@ func Run(ctx context.Context, opts options.Options) error {
9073
return fmt.Errorf("parse init args: %w", err)
9174
}
9275
}
93-
if opts.Filesystem == nil {
94-
opts.Filesystem = chmodfs.New(osfs.New("/"))
95-
}
96-
if opts.WorkspaceFolder == "" {
97-
opts.WorkspaceFolder = options.DefaultWorkspaceFolder(opts.GitURL)
98-
}
9976

10077
stageNumber := 0
10178
startStage := func(format string, args ...any) func(format string, args ...any) {

options/defaults.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package options
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/go-git/go-billy/v5/osfs"
8+
9+
giturls "github.com/chainguard-dev/git-urls"
10+
"github.com/coder/envbuilder/constants"
11+
"github.com/coder/envbuilder/internal/chmodfs"
12+
)
13+
14+
// DefaultWorkspaceFolder returns the default workspace folder
15+
// for a given repository URL.
16+
func DefaultWorkspaceFolder(repoURL string) string {
17+
if repoURL == "" {
18+
return constants.EmptyWorkspaceDir
19+
}
20+
parsed, err := giturls.Parse(repoURL)
21+
if err != nil {
22+
return constants.EmptyWorkspaceDir
23+
}
24+
name := strings.Split(parsed.Path, "/")
25+
hasOwnerAndRepo := len(name) >= 2
26+
if !hasOwnerAndRepo {
27+
return constants.EmptyWorkspaceDir
28+
}
29+
repo := strings.TrimSuffix(name[len(name)-1], ".git")
30+
return fmt.Sprintf("/workspaces/%s", repo)
31+
}
32+
33+
func (o *Options) SetDefaults() {
34+
// Temporarily removed these from the default settings to prevent conflicts
35+
// between current and legacy environment variables that add default values.
36+
// Once the legacy environment variables are phased out, this can be
37+
// reinstated to the previous default values.
38+
if len(o.IgnorePaths) == 0 {
39+
o.IgnorePaths = []string{
40+
"/var/run",
41+
// KinD adds these paths to pods, so ignore them by default.
42+
"/product_uuid", "/product_name",
43+
}
44+
}
45+
if o.InitScript == "" {
46+
o.InitScript = "sleep infinity"
47+
}
48+
if o.InitCommand == "" {
49+
o.InitCommand = "/bin/sh"
50+
}
51+
52+
if o.Filesystem == nil {
53+
o.Filesystem = chmodfs.New(osfs.New("/"))
54+
}
55+
if o.WorkspaceFolder == "" {
56+
o.WorkspaceFolder = DefaultWorkspaceFolder(o.GitURL)
57+
}
58+
}

options/defaults_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package options_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/envbuilder/internal/chmodfs"
7+
"github.com/go-git/go-billy/v5/osfs"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/coder/envbuilder/constants"
12+
"github.com/coder/envbuilder/options"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestDefaultWorkspaceFolder(t *testing.T) {
17+
t.Parallel()
18+
19+
successTests := []struct {
20+
name string
21+
gitURL string
22+
expected string
23+
}{
24+
{
25+
name: "HTTP",
26+
gitURL: "https://github.com/coder/envbuilder.git",
27+
expected: "/workspaces/envbuilder",
28+
},
29+
{
30+
name: "SSH",
31+
gitURL: "[email protected]:coder/envbuilder.git",
32+
expected: "/workspaces/envbuilder",
33+
},
34+
{
35+
name: "username and password",
36+
gitURL: "https://username:[email protected]/coder/envbuilder.git",
37+
expected: "/workspaces/envbuilder",
38+
},
39+
{
40+
name: "fragment",
41+
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
42+
expected: "/workspaces/envbuilder",
43+
},
44+
{
45+
name: "empty",
46+
gitURL: "",
47+
expected: constants.EmptyWorkspaceDir,
48+
},
49+
}
50+
for _, tt := range successTests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
dir := options.DefaultWorkspaceFolder(tt.gitURL)
53+
require.Equal(t, tt.expected, dir)
54+
})
55+
}
56+
57+
invalidTests := []struct {
58+
name string
59+
invalidURL string
60+
}{
61+
{
62+
name: "simple text",
63+
invalidURL: "not a valid URL",
64+
},
65+
{
66+
name: "website URL",
67+
invalidURL: "www.google.com",
68+
},
69+
}
70+
for _, tt := range invalidTests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
dir := options.DefaultWorkspaceFolder(tt.invalidURL)
73+
require.Equal(t, constants.EmptyWorkspaceDir, dir)
74+
})
75+
}
76+
}
77+
78+
func TestOptions_SetDefaults(t *testing.T) {
79+
t.Parallel()
80+
81+
expected := options.Options{
82+
InitScript: "sleep infinity",
83+
InitCommand: "/bin/sh",
84+
IgnorePaths: []string{"/var/run", "/product_uuid", "/product_name"},
85+
Filesystem: chmodfs.New(osfs.New("/")),
86+
GitURL: "",
87+
WorkspaceFolder: constants.EmptyWorkspaceDir,
88+
}
89+
90+
var actual options.Options
91+
actual.SetDefaults()
92+
assert.Equal(t, expected, actual)
93+
}

0 commit comments

Comments
 (0)