Skip to content

Commit

Permalink
feat: added project backends to load context to support HasBackend() …
Browse files Browse the repository at this point in the history
…func

# Conflicts:
#	internal/pkg/service/cli/cmd/template/test/create/create_test_template_test.go
  • Loading branch information
hosekpeter committed Dec 2, 2024
1 parent ec4ca58 commit 05efd37
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestAskCreateTemplateTestInteractive(t *testing.T) {
Versions: []repository.VersionRecord{versionRec},
}

tmpl, err := template.New(context.Background(), tmplRef, tmplRec, versionRec, fs, fs, "", testapi.MockedComponentsMap())
tmpl, err := template.New(context.Background(), tmplRef, tmplRec, versionRec, fs, fs, testapi.MockedComponentsMap(), template.Option{})
require.NoError(t, err)

// Interaction
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/service/cli/dependencies/cmdlocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,13 @@ func (v *localCommandScope) TemplateForTests(ctx context.Context, reference mode
}
}

o := template.Option{
ProjectPath: path,
ProjectBackend: v.projectBackends,
}

// Load template
return loadTemplateOp.Run(ctx, v, repo, reference, path)
return loadTemplateOp.Run(ctx, v, repo, reference, o)
}

func (v *localCommandScope) LocalProject(ctx context.Context, ignoreErrors bool) (*projectPkg.Project, bool, error) {
Expand Down
15 changes: 9 additions & 6 deletions internal/pkg/template/context/load/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (
// Context represents the process of template loading from the filesystem.
type Context struct {
_context
components *model.ComponentsMap
jsonnetCtx *jsonnet.Context
components *model.ComponentsMap
jsonnetCtx *jsonnet.Context
projectBackends []string
}

type _context context.Context

func NewContext(ctx context.Context, objectsRoot filesystem.Fs, components *model.ComponentsMap) *Context {
func NewContext(ctx context.Context, objectsRoot filesystem.Fs, components *model.ComponentsMap, projectBackends []string) *Context {
c := &Context{
_context: ctx,
components: components,
jsonnetCtx: jsonnet.NewContext().WithCtx(ctx).WithImporter(fsimporter.New(objectsRoot)),
_context: ctx,
components: components,
jsonnetCtx: jsonnet.NewContext().WithCtx(ctx).WithImporter(fsimporter.New(objectsRoot)),
projectBackends: projectBackends,
}

// Register Jsonnet functions
Expand All @@ -40,4 +42,5 @@ func (c *Context) JsonnetContext() *jsonnet.Context {
func (c *Context) registerJsonnetFunctions() {
c.jsonnetCtx.NativeFunctionWithAlias(function.ComponentIsAvailable(c.components))
c.jsonnetCtx.NativeFunctionWithAlias(function.SnowflakeWriterComponentID(c.components))
c.jsonnetCtx.NativeFunctionWithAlias(function.HasProjectBackend(c.projectBackends))
}
2 changes: 1 addition & 1 deletion internal/pkg/template/repository/manager/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (r *CachedRepository) Template(ctx context.Context, reference model.Templat

// Load template
r.d.Logger().Infof(ctx, r.repo.Fs().BasePath())
tmpl, err := loadTemplateOp.Run(ctx, r.d, r.repo, reference, "")
tmpl, err := loadTemplateOp.Run(ctx, r.d, r.repo, reference, template.Option{})
if err != nil {
return nil, errors.Errorf(`cannot load template "%s": %w`, reference.FullName(), err)
}
Expand Down
11 changes: 8 additions & 3 deletions internal/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ type Test struct {
fs filesystem.Fs
}

type Option struct {
ProjectBackend []string
ProjectPath string
}

type CreatedTest struct {
*Test
}

func New(ctx context.Context, reference model.TemplateRef, template repository.TemplateRecord, version repository.VersionRecord, templateDir, commonDir filesystem.Fs, projectsFilePath string, components *model.ComponentsMap) (*Template, error) {
func New(ctx context.Context, reference model.TemplateRef, template repository.TemplateRecord, version repository.VersionRecord, templateDir, commonDir filesystem.Fs, components *model.ComponentsMap, o Option) (*Template, error) {
// Mount <common> directory to:
// template dir FS - used to load manifest, inputs, readme
// src dir FS - objects root
Expand All @@ -161,10 +166,10 @@ func New(ctx context.Context, reference model.TemplateRef, template repository.T
}

// Create struct
out := &Template{_reference: reference, template: template, version: version, fs: templateDir, srcDir: srcDir, projectsFilePath: projectsFilePath}
out := &Template{_reference: reference, template: template, version: version, fs: templateDir, srcDir: srcDir, projectsFilePath: o.ProjectPath}

// Create load context
loadCtx := load.NewContext(ctx, srcDir, components)
loadCtx := load.NewContext(ctx, srcDir, components, o.ProjectBackend)

// Load manifest
out.manifestFile, err = LoadManifest(ctx, templateDir)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/operation/template/load/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (e DeprecatedTemplateError) StatusCode() int {
return http.StatusBadRequest
}

func Run(ctx context.Context, d dependencies, repository *repository.Repository, reference model.TemplateRef, projectsFilePath string) (tmpl *template.Template, err error) {
func Run(ctx context.Context, d dependencies, repository *repository.Repository, reference model.TemplateRef, o template.Option) (tmpl *template.Template, err error) {
ctx, span := d.Telemetry().Tracer().Start(ctx, "keboola.go.operation.template.load")
defer span.End(&err)

Expand Down Expand Up @@ -75,5 +75,5 @@ func Run(ctx context.Context, d dependencies, repository *repository.Repository,
reference = model.NewTemplateRef(reference.Repository(), reference.TemplateID(), versionRecord.Version.String())

// Load template
return template.New(ctx, reference, templateRecord, versionRecord, templateDir, repository.CommonDir(), projectsFilePath, d.Components())
return template.New(ctx, reference, templateRecord, versionRecord, templateDir, repository.CommonDir(), d.Components(), o)
}

0 comments on commit 05efd37

Please sign in to comment.