-
Notifications
You must be signed in to change notification settings - Fork 168
Add workspace import-dir command #456
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
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2520cc0
Add workspace import-dir command
shreyas-goenka 7faae21
Merge remote-tracking branch 'origin' into import-dir-1
shreyas-goenka b4d6dc6
-
shreyas-goenka bf0fd01
add support for dirs
shreyas-goenka 6492bb5
add check for file exists in filer
shreyas-goenka 271a7ae
-
shreyas-goenka 971e838
added comments
shreyas-goenka 57af9b9
Merge remote-tracking branch 'origin' into import-dir-1
shreyas-goenka 326d634
account for windows paths
shreyas-goenka b2d5aa2
fix name for overwrite case
shreyas-goenka 73ac81b
types
shreyas-goenka 9cd2389
remove to abs
shreyas-goenka 54cb796
merge
shreyas-goenka 2aaa32b
merge
shreyas-goenka 857f7d6
debug test
shreyas-goenka 0602810
-
shreyas-goenka cfaa536
-
shreyas-goenka ba065f0
-
shreyas-goenka 14cf214
-
shreyas-goenka ae49f99
-
shreyas-goenka bba09a0
capture path from regex
shreyas-goenka 1832539
-
shreyas-goenka a38858c
Merge remote-tracking branch 'origin' into import-dir-1
shreyas-goenka 4941f13
fix integraion tests
shreyas-goenka ca4db9c
remove regex test
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package workspace | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io/fs" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/databricks/cli/libs/filer" | ||
| "github.com/databricks/cli/libs/notebook" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // The callback function imports the file specified at sourcePath. This function is | ||
| // meant to be used in conjunction with fs.WalkDir | ||
| // | ||
| // We deal with 3 different names for files. The need for this | ||
| // arises due to workspace API behaviour and limitations | ||
| // | ||
| // 1. Local name: The name for the file in the local file system | ||
| // 2. Remote name: The name of the file as materialized in the workspace | ||
| // 3. API payload name: The name to be used for API calls | ||
| // | ||
| // Example, consider the notebook "foo\\myNotebook.py" on a windows file system. | ||
| // The process to upload it would look like | ||
| // 1. Read the notebook, referring to it using it's local name "foo\\myNotebook.py" | ||
| // 2. API call to import the notebook to the workspace, using it API payload name "foo/myNotebook.py" | ||
| // 3. The notebook is materialized in the workspace using it's remote name "foo/myNotebook" | ||
| func importFileCallback(ctx context.Context, workspaceFiler filer.Filer, sourceDir, targetDir string) func(string, fs.DirEntry, error) error { | ||
| return func(sourcePath string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // localName is the name for the file in the local file system | ||
| localName, err := filepath.Rel(sourceDir, sourcePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // nameForApiCall is the name for the file to be used in any API call. | ||
| // This is a file name we provide to the filer.Write and Mkdir methods | ||
| nameForApiCall := filepath.ToSlash(localName) | ||
|
|
||
| // create directory and return early | ||
| if d.IsDir() { | ||
| return workspaceFiler.Mkdir(ctx, nameForApiCall) | ||
| } | ||
|
|
||
| // remoteName is the name of the file as visible in the workspace. We compute | ||
| // the remote name on the client side for logging purposes | ||
| remoteName := filepath.ToSlash(localName) | ||
| isNotebook, _, err := notebook.Detect(sourcePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if isNotebook { | ||
| ext := path.Ext(localName) | ||
| remoteName = strings.TrimSuffix(localName, ext) | ||
|
shreyas-goenka marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Open the local file | ||
| f, err := os.Open(sourcePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
shreyas-goenka marked this conversation as resolved.
|
||
| defer f.Close() | ||
|
|
||
| // Create file in WSFS | ||
| if importOverwrite { | ||
| err = workspaceFiler.Write(ctx, nameForApiCall, f, filer.OverwriteIfExists) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| err = workspaceFiler.Write(ctx, nameForApiCall, f) | ||
| if errors.Is(err, fs.ErrExist) { | ||
| // Emit file skipped event with the appropriate template | ||
| fileSkippedEvent := newFileSkippedEvent(localName, path.Join(targetDir, remoteName)) | ||
| template := "{{.SourcePath}} -> {{.TargetPath}} (skipped; already exists)\n" | ||
| return cmdio.RenderWithTemplate(ctx, fileSkippedEvent, template) | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| fileImportedEvent := newFileImportedEvent(localName, path.Join(targetDir, remoteName)) | ||
| return cmdio.RenderWithTemplate(ctx, fileImportedEvent, "{{.SourcePath}} -> {{.TargetPath}}\n") | ||
| } | ||
| } | ||
|
|
||
| var importDirCommand = &cobra.Command{ | ||
| Use: "import-dir SOURCE_PATH TARGET_PATH", | ||
| Short: `Import a directory from the local filesystem to a Databricks workspace.`, | ||
| Long: ` | ||
| Import a directory recursively from the local file system to a Databricks workspace. | ||
| Notebooks will have their extensions (one of .scala, .py, .sql, .ipynb, .r) stripped | ||
| `, | ||
| PreRunE: root.MustWorkspaceClient, | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
| ctx := cmd.Context() | ||
| w := root.WorkspaceClient(ctx) | ||
| sourceDir := args[0] | ||
| targetDir := args[1] | ||
|
|
||
| // Initialize a filer rooted at targetDir | ||
| workspaceFiler, err := filer.NewWorkspaceFilesClient(w, targetDir) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // TODO: print progress events on stderr instead: https://github.com/databricks/cli/issues/448 | ||
| err = cmdio.RenderJson(ctx, newImportStartedEvent(sourceDir)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Walk local directory tree and import files to the workspace | ||
| err = filepath.WalkDir(sourceDir, importFileCallback(ctx, workspaceFiler, sourceDir, targetDir)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return cmdio.RenderJson(ctx, newImportCompletedEvent(targetDir)) | ||
| }, | ||
| } | ||
|
|
||
| var importOverwrite bool | ||
|
|
||
| func init() { | ||
| importDirCommand.Flags().BoolVar(&importOverwrite, "overwrite", false, "overwrite existing workspace files") | ||
| Cmd.AddCommand(importDirCommand) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| file-in-dir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| hello, world |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "print(\"jupyter\")" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "language_info": { | ||
| "name": "python" | ||
| }, | ||
| "orig_nbformat": 4 | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 2 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Databricks notebook source | ||
| print("python") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Databricks notebook source | ||
| print("r") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // Databricks notebook source | ||
| println("scala") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- Databricks notebook source | ||
| SELECT "sql" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.