2023-01-27 15:04:58 +00:00
|
|
|
package fileset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"time"
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/notebook"
|
2024-05-30 07:41:50 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2024-03-18 14:41:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown fileType = iota
|
|
|
|
Notebook // Databricks notebook file
|
|
|
|
Source // Any other file type
|
2023-01-27 15:04:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
2024-05-30 07:41:50 +00:00
|
|
|
// Root path of the fileset.
|
|
|
|
root vfs.Path
|
|
|
|
|
|
|
|
// File entry as returned by the [fs.WalkDir] function.
|
|
|
|
entry fs.DirEntry
|
|
|
|
|
|
|
|
// Type of the file.
|
|
|
|
fileType fileType
|
|
|
|
|
|
|
|
// Relative path within the fileset.
|
|
|
|
// Combine with the [vfs.Path] to interact with the underlying file.
|
|
|
|
Relative string
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func NewNotebookFile(root vfs.Path, entry fs.DirEntry, relative string) File {
|
2024-03-18 14:41:58 +00:00
|
|
|
return File{
|
2024-05-30 07:41:50 +00:00
|
|
|
root: root,
|
|
|
|
entry: entry,
|
2024-03-18 14:41:58 +00:00
|
|
|
fileType: Notebook,
|
2024-05-30 07:41:50 +00:00
|
|
|
Relative: relative,
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func NewFile(root vfs.Path, entry fs.DirEntry, relative string) File {
|
2024-03-18 14:41:58 +00:00
|
|
|
return File{
|
2024-05-30 07:41:50 +00:00
|
|
|
root: root,
|
|
|
|
entry: entry,
|
2024-03-18 14:41:58 +00:00
|
|
|
fileType: Unknown,
|
2024-05-30 07:41:50 +00:00
|
|
|
Relative: relative,
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func NewSourceFile(root vfs.Path, entry fs.DirEntry, relative string) File {
|
2024-03-18 14:41:58 +00:00
|
|
|
return File{
|
2024-05-30 07:41:50 +00:00
|
|
|
root: root,
|
|
|
|
entry: entry,
|
2024-03-18 14:41:58 +00:00
|
|
|
fileType: Source,
|
2024-05-30 07:41:50 +00:00
|
|
|
Relative: relative,
|
2024-03-18 14:41:58 +00:00
|
|
|
}
|
2023-01-27 15:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) Modified() (ts time.Time) {
|
2024-05-30 07:41:50 +00:00
|
|
|
info, err := f.entry.Info()
|
2023-01-27 15:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
// return default time, beginning of epoch
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
return info.ModTime()
|
|
|
|
}
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
func (f *File) IsNotebook() (bool, error) {
|
|
|
|
if f.fileType != Unknown {
|
|
|
|
return f.fileType == Notebook, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, detect the notebook type.
|
2024-05-30 07:41:50 +00:00
|
|
|
isNotebook, _, err := notebook.DetectWithFS(f.root, f.Relative)
|
2024-03-18 14:41:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if isNotebook {
|
|
|
|
f.fileType = Notebook
|
|
|
|
} else {
|
|
|
|
f.fileType = Source
|
|
|
|
}
|
|
|
|
return isNotebook, nil
|
|
|
|
}
|