feat: Support notebooks without extensions

This commit is contained in:
Ilya Kuznetsov 2024-11-12 13:41:39 +01:00
parent 6c1230889a
commit a42b829968
No known key found for this signature in database
GPG Key ID: 91F3DDCF5D21CDDF
3 changed files with 28 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import (
"strings"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/notebook"
@ -117,6 +118,10 @@ func (t *translateContext) rewritePath(
return nil
}
type FileInfoWithVirtualExtension interface {
VirtualExtension() string
}
func (t *translateContext) translateNotebookPath(literal, localFullPath, localRelPath, remotePath string) (string, error) {
nb, _, err := notebook.DetectWithFS(t.b.SyncRoot, filepath.ToSlash(localRelPath))
if errors.Is(err, fs.ErrNotExist) {
@ -129,6 +134,18 @@ func (t *translateContext) translateNotebookPath(literal, localFullPath, localRe
return "", ErrIsNotNotebook{localFullPath}
}
if config.IsExplicitlyEnabled(t.b.Config.Presets.InPlaceDeployment) {
stat, err := t.b.SyncRoot.Stat(filepath.ToSlash(localRelPath))
if err != nil {
return "", fmt.Errorf("unable to get file info for %s: %w", localFullPath, err)
}
if i, ok := stat.(FileInfoWithVirtualExtension); ok {
vext := i.VirtualExtension()
return strings.TrimSuffix(remotePath, vext), nil
}
}
// Upon import, notebooks are stripped of their extension.
return strings.TrimSuffix(remotePath, filepath.Ext(localFullPath)), nil
}

View File

@ -53,6 +53,9 @@ type wsfsFileInfo struct {
// The export format of a notebook. This is not exposed by the SDK.
ReposExportFormat workspace.ExportFormat `json:"repos_export_format,omitempty"`
// Explicitly added file extension of the notebook based on its language
VirtualFileExtension string `json:"virtual_file_extension,omitempty"`
}
func (info wsfsFileInfo) Name() string {
@ -88,6 +91,10 @@ func (info wsfsFileInfo) WorkspaceObjectInfo() workspace.ObjectInfo {
return info.ObjectInfo
}
func (info wsfsFileInfo) VirtualExtension() string {
return info.VirtualFileExtension
}
// UnmarshalJSON is a custom unmarshaller for the wsfsFileInfo struct.
// It must be defined for this type because otherwise the implementation
// of the embedded ObjectInfo type will be used.

View File

@ -98,6 +98,7 @@ func (w *workspaceFilesExtensionsClient) getNotebookStatByNameWithExt(ctx contex
// Modify the stat object path to include the extension. This stat object will be used
// to return the fs.FileInfo object in the stat method.
stat.Path = stat.Path + ext
stat.VirtualFileExtension = ext
return &workspaceFileStatus{
wsfsFileInfo: stat,
nameForWorkspaceAPI: nameWithoutExt,
@ -127,6 +128,7 @@ func (w *workspaceFilesExtensionsClient) getNotebookStatByNameWithoutExt(ctx con
// Modify the stat object path to include the extension. This stat object will be used
// to return the fs.DirEntry object in the ReadDir method.
stat.Path = stat.Path + ext
stat.VirtualFileExtension = ext
return &workspaceFileStatus{
wsfsFileInfo: stat,
nameForWorkspaceAPI: name,
@ -214,7 +216,7 @@ func (w *workspaceFilesExtensionsClient) ReadDir(ctx context.Context, name strin
return nil, err
}
// Replace the entry with the new entry that includes the extension.
entries[i] = wsfsDirEntry{wsfsFileInfo{ObjectInfo: stat.ObjectInfo}}
entries[i] = wsfsDirEntry{wsfsFileInfo{ObjectInfo: stat.ObjectInfo, VirtualFileExtension: stat.VirtualFileExtension}}
}
// Error if we have seen this path before in the current directory.
@ -313,7 +315,7 @@ func (w *workspaceFilesExtensionsClient) Stat(ctx context.Context, name string)
return nil, err
}
return wsfsFileInfo{ObjectInfo: stat.ObjectInfo}, nil
return wsfsFileInfo{ObjectInfo: stat.ObjectInfo, VirtualFileExtension: stat.VirtualFileExtension}, nil
}
return info, err