feat: add context and directory

This commit is contained in:
Kevin Franklin Kim 2025-03-11 12:25:56 +01:00
parent f1e134c7ca
commit 59522235bc
No known key found for this signature in database
3 changed files with 10 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package contemplate
type Config struct {
// Directory containing the go.mod file
Directory string `json:"directory" yaml:"directory"`
// Packages to load
Packages []*PackageConfig `json:"packages" yaml:"packages"`
}

View File

@ -10,7 +10,7 @@ import (
func TestNewLoader(t *testing.T) {
t.Parallel()
ctpl, err := contemplate.Load(&contemplate.Config{
ctpl, err := contemplate.Load(t.Context(), &contemplate.Config{
Packages: []*contemplate.PackageConfig{
{
Path: "github.com/foomo/gocontemplate/test/event",
@ -25,7 +25,7 @@ func TestNewLoader(t *testing.T) {
func TestLoader_LookupTypesByType(t *testing.T) {
t.Parallel()
ctpl, err := contemplate.Load(&contemplate.Config{
ctpl, err := contemplate.Load(t.Context(), &contemplate.Config{
Packages: []*contemplate.PackageConfig{
{
Path: "github.com/foomo/gocontemplate/test/event",

View File

@ -1,10 +1,12 @@
package contemplate
import (
"context"
"golang.org/x/tools/go/packages"
)
func Load(cfg *Config) (*Contemplate, error) {
func Load(ctx context.Context, cfg *Config) (*Contemplate, error) {
inst := &Contemplate{
cfg: cfg,
Packages: map[string]*Package{},
@ -12,6 +14,8 @@ func Load(cfg *Config) (*Contemplate, error) {
// load packages
pkgs, err := packages.Load(&packages.Config{
Context: ctx,
Dir: cfg.Directory,
Mode: packages.NeedName | packages.NeedTypesInfo |
packages.NeedFiles | packages.NeedImports | packages.NeedDeps |
packages.NeedModule | packages.NeedTypes | packages.NeedSyntax,