github.com/octohelm/cuemod@v0.9.4/pkg/cueify/golang/extractor.go (about) 1 package golang 2 3 import ( 4 "context" 5 "go/build" 6 "path/filepath" 7 8 cueast "cuelang.org/go/cue/ast" 9 "github.com/octohelm/cuemod/pkg/cueify/core" 10 ) 11 12 func init() { 13 core.Register(&Extractor{}) 14 } 15 16 // Extractor similar to cue go, but just only generate for one import path 17 // 18 // Targets: 19 // 20 // * gen const values 21 // * gen types 22 // * k8s resources with meta_v1.TypeMeta should gen with { apiVersion, kind } 23 // 24 // Rules: 25 // 26 // * skip & drop imports from go std libs exclude cue builtins support. 27 type Extractor struct { 28 } 29 30 func (Extractor) Name() string { 31 return "go" 32 } 33 34 func (Extractor) Detect(ctx context.Context, src string) (bool, map[string]string) { 35 goFiles, err := filepath.Glob(filepath.Join(src, "*.go")) 36 if err == nil { 37 return len(goFiles) > 0, nil 38 } 39 return false, nil 40 } 41 42 func (e *Extractor) Extract(ctx context.Context, src string) ([]*cueast.File, error) { 43 pkg, err := build.ImportDir(src, build.IgnoreVendor) 44 if err != nil { 45 return nil, err 46 } 47 return (&pkgExtractor{Package: pkg}).Extract(ctx) 48 }