2023-08-01 13:43:27 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
2024-11-20 10:11:31 +00:00
|
|
|
"bytes"
|
2023-08-01 13:43:27 +00:00
|
|
|
"context"
|
|
|
|
"io/fs"
|
2024-11-20 09:28:35 +00:00
|
|
|
"slices"
|
2024-11-20 10:11:31 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
2023-08-01 13:43:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interface representing a file to be materialized from a template into a project
|
|
|
|
// instance
|
|
|
|
type file interface {
|
2024-11-20 10:11:31 +00:00
|
|
|
// Path of the file relative to the root of the instantiated template.
|
|
|
|
// This is where the file is written to when persisting the template to disk.
|
|
|
|
// Must be slash-separated.
|
|
|
|
RelPath() string
|
2023-08-01 13:43:27 +00:00
|
|
|
|
|
|
|
// Write file to disk at the destination path.
|
2024-11-20 10:11:31 +00:00
|
|
|
Write(ctx context.Context, out filer.Filer) error
|
2024-11-20 09:28:35 +00:00
|
|
|
|
|
|
|
// contents returns the file contents as a byte slice.
|
|
|
|
// This is used for testing purposes.
|
|
|
|
contents() ([]byte, error)
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type copyFile struct {
|
|
|
|
// Permissions bits for the destination file
|
|
|
|
perm fs.FileMode
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
// Destination path for the file.
|
|
|
|
relPath string
|
2023-08-01 13:43:27 +00:00
|
|
|
|
2024-11-20 09:28:35 +00:00
|
|
|
// [fs.FS] rooted at template root. Used to read srcPath.
|
|
|
|
srcFS fs.FS
|
2023-08-01 13:43:27 +00:00
|
|
|
|
|
|
|
// Relative path from template root for file to be copied.
|
|
|
|
srcPath string
|
|
|
|
}
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func (f *copyFile) RelPath() string {
|
|
|
|
return f.relPath
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func (f *copyFile) Write(ctx context.Context, out filer.Filer) error {
|
|
|
|
src, err := f.srcFS.Open(f.srcPath)
|
2023-08-01 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-20 10:11:31 +00:00
|
|
|
defer src.Close()
|
|
|
|
return out.Write(ctx, f.relPath, src, filer.CreateParentDirectories, filer.WriteMode(f.perm))
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2024-11-20 09:28:35 +00:00
|
|
|
func (f *copyFile) contents() ([]byte, error) {
|
|
|
|
return fs.ReadFile(f.srcFS, f.srcPath)
|
|
|
|
}
|
|
|
|
|
2023-08-01 13:43:27 +00:00
|
|
|
type inMemoryFile struct {
|
|
|
|
// Permissions bits for the destination file
|
|
|
|
perm fs.FileMode
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
// Destination path for the file.
|
|
|
|
relPath string
|
|
|
|
|
|
|
|
// Contents of the file.
|
|
|
|
content []byte
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func (f *inMemoryFile) RelPath() string {
|
|
|
|
return f.relPath
|
|
|
|
}
|
2023-08-01 13:43:27 +00:00
|
|
|
|
2024-11-20 10:11:31 +00:00
|
|
|
func (f *inMemoryFile) Write(ctx context.Context, out filer.Filer) error {
|
|
|
|
return out.Write(ctx, f.relPath, bytes.NewReader(f.content), filer.CreateParentDirectories, filer.WriteMode(f.perm))
|
2023-08-01 13:43:27 +00:00
|
|
|
}
|
2024-11-20 09:28:35 +00:00
|
|
|
|
|
|
|
func (f *inMemoryFile) contents() ([]byte, error) {
|
|
|
|
return slices.Clone(f.content), nil
|
|
|
|
}
|