feat: split config

This commit is contained in:
Kevin Franklin Kim 2024-03-08 23:31:23 +01:00
parent 604aadb998
commit ea8cd02961
No known key found for this signature in database
5 changed files with 20 additions and 11 deletions

View File

@ -16,8 +16,15 @@ google:
credentials_file: ./tmp/google_service_account_creds.json credentials_file: ./tmp/google_service_account_creds.json
events: typescript:
packages: packages:
- path: "github.com/foomo/sesamy-cli/_example/server" - path: "github.com/foomo/sesamy-cli/_example/server"
output_path: "./_example/client/types.d.ts" output_path: "./_example/client/types.d.ts"
indent: "\t" indent: "\t"
tagmanager:
packages:
- path: "github.com/foomo/sesamy-cli/_example/server"
output_path: "./_example/client/types.d.ts"
exclude_files:
- item.go

View File

@ -21,7 +21,7 @@ var tagmanagerWebCmd = &cobra.Command{
clientCredentialsOption = option.WithCredentialsJSON([]byte(cfg.Google.CredentialsJSON)) clientCredentialsOption = option.WithCredentialsJSON([]byte(cfg.Google.CredentialsJSON))
} }
eventParameters, err := internal.GetEventParameters(cfg) eventParameters, err := internal.GetEventParameters(cfg.Tagmanager)
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,9 +12,9 @@ var typescriptCmd = &cobra.Command{
PersistentPreRunE: preRunReadConfig, PersistentPreRunE: preRunReadConfig,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
gen := tygo.New(&tygo.Config{ gen := tygo.New(&tygo.Config{
Packages: cfg.Events.Packages, Packages: cfg.Typescript.Packages,
}) })
for k, v := range cfg.Events.TypeMappings { for k, v := range cfg.Typescript.TypeMappings {
gen.SetTypeMapping(k, v) gen.SetTypeMapping(k, v)
} }

View File

@ -13,12 +13,12 @@ import (
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
) )
func GetEventParameters(cfg *config.Config) (map[string][]string, error) { func GetEventParameters(source config.Source) (map[string][]string, error) {
ret := map[string][]string{} ret := map[string][]string{}
pkgs, err := packages.Load(&packages.Config{ pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax | packages.NeedFiles, Mode: packages.NeedSyntax | packages.NeedFiles,
}, cfg.Events.PackageNames()...) }, source.PackageNames()...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -32,7 +32,7 @@ func GetEventParameters(cfg *config.Config) (map[string][]string, error) {
return nil, fmt.Errorf("no input go files for package index %d", i) return nil, fmt.Errorf("no input go files for package index %d", i)
} }
conf := cfg.Events.PackageConfig(pkg.ID) conf := source.PackageConfig(pkg.ID)
for i, file := range pkg.Syntax { for i, file := range pkg.Syntax {
if conf.IsFileIgnored(pkg.GoFiles[i]) { if conf.IsFileIgnored(pkg.GoFiles[i]) {

View File

@ -7,7 +7,9 @@ import (
type Config struct { type Config struct {
Google Google `yaml:"google"` Google Google `yaml:"google"`
// https://github.com/gzuidhof/tygo // https://github.com/gzuidhof/tygo
Events Events `yaml:"events"` Typescript Source `yaml:"typescript"`
// https://github.com/gzuidhof/tygo
Tagmanager Source `yaml:"tagmanager"`
} }
type Google struct { type Google struct {
@ -27,12 +29,12 @@ type GTM struct {
Server Container `yaml:"server"` Server Container `yaml:"server"`
} }
type Events struct { type Source struct {
Packages []*tygo.PackageConfig `yaml:"packages"` Packages []*tygo.PackageConfig `yaml:"packages"`
TypeMappings map[string]string `yaml:"type_mappings"` TypeMappings map[string]string `yaml:"type_mappings"`
} }
func (e Events) PackageNames() []string { func (e Source) PackageNames() []string {
ret := make([]string, len(e.Packages)) ret := make([]string, len(e.Packages))
for i, value := range e.Packages { for i, value := range e.Packages {
ret[i] = value.Path ret[i] = value.Path
@ -40,7 +42,7 @@ func (e Events) PackageNames() []string {
return ret return ret
} }
func (e Events) PackageConfig(path string) *tygo.PackageConfig { func (e Source) PackageConfig(path string) *tygo.PackageConfig {
for _, value := range e.Packages { for _, value := range e.Packages {
if value.Path == path { if value.Path == path {
return value return value