Rename modes to more accurately capture what they do

This commit is contained in:
Pieter Noordhuis 2025-01-13 11:14:31 +01:00
parent 246e10c8c8
commit 70a0dd8743
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
4 changed files with 23 additions and 19 deletions

View File

@ -30,14 +30,18 @@ const (
// TranslateModeDirectory translates a path to a remote directory. // TranslateModeDirectory translates a path to a remote directory.
TranslateModeDirectory TranslateModeDirectory
// TranslateModeRetainLocalAbsoluteFilePath translates a path to the local absolute file path. // TranslateModeLocalAbsoluteFile translates a path to the local absolute file path.
TranslateModeRetainLocalAbsoluteFilePath // It returns an error if the path does not exist or is a directory.
TranslateModeLocalAbsoluteFile
// TranslateModeNoOp does not translate the path. // TranslateModeLocalRelative translates a path to be relative to the bundle sync root path.
TranslateModeNoOp // It does not check if the path exists, nor care if it is a file or directory.
TranslateModeLocalRelative
// TranslateModeNoOpWithPrefix does not translate the path, but adds a prefix to it. // TranslateModeLocalRelativeWithPrefix translates a path to be relative to the bundle sync root path.
TranslateModeNoOpWithPrefix // It a "./" prefix to the path if it does not already have one.
// This allows for disambiguating between paths and PyPI package names.
TranslateModeLocalRelativeWithPrefix
) )
// translateOptions specifies how a path should be translated. // translateOptions specifies how a path should be translated.
@ -146,12 +150,12 @@ func (t *translateContext) rewritePath(
interp, err = t.translateFilePath(ctx, input, localPath, localRelPath) interp, err = t.translateFilePath(ctx, input, localPath, localRelPath)
case TranslateModeDirectory: case TranslateModeDirectory:
interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath) interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath)
case TranslateModeRetainLocalAbsoluteFilePath: case TranslateModeLocalAbsoluteFile:
interp, err = t.retainLocalAbsoluteFilePath(ctx, input, localPath, localRelPath) interp, err = t.translateLocalAbsoluteFilePath(ctx, input, localPath, localRelPath)
case TranslateModeNoOp: case TranslateModeLocalRelative:
interp, err = t.translateNoOp(ctx, input, localPath, localRelPath) interp, err = t.translateLocalRelativePath(ctx, input, localPath, localRelPath)
case TranslateModeNoOpWithPrefix: case TranslateModeLocalRelativeWithPrefix:
interp, err = t.translateNoOpWithPrefix(ctx, input, localPath, localRelPath) interp, err = t.translateLocalRelativeWithPrefixPath(ctx, input, localPath, localRelPath)
default: default:
return "", fmt.Errorf("unsupported translate mode: %d", opts.Mode) return "", fmt.Errorf("unsupported translate mode: %d", opts.Mode)
} }
@ -231,7 +235,7 @@ func (t *translateContext) translateDirectoryPath(ctx context.Context, literal,
return path.Join(t.remoteRoot, filepath.ToSlash(localRelPath)), nil return path.Join(t.remoteRoot, filepath.ToSlash(localRelPath)), nil
} }
func (t *translateContext) retainLocalAbsoluteFilePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) { func (t *translateContext) translateLocalAbsoluteFilePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
info, err := t.b.SyncRoot.Stat(filepath.ToSlash(localRelPath)) info, err := t.b.SyncRoot.Stat(filepath.ToSlash(localRelPath))
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("file %s not found", literal) return "", fmt.Errorf("file %s not found", literal)
@ -245,11 +249,11 @@ func (t *translateContext) retainLocalAbsoluteFilePath(ctx context.Context, lite
return localFullPath, nil return localFullPath, nil
} }
func (t *translateContext) translateNoOp(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
} }
func (t *translateContext) translateNoOpWithPrefix(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) { func (t *translateContext) translateLocalRelativeWithPrefixPath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
if !strings.HasPrefix(localRelPath, ".") { if !strings.HasPrefix(localRelPath, ".") {
localRelPath = "." + string(filepath.Separator) + localRelPath localRelPath = "." + string(filepath.Separator) + localRelPath
} }

View File

@ -23,7 +23,7 @@ func (t *translateContext) artifactRewritePatterns() []artifactRewritePattern {
return []artifactRewritePattern{ return []artifactRewritePattern{
{ {
base.Append(dyn.Key("path")), base.Append(dyn.Key("path")),
translateOptions{Mode: TranslateModeNoOp}, translateOptions{Mode: TranslateModeLocalRelative},
}, },
} }
} }

View File

@ -18,7 +18,7 @@ func (t *translateContext) applyDashboardTranslations(ctx context.Context, v dyn
) )
opts := translateOptions{ opts := translateOptions{
Mode: TranslateModeRetainLocalAbsoluteFilePath, Mode: TranslateModeLocalAbsoluteFile,
} }
return dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { return dyn.MapByPattern(v, pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {

View File

@ -71,7 +71,7 @@ func (t *translateContext) applyJobTranslations(ctx context.Context, v dyn.Value
func getJobTranslateMode(kind paths.PathKind) (TranslateMode, error) { func getJobTranslateMode(kind paths.PathKind) (TranslateMode, error) {
switch kind { switch kind {
case paths.PathKindLibrary: case paths.PathKindLibrary:
return TranslateModeNoOp, nil return TranslateModeLocalRelative, nil
case paths.PathKindNotebook: case paths.PathKindNotebook:
return TranslateModeNotebook, nil return TranslateModeNotebook, nil
case paths.PathKindWorkspaceFile: case paths.PathKindWorkspaceFile:
@ -79,7 +79,7 @@ func getJobTranslateMode(kind paths.PathKind) (TranslateMode, error) {
case paths.PathKindDirectory: case paths.PathKindDirectory:
return TranslateModeDirectory, nil return TranslateModeDirectory, nil
case paths.PathKindWithPrefix: case paths.PathKindWithPrefix:
return TranslateModeNoOpWithPrefix, nil return TranslateModeLocalRelativeWithPrefix, nil
} }
return TranslateMode(0), fmt.Errorf("unsupported path kind: %d", kind) return TranslateMode(0), fmt.Errorf("unsupported path kind: %d", kind)