github.com/databricks/cli@v0.203.0/bundle/config/mutator/expand_workspace_root.go (about)

     1  package mutator
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/databricks/cli/bundle"
    10  )
    11  
    12  type expandWorkspaceRoot struct{}
    13  
    14  // ExpandWorkspaceRoot expands ~ if present in the workspace root.
    15  func ExpandWorkspaceRoot() bundle.Mutator {
    16  	return &expandWorkspaceRoot{}
    17  }
    18  
    19  func (m *expandWorkspaceRoot) Name() string {
    20  	return "ExpandWorkspaceRoot"
    21  }
    22  
    23  func (m *expandWorkspaceRoot) Apply(ctx context.Context, b *bundle.Bundle) error {
    24  	root := b.Config.Workspace.RootPath
    25  	if root == "" {
    26  		return fmt.Errorf("unable to expand workspace root: workspace root not defined")
    27  	}
    28  
    29  	currentUser := b.Config.Workspace.CurrentUser
    30  	if currentUser == nil || currentUser.UserName == "" {
    31  		return fmt.Errorf("unable to expand workspace root: current user not set")
    32  	}
    33  
    34  	if strings.HasPrefix(root, "~/") {
    35  		home := fmt.Sprintf("/Users/%s", currentUser.UserName)
    36  		b.Config.Workspace.RootPath = path.Join(home, root[2:])
    37  	}
    38  
    39  	return nil
    40  }