mirror of https://github.com/databricks/cli.git
Translate artifact paths to local absolute directory path
This commit is contained in:
parent
6856a3916e
commit
de011c4018
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -34,6 +35,10 @@ const (
|
||||||
// It returns an error if the path does not exist or is a directory.
|
// It returns an error if the path does not exist or is a directory.
|
||||||
TranslateModeLocalAbsoluteFile
|
TranslateModeLocalAbsoluteFile
|
||||||
|
|
||||||
|
// TranslateModeLocalAbsoluteDirectory translates a path to the local absolute directory path.
|
||||||
|
// It returns an error if the path does not exist or is not a directory.
|
||||||
|
TranslateModeLocalAbsoluteDirectory
|
||||||
|
|
||||||
// TranslateModeLocalRelative translates a path to be relative to the bundle sync root path.
|
// TranslateModeLocalRelative translates a path to be relative to the bundle sync root path.
|
||||||
// It does not check if the path exists, nor care if it is a file or directory.
|
// It does not check if the path exists, nor care if it is a file or directory.
|
||||||
TranslateModeLocalRelative
|
TranslateModeLocalRelative
|
||||||
|
@ -157,6 +162,8 @@ func (t *translateContext) rewritePath(
|
||||||
interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath)
|
interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath)
|
||||||
case TranslateModeLocalAbsoluteFile:
|
case TranslateModeLocalAbsoluteFile:
|
||||||
interp, err = t.translateLocalAbsoluteFilePath(ctx, input, localPath, localRelPath)
|
interp, err = t.translateLocalAbsoluteFilePath(ctx, input, localPath, localRelPath)
|
||||||
|
case TranslateModeLocalAbsoluteDirectory:
|
||||||
|
interp, err = t.translateLocalAbsoluteDirectoryPath(ctx, input, localPath, localRelPath)
|
||||||
case TranslateModeLocalRelative:
|
case TranslateModeLocalRelative:
|
||||||
interp, err = t.translateLocalRelativePath(ctx, input, localPath, localRelPath)
|
interp, err = t.translateLocalRelativePath(ctx, input, localPath, localRelPath)
|
||||||
case TranslateModeLocalRelativeWithPrefix:
|
case TranslateModeLocalRelativeWithPrefix:
|
||||||
|
@ -254,6 +261,20 @@ func (t *translateContext) translateLocalAbsoluteFilePath(ctx context.Context, l
|
||||||
return localFullPath, nil
|
return localFullPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *translateContext) translateLocalAbsoluteDirectoryPath(ctx context.Context, literal, localFullPath, _ string) (string, error) {
|
||||||
|
info, err := os.Stat(localFullPath)
|
||||||
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
return "", fmt.Errorf("directory %s not found", literal)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to determine if %s is a directory: %w", localFullPath, err)
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
return "", fmt.Errorf("expected %s to be a directory but found a file", literal)
|
||||||
|
}
|
||||||
|
return localFullPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *translateContext) translateLocalRelativePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
|
func (t *translateContext) translateLocalRelativePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
|
||||||
return localRelPath, nil
|
return localRelPath, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (t *translateContext) artifactRewritePatterns() []artifactRewritePattern {
|
||||||
{
|
{
|
||||||
base.Append(dyn.Key("path")),
|
base.Append(dyn.Key("path")),
|
||||||
translateOptions{
|
translateOptions{
|
||||||
Mode: TranslateModeLocalRelative,
|
Mode: TranslateModeLocalAbsoluteDirectory,
|
||||||
|
|
||||||
// Artifact paths may be outside the sync root.
|
// Artifact paths may be outside the sync root.
|
||||||
// They are the working directory for artifact builds.
|
// They are the working directory for artifact builds.
|
||||||
|
|
|
@ -19,6 +19,8 @@ import (
|
||||||
func TestTranslatePathsArtifacts_InsideSyncRoot(t *testing.T) {
|
func TestTranslatePathsArtifacts_InsideSyncRoot(t *testing.T) {
|
||||||
tmp := t.TempDir()
|
tmp := t.TempDir()
|
||||||
dir := filepath.Join(tmp, "bundle")
|
dir := filepath.Join(tmp, "bundle")
|
||||||
|
lib := filepath.Join(dir, "my_lib")
|
||||||
|
_ = os.MkdirAll(lib, 0o755)
|
||||||
_ = os.MkdirAll(dir, 0o755)
|
_ = os.MkdirAll(dir, 0o755)
|
||||||
|
|
||||||
b := &bundle.Bundle{
|
b := &bundle.Bundle{
|
||||||
|
@ -44,12 +46,14 @@ func TestTranslatePathsArtifacts_InsideSyncRoot(t *testing.T) {
|
||||||
require.NoError(t, diags.Error())
|
require.NoError(t, diags.Error())
|
||||||
|
|
||||||
// Assert that the artifact path has been converted to a path relative to the sync root.
|
// Assert that the artifact path has been converted to a path relative to the sync root.
|
||||||
assert.Equal(t, "my_lib", b.Config.Artifacts["my_artifact"].Path)
|
assert.Equal(t, lib, b.Config.Artifacts["my_artifact"].Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
|
func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
|
||||||
tmp := t.TempDir()
|
tmp := t.TempDir()
|
||||||
|
lib := filepath.Join(tmp, "my_lib")
|
||||||
dir := filepath.Join(tmp, "bundle")
|
dir := filepath.Join(tmp, "bundle")
|
||||||
|
_ = os.MkdirAll(lib, 0o755)
|
||||||
_ = os.MkdirAll(dir, 0o755)
|
_ = os.MkdirAll(dir, 0o755)
|
||||||
|
|
||||||
b := &bundle.Bundle{
|
b := &bundle.Bundle{
|
||||||
|
@ -60,8 +64,8 @@ func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
|
||||||
"my_artifact": {
|
"my_artifact": {
|
||||||
Type: "wheel",
|
Type: "wheel",
|
||||||
|
|
||||||
// Assume this is defined in a subdir to the sync root.
|
// Assume this is defined in a subdir of the bundle root.
|
||||||
Path: "../../common/my_lib",
|
Path: "../../my_lib",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -75,5 +79,5 @@ func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
|
||||||
require.NoError(t, diags.Error())
|
require.NoError(t, diags.Error())
|
||||||
|
|
||||||
// Assert that the artifact path has been converted to a path relative to the sync root.
|
// Assert that the artifact path has been converted to a path relative to the sync root.
|
||||||
assert.Equal(t, "../common/my_lib", b.Config.Artifacts["my_artifact"].Path)
|
assert.Equal(t, lib, b.Config.Artifacts["my_artifact"].Path)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue