2023-01-26 18:55:38 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-01-26 18:55:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type expandWorkspaceRoot struct{}
|
|
|
|
|
|
|
|
// ExpandWorkspaceRoot expands ~ if present in the workspace root.
|
|
|
|
func ExpandWorkspaceRoot() bundle.Mutator {
|
|
|
|
return &expandWorkspaceRoot{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *expandWorkspaceRoot) Name() string {
|
|
|
|
return "ExpandWorkspaceRoot"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *expandWorkspaceRoot) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-04-12 14:54:36 +00:00
|
|
|
root := b.Config.Workspace.RootPath
|
2023-01-26 18:55:38 +00:00
|
|
|
if root == "" {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unable to expand workspace root: workspace root not defined")
|
2023-01-26 18:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentUser := b.Config.Workspace.CurrentUser
|
|
|
|
if currentUser == nil || currentUser.UserName == "" {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("unable to expand workspace root: current user not set")
|
2023-01-26 18:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(root, "~/") {
|
2024-10-02 15:34:00 +00:00
|
|
|
home := fmt.Sprintf("/Workspace/Users/%s", currentUser.UserName)
|
2023-04-12 14:54:36 +00:00
|
|
|
b.Config.Workspace.RootPath = path.Join(home, root[2:])
|
2023-01-26 18:55:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-01-26 18:55:38 +00:00
|
|
|
}
|