databricks-cli/libs/template/reader.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 lines
3.6 KiB
Go
Raw Normal View History

2025-01-03 10:58:35 +00:00
package template
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/databricks/cli/libs/cmdio"
)
type Reader interface {
// FS returns a file system that contains the template
// definition files. This function is NOT thread safe.
FS(ctx context.Context) (fs.FS, error)
// Close releases any resources associated with the reader
// like cleaning up temporary directories.
Close() error
}
type builtinReader struct {
2025-01-03 12:59:50 +00:00
name string
2025-01-03 10:58:35 +00:00
fsCached fs.FS
}
func (r *builtinReader) FS(ctx context.Context) (fs.FS, error) {
// If the FS has already been loaded, return it.
if r.fsCached != nil {
return r.fsCached, nil
}
2025-01-03 12:59:50 +00:00
builtin, err := builtin()
2025-01-03 10:58:35 +00:00
if err != nil {
return nil, err
}
var templateFS fs.FS
for _, entry := range builtin {
2025-01-03 12:59:50 +00:00
if entry.Name == r.name {
2025-01-03 10:58:35 +00:00
templateFS = entry.FS
break
}
}
2025-01-03 12:59:50 +00:00
if templateFS == nil {
return nil, fmt.Errorf("builtin template %s not found", r.name)
}
2025-01-03 10:58:35 +00:00
r.fsCached = templateFS
return r.fsCached, nil
}
func (r *builtinReader) Close() error {
return nil
}
type gitReader struct {
gitUrl string
// tag or branch to checkout
ref string
// subdirectory within the repository that contains the template
templateDir string
// temporary directory where the repository is cloned
tmpRepoDir string
2025-01-03 12:59:50 +00:00
// Function to clone the repository. This is a function pointer to allow
// mocking in tests.
cloneFunc func(ctx context.Context, url, reference, targetPath string) error
2025-01-03 10:58:35 +00:00
fsCached fs.FS
}
// Computes the repo name from the repo URL. Treats the last non empty word
// when splitting at '/' as the repo name. For example: for url git@github.com:databricks/cli.git
// the name would be "cli.git"
func repoName(url string) string {
parts := strings.Split(strings.TrimRight(url, "/"), "/")
return parts[len(parts)-1]
}
var gitUrlPrefixes = []string{
"https://",
"git@",
}
2025-01-03 12:59:50 +00:00
func isRepoUrl(url string) bool {
2025-01-03 10:58:35 +00:00
result := false
for _, prefix := range gitUrlPrefixes {
if strings.HasPrefix(url, prefix) {
result = true
break
}
}
return result
}
func (r *gitReader) FS(ctx context.Context) (fs.FS, error) {
// If the FS has already been loaded, return it.
if r.fsCached != nil {
return r.fsCached, nil
}
// Create a temporary directory with the name of the repository. The '*'
// character is replaced by a random string in the generated temporary directory.
repoDir, err := os.MkdirTemp("", repoName(r.gitUrl)+"-*")
if err != nil {
return nil, err
}
r.tmpRepoDir = repoDir
// start the spinner
promptSpinner := cmdio.Spinner(ctx)
promptSpinner <- "Downloading the template\n"
2025-01-03 12:59:50 +00:00
err = r.cloneFunc(ctx, r.gitUrl, r.ref, repoDir)
2025-01-03 10:58:35 +00:00
close(promptSpinner)
if err != nil {
return nil, err
}
r.fsCached = os.DirFS(filepath.Join(repoDir, r.templateDir))
return r.fsCached, nil
}
func (r *gitReader) Close() error {
if r.tmpRepoDir == "" {
return nil
}
return os.RemoveAll(r.tmpRepoDir)
}
type localReader struct {
// Path on the local filesystem that contains the template
path string
fsCached fs.FS
}
func (r *localReader) FS(ctx context.Context) (fs.FS, error) {
// If the FS has already been loaded, return it.
if r.fsCached != nil {
return r.fsCached, nil
}
r.fsCached = os.DirFS(r.path)
return r.fsCached, nil
}
func (r *localReader) Close() error {
return nil
}
type failReader struct{}
func (r *failReader) FS(ctx context.Context) (fs.FS, error) {
return nil, fmt.Errorf("this is a placeholder reader that always fails. Please configure a real reader.")
}
func (r *failReader) Close() error {
return fmt.Errorf("this is a placeholder reader that always fails. Please configure a real reader.")
}