Skip to content

fix: allow setting MagicDir in Options #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move EmptyWorkspaceDir to options
  • Loading branch information
johnstcn committed Sep 9, 2024
commit 0b0cdcdead5ab390c453d7340e6674ac0ce79b4c
8 changes: 0 additions & 8 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ import (
)

const (
// WorkspacesDir is the path to the directory where
// all workspaces are stored by default.
WorkspacesDir = "/workspaces"

// EmptyWorkspaceDir is the path to a workspace that has
// nothing going on... it's empty!
EmptyWorkspaceDir = WorkspacesDir + "/empty"

// defaultMagicDirBase is the default working location for envbuilder.
// This is a special directory that must not be modified by the user
// or images. This is intentionally unexported.
Expand Down
11 changes: 7 additions & 4 deletions options/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import (
"github.com/go-git/go-billy/v5/osfs"

giturls "github.com/chainguard-dev/git-urls"
"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/internal/chmodfs"
)

// EmptyWorkspaceDir is the path to a workspace that has
// nothing going on... it's empty!
var EmptyWorkspaceDir = "/workspaces/empty"

// DefaultWorkspaceFolder returns the default workspace folder
// for a given repository URL.
func DefaultWorkspaceFolder(repoURL string) string {
if repoURL == "" {
return constants.EmptyWorkspaceDir
return EmptyWorkspaceDir
}
parsed, err := giturls.Parse(repoURL)
if err != nil {
return constants.EmptyWorkspaceDir
return EmptyWorkspaceDir
}
name := strings.Split(parsed.Path, "/")
hasOwnerAndRepo := len(name) >= 2
if !hasOwnerAndRepo {
return constants.EmptyWorkspaceDir
return EmptyWorkspaceDir
}
repo := strings.TrimSuffix(name[len(name)-1], ".git")
return fmt.Sprintf("/workspaces/%s", repo)
Expand Down
7 changes: 3 additions & 4 deletions options/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/stretchr/testify/assert"

"github.com/coder/envbuilder/constants"
"github.com/coder/envbuilder/options"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
{
name: "empty",
gitURL: "",
expected: constants.EmptyWorkspaceDir,
expected: options.EmptyWorkspaceDir,
},
}
for _, tt := range successTests {
Expand All @@ -70,7 +69,7 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
for _, tt := range invalidTests {
t.Run(tt.name, func(t *testing.T) {
dir := options.DefaultWorkspaceFolder(tt.invalidURL)
require.Equal(t, constants.EmptyWorkspaceDir, dir)
require.Equal(t, options.EmptyWorkspaceDir, dir)
})
}
}
Expand All @@ -84,7 +83,7 @@ func TestOptions_SetDefaults(t *testing.T) {
IgnorePaths: []string{"/var/run", "/product_uuid", "/product_name"},
Filesystem: chmodfs.New(osfs.New("/")),
GitURL: "",
WorkspaceFolder: constants.EmptyWorkspaceDir,
WorkspaceFolder: options.EmptyWorkspaceDir,
BinaryPath: "/.envbuilder/bin/envbuilder",
}

Expand Down