-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
fs.FS
interface to read template (#1910)
## Changes While working on the v2 of #1744, I found that: * Template initialization first copies built-in templates to a temporary directory before initializing them * Reading a template's contents goes through a `filer.Filer` but is hardcoded to a local one This change updates the interface for reading templates to be `fs.FS`. This is compatible with the `embed.FS` type for the built-in templates, so they no longer have to be copied to a temporary directory before being used. The alternative is to use a `filer.Filer` throughout, but this would have required even more plumbing, and we don't need to _read_ templates, including notebooks, from the workspace filesystem (yet?). As part of making `template.Materialize` take an `fs.FS` argument, the logic to match a given argument to a particular built-in template in the `init` command has moved to sit next to its implementation. ## Tests Existing tests pass.
- Loading branch information
Showing
15 changed files
with
232 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package template | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
//go:embed all:templates | ||
var builtinTemplates embed.FS | ||
|
||
// BuiltinTemplate represents a template that is built into the CLI. | ||
type BuiltinTemplate struct { | ||
Name string | ||
FS fs.FS | ||
} | ||
|
||
// Builtin returns the list of all built-in templates. | ||
func Builtin() ([]BuiltinTemplate, error) { | ||
templates, err := fs.Sub(builtinTemplates, "templates") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entries, err := fs.ReadDir(templates, ".") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var out []BuiltinTemplate | ||
for _, entry := range entries { | ||
if !entry.IsDir() { | ||
continue | ||
} | ||
|
||
templateFS, err := fs.Sub(templates, entry.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out = append(out, BuiltinTemplate{ | ||
Name: entry.Name(), | ||
FS: templateFS, | ||
}) | ||
} | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package template | ||
|
||
import ( | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBuiltin(t *testing.T) { | ||
out, err := Builtin() | ||
require.NoError(t, err) | ||
assert.Len(t, out, 3) | ||
|
||
// Confirm names. | ||
assert.Equal(t, "dbt-sql", out[0].Name) | ||
assert.Equal(t, "default-python", out[1].Name) | ||
assert.Equal(t, "default-sql", out[2].Name) | ||
|
||
// Confirm that the filesystems work. | ||
_, err = fs.Stat(out[0].FS, `template/{{.project_name}}/dbt_project.yml.tmpl`) | ||
assert.NoError(t, err) | ||
_, err = fs.Stat(out[1].FS, `template/{{.project_name}}/tests/main_test.py.tmpl`) | ||
assert.NoError(t, err) | ||
_, err = fs.Stat(out[2].FS, `template/{{.project_name}}/src/orders_daily.sql.tmpl`) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.