2023-06-05 15:41:30 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2023-06-16 15:09:08 +00:00
|
|
|
"context"
|
2023-06-05 15:41:30 +00:00
|
|
|
"fmt"
|
2023-06-16 15:09:08 +00:00
|
|
|
"runtime"
|
2023-06-05 15:41:30 +00:00
|
|
|
"strings"
|
2023-06-16 15:09:08 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Scheme string
|
|
|
|
|
|
|
|
const (
|
|
|
|
DbfsScheme = Scheme("dbfs")
|
|
|
|
LocalScheme = Scheme("file")
|
|
|
|
NoScheme = Scheme("")
|
2023-06-05 15:41:30 +00:00
|
|
|
)
|
|
|
|
|
2023-06-16 15:09:08 +00:00
|
|
|
func filerForPath(ctx context.Context, fullPath string) (filer.Filer, string, error) {
|
|
|
|
parts := strings.SplitN(fullPath, ":/", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return nil, "", fmt.Errorf(`no scheme specified for path %s. Please specify scheme "dbfs" or "file". Example: file:/foo/bar or file:/c:/foo/bar`, fullPath)
|
2023-06-05 15:41:30 +00:00
|
|
|
}
|
2023-06-16 15:09:08 +00:00
|
|
|
scheme := Scheme(parts[0])
|
|
|
|
path := parts[1]
|
|
|
|
switch scheme {
|
|
|
|
case DbfsScheme:
|
|
|
|
w := root.WorkspaceClient(ctx)
|
|
|
|
f, err := filer.NewDbfsClient(w, "/")
|
|
|
|
return f, path, err
|
|
|
|
|
|
|
|
case LocalScheme:
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
parts := strings.SplitN(path, ":", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return nil, "", fmt.Errorf("no volume specfied for path: %s", path)
|
|
|
|
}
|
|
|
|
volume := parts[0] + ":"
|
|
|
|
relPath := parts[1]
|
|
|
|
f, err := filer.NewLocalClient(volume)
|
|
|
|
return f, relPath, err
|
|
|
|
}
|
|
|
|
f, err := filer.NewLocalClient("/")
|
|
|
|
return f, path, err
|
2023-06-05 15:41:30 +00:00
|
|
|
|
2023-06-16 15:09:08 +00:00
|
|
|
default:
|
|
|
|
return nil, "", fmt.Errorf(`unsupported scheme %s specified for path %s. Please specify scheme "dbfs" or "file". Example: file:/foo/bar or file:/c:/foo/bar`, scheme, fullPath)
|
|
|
|
}
|
2023-06-05 15:41:30 +00:00
|
|
|
}
|