mirror of
https://github.com/foomo/gocontemplate.git
synced 2025-10-16 12:35:36 +00:00
feat: add context and directory
This commit is contained in:
parent
f1e134c7ca
commit
59522235bc
@ -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"`
|
||||
}
|
||||
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user