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

     1  package mutator
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"unicode"
     7  
     8  	"github.com/databricks/cli/bundle"
     9  	"github.com/databricks/cli/bundle/config"
    10  )
    11  
    12  type populateCurrentUser struct{}
    13  
    14  // PopulateCurrentUser sets the `current_user` property on the workspace.
    15  func PopulateCurrentUser() bundle.Mutator {
    16  	return &populateCurrentUser{}
    17  }
    18  
    19  func (m *populateCurrentUser) Name() string {
    20  	return "PopulateCurrentUser"
    21  }
    22  
    23  func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) error {
    24  	w := b.WorkspaceClient()
    25  	me, err := w.CurrentUser.Me(ctx)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	b.Config.Workspace.CurrentUser = &config.User{
    31  		ShortName: getShortUserName(me.UserName),
    32  		User:      me,
    33  	}
    34  	return nil
    35  }
    36  
    37  // Get a short-form username, based on the user's primary email address.
    38  // We leave the full range of unicode letters in tact, but remove all "special" characters,
    39  // including dots, which are not supported in e.g. experiment names.
    40  func getShortUserName(emailAddress string) string {
    41  	r := []rune(strings.Split(emailAddress, "@")[0])
    42  	for i := 0; i < len(r); i++ {
    43  		if !unicode.IsLetter(r[i]) {
    44  			r[i] = '_'
    45  		}
    46  	}
    47  	return string(r)
    48  }