11package template
22
33import (
4+ "bytes"
45 "context"
5- "io"
66 "io/fs"
7- "os"
8- "path/filepath"
97 "slices"
8+
9+ "github.com/databricks/cli/libs/filer"
1010)
1111
1212// Interface representing a file to be materialized from a template into a project
1313// instance
1414type file interface {
15- // Destination path for file. This is where the file will be created when
16- // PersistToDisk is called.
17- DstPath () * destinationPath
15+ // Path of the file relative to the root of the instantiated template.
16+ // This is where the file is written to when persisting the template to disk.
17+ // Must be slash-separated.
18+ RelPath () string
1819
1920 // Write file to disk at the destination path.
20- Write (ctx context.Context ) error
21+ Write (ctx context.Context , out filer. Filer ) error
2122
2223 // contents returns the file contents as a byte slice.
2324 // This is used for testing purposes.
2425 contents () ([]byte , error )
2526}
2627
27- type destinationPath struct {
28- // Root path for the project instance. This path uses the system's default
29- // file separator. For example /foo/bar on Unix and C:\foo\bar on windows
30- root string
31-
32- // Unix like file path relative to the "root" of the instantiated project. Is used to
33- // evaluate whether the file should be skipped by comparing it to a list of
34- // skip glob patterns.
35- relPath string
36- }
37-
38- // Absolute path of the file, in the os native format. For example /foo/bar on
39- // Unix and C:\foo\bar on windows
40- func (f * destinationPath ) absPath () string {
41- return filepath .Join (f .root , filepath .FromSlash (f .relPath ))
42- }
43-
4428type copyFile struct {
45- ctx context.Context
46-
4729 // Permissions bits for the destination file
4830 perm fs.FileMode
4931
50- dstPath * destinationPath
32+ // Destination path for the file.
33+ relPath string
5134
5235 // [fs.FS] rooted at template root. Used to read srcPath.
5336 srcFS fs.FS
@@ -56,55 +39,40 @@ type copyFile struct {
5639 srcPath string
5740}
5841
59- func (f * copyFile ) DstPath () * destinationPath {
60- return f .dstPath
42+ func (f * copyFile ) RelPath () string {
43+ return f .relPath
6144}
6245
63- func (f * copyFile ) Write (ctx context.Context ) error {
64- path := f .DstPath ().absPath ()
65- err := os .MkdirAll (filepath .Dir (path ), 0755 )
66- if err != nil {
67- return err
68- }
69- srcFile , err := f .srcFS .Open (f .srcPath )
70- if err != nil {
71- return err
72- }
73- defer srcFile .Close ()
74- dstFile , err := os .OpenFile (path , os .O_CREATE | os .O_EXCL | os .O_WRONLY , f .perm )
46+ func (f * copyFile ) Write (ctx context.Context , out filer.Filer ) error {
47+ src , err := f .srcFS .Open (f .srcPath )
7548 if err != nil {
7649 return err
7750 }
78- defer dstFile .Close ()
79- _ , err = io .Copy (dstFile , srcFile )
80- return err
51+ defer src .Close ()
52+ return out .Write (ctx , f .relPath , src , filer .CreateParentDirectories , filer .WriteMode (f .perm ))
8153}
8254
8355func (f * copyFile ) contents () ([]byte , error ) {
8456 return fs .ReadFile (f .srcFS , f .srcPath )
8557}
8658
8759type inMemoryFile struct {
88- dstPath * destinationPath
89-
90- content []byte
91-
9260 // Permissions bits for the destination file
9361 perm fs.FileMode
94- }
9562
96- func (f * inMemoryFile ) DstPath () * destinationPath {
97- return f .dstPath
63+ // Destination path for the file.
64+ relPath string
65+
66+ // Contents of the file.
67+ content []byte
9868}
9969
100- func (f * inMemoryFile ) Write (ctx context.Context ) error {
101- path := f .DstPath ().absPath ()
70+ func (f * inMemoryFile ) RelPath () string {
71+ return f .relPath
72+ }
10273
103- err := os .MkdirAll (filepath .Dir (path ), 0755 )
104- if err != nil {
105- return err
106- }
107- return os .WriteFile (path , f .content , f .perm )
74+ func (f * inMemoryFile ) Write (ctx context.Context , out filer.Filer ) error {
75+ return out .Write (ctx , f .relPath , bytes .NewReader (f .content ), filer .CreateParentDirectories , filer .WriteMode (f .perm ))
10876}
10977
11078func (f * inMemoryFile ) contents () ([]byte , error ) {
0 commit comments