Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 214
Improve workspace import command by allowing references to local files for content#793
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e2e71c
Support import by specifying file path
shreyas-goenka 13a5f74
Merge remote-tracking branch 'origin' into improve-improt
shreyas-goenka cff3e80
add integration test coverage
shreyas-goenka e2555dc
refinements
shreyas-goenka 33d8c6b
-
shreyas-goenka 7dcbc2f
add be
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| package workspace | ||
| import ( | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/databricks/databricks-sdk-go/apierr" | ||
| "github.com/databricks/databricks-sdk-go/service/workspace" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
| @@ -19,7 +27,43 @@ func exportOverride(exportCmd *cobra.Command, exportReq *workspace.ExportRequest | ||
| exportCmd.Annotations["template"] = `{{.Content | b64_decode}}` | ||
| } | ||
| // Give better errors / hints for common API errors. | ||
| func wrapImportAPIErrors(err error, importReq *workspace.Import) error { | ||
| apiErr := &apierr.APIError{} | ||
| if !errors.As(err, &apiErr) { | ||
| return err | ||
| } | ||
| isFormatSource := importReq.Format == workspace.ImportFormatSource || importReq.Format == "" | ||
| if isFormatSource && apiErr.StatusCode == http.StatusBadRequest && | ||
| strings.Contains(apiErr.Message, "The zip file may not be valid or may be an unsupported version.") { | ||
| return fmt.Errorf("%w Hint: Objects imported using format=SOURCE are expected to be zip encoded databricks source notebook(s) by default. Please specify a language using the --language flag if you are trying to import a single uncompressed notebook", err) | ||
| } | ||
| return err | ||
| } | ||
| func importOverride(importCmd *cobra.Command, importReq *workspace.Import) { | ||
| var filePath string | ||
| importCmd.Use = "import TARGET_PATH" | ||
| importCmd.Flags().StringVar(&filePath, "file", "", `Path of local file to import`) | ||
| importCmd.MarkFlagsMutuallyExclusive("content", "file") | ||
| originalRunE := importCmd.RunE | ||
| importCmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
| if filePath != "" { | ||
| b, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| importReq.Content = base64.StdEncoding.EncodeToString(b) | ||
| } | ||
| err := originalRunE(cmd, args) | ||
shreyas-goenka marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return wrapImportAPIErrors(err, importReq) | ||
| } | ||
| } | ||
| func init() { | ||
| listOverrides = append(listOverrides, listOverride) | ||
| exportOverrides = append(exportOverrides, exportOverride) | ||
| importOverrides = append(importOverrides, importOverride) | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like in the end it checks that the error type is
**apierr.APIErrorinstead ofapierr.APIErroror*apierr.APIError, is it expected?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This checks that the error type is
*apierr.APIErr. Theerrors.Asmethod requires a pointer to a type that implementserror(ie theError()method)In the Go SDK this is done by
*apierr.APIhence the double pointer being passed. see: https://github.com/databricks/databricks-sdk-go/blob/410c4b0a4cd0c38e97d8e5e3de7ddc2785fd9d12/apierr/errors.go#L65