proj local

This commit is contained in:
Shreyas Goenka 2022-11-23 13:43:02 +01:00
parent 696e337516
commit 66d32f914f
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 52 additions and 12 deletions

View File

@ -5,13 +5,13 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"github.com/databricks/bricks/git" "github.com/databricks/bricks/git"
"github.com/databricks/databricks-sdk-go/databricks" "github.com/databricks/databricks-sdk-go/databricks"
"github.com/databricks/databricks-sdk-go/service/clusters" "github.com/databricks/databricks-sdk-go/service/clusters"
"github.com/databricks/databricks-sdk-go/service/commands" "github.com/databricks/databricks-sdk-go/service/commands"
"github.com/databricks/databricks-sdk-go/service/scim" "github.com/databricks/databricks-sdk-go/service/scim"
"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/databricks/databricks-sdk-go/workspaces" "github.com/databricks/databricks-sdk-go/workspaces"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -19,9 +19,8 @@ import (
const CacheDirName = ".databricks" const CacheDirName = ".databricks"
type project struct { type project struct {
mu sync.Mutex localRoot string
remoteRoot string
root string
env string env string
config *Config config *Config
@ -74,7 +73,7 @@ func Initialize(ctx context.Context, root, env string) (context.Context, error)
} }
p := project{ p := project{
root: root, localRoot: root,
env: env, env: env,
config: &config, config: &config,
@ -114,8 +113,51 @@ func (p *project) WorkspacesClient() *workspaces.WorkspacesClient {
return p.wsc return p.wsc
} }
func (p *project) Root() string { func (p *project) LocalRoot() string {
return p.root return p.localRoot
}
func (p *project) OverrideRemoteRoot(newRemoteRoot string) {
p.remoteRoot = newRemoteRoot
}
func (p *project) RemoteRoot() (string, error) {
if p.remoteRoot != "" {
return p.remoteRoot, nil
}
me, err := p.Me()
if err != nil {
return "", err
}
repositoryName, err := git.RepositoryName()
if err != nil {
return "", err
}
p.remoteRoot = fmt.Sprintf("/Repos/%s/%s", me.UserName, repositoryName)
return p.remoteRoot, nil
}
// helper function useful for debugging
// TODO: recursively make api queries to get entire directory structure of project
// TODO: We can later leverage this to clean up empty directories that does not happen
// right now on bricks sync
func (p *project) ListRemoteFiles(ctx context.Context) ([]string, error) {
remoteRoot, err := p.RemoteRoot()
if err != nil {
return []string{}, err
}
wsc := p.WorkspacesClient()
files, err := wsc.Workspace.ListAll(ctx, workspace.ListRequest{
Path: remoteRoot,
})
if err != nil {
return []string{}, nil
}
filePaths := make([]string, len(files))
for i, file := range files {
filePaths[i] = file.Path
}
return filePaths, nil
} }
func (p *project) GetFileSet() *git.FileSet { func (p *project) GetFileSet() *git.FileSet {
@ -135,7 +177,7 @@ func (p *project) CacheDir() (string, error) {
return "", fmt.Errorf("please add /%s/ to .gitignore", CacheDirName) return "", fmt.Errorf("please add /%s/ to .gitignore", CacheDirName)
} }
cacheDirPath := filepath.Join(p.root, CacheDirName) cacheDirPath := filepath.Join(p.localRoot, CacheDirName)
// create cache dir if it does not exist // create cache dir if it does not exist
if _, err := os.Stat(cacheDirPath); os.IsNotExist(err) { if _, err := os.Stat(cacheDirPath); os.IsNotExist(err) {
err = os.Mkdir(cacheDirPath, os.ModeDir|os.ModePerm) err = os.Mkdir(cacheDirPath, os.ModeDir|os.ModePerm)
@ -155,8 +197,6 @@ func (p *project) Environment() Environment {
} }
func (p *project) Me() (*scim.User, error) { func (p *project) Me() (*scim.User, error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.me != nil { if p.me != nil {
return p.me, nil return p.me, nil
} }