diff --git a/cmd/pgxgen/main.go b/cmd/pgxgen/main.go index d264256..2a9c795 100644 --- a/cmd/pgxgen/main.go +++ b/cmd/pgxgen/main.go @@ -2,9 +2,9 @@ package main import ( "os" - "strings" "github.com/cristalhq/acmd" + "github.com/cristalhq/flagx" "github.com/tkcrm/pgxgen/internal/config" "github.com/tkcrm/pgxgen/internal/crud" "github.com/tkcrm/pgxgen/internal/gomodels" @@ -15,12 +15,26 @@ import ( "github.com/tkcrm/pgxgen/pkg/logger" ) -var version = "v0.3.2" +var ( + appName = "pgxgen" + version = "v0.3.3" +) func main() { logger := logger.New() - cfg, err := config.LoadConfig(version) + // custom config file path + var cf config.Flags + fset := flagx.NewFlagSet(appName, os.Stdout) + fset.String(&cf.PgxgenConfigFilePath, "pgxgen-config", "", "pgxgen.yaml", "absolute or relative path to sqlc.yaml file") + fset.String(&cf.SqlcConfigFilePath, "sqlc-config", "", "sqlc.yaml", "absolute or relative path to pgxgen.yaml file") + + // parse flags + if err := fset.Parse(os.Args[1:]); err != nil { + logger.Fatalf("failed to parse flags: %s", err) + } + + cfg, err := config.LoadConfig(cf, version) if err != nil { logger.Fatalf("load config error: %s", err) } @@ -63,26 +77,10 @@ func main() { AppDescription: "Generate GO models, DB CRUD, Mobx Keystone models and typescript code based on DDL", PostDescription: "pgxgen crud", Version: version, - Args: filterFlags(), + Args: append([]string{os.Args[0]}, fset.Args()...), }) if err := r.Run(); err != nil { logger.Fatalf("error: %s", err) } } - -// filterFlags filter os.Args from `pgxgen-config` and `sqlc-config` flags -func filterFlags() []string { - res := []string{} - args := os.Args - for i := 0; i < len(args); i++ { - if strings.Contains(args[i], "pgxgen-config") || - strings.Contains(args[i], "sqlc-config") { - i++ - continue - } - res = append(res, args[i]) - } - - return res -} diff --git a/go.mod b/go.mod index 768354d..795968b 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( ) require ( - github.com/a-h/templ v0.2.663 + github.com/a-h/templ v0.2.680 github.com/antlr4-go/antlr/v4 v4.13.0 github.com/cristalhq/flagx v0.5.0 github.com/fatih/structtag v1.2.0 diff --git a/go.sum b/go.sum index 0e8b106..b0cd4a2 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/a-h/templ v0.2.663 h1:aa0WMm27InkYHGjimcM7us6hJ6BLhg98ZbfaiDPyjHE= github.com/a-h/templ v0.2.663/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/a-h/templ v0.2.680 h1:TflYFucxp5rmOxAXB9Xy3+QHTk8s8xG9+nCT/cLzjeE= +github.com/a-h/templ v0.2.680/go.mod h1:NQGQOycaPKBxRB14DmAaeIpcGC1AOBPJEMO4ozS7m90= github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI= github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g= github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= diff --git a/internal/assets/templates/constants_templ.go b/internal/assets/templates/constants_templ.go index f3f8217..83586ec 100644 --- a/internal/assets/templates/constants_templ.go +++ b/internal/assets/templates/constants_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.663 +// templ: version: v0.2.680 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/internal/config/config.go b/internal/config/config.go index 1de1366..4c0b121 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,49 +1,36 @@ package config import ( - "flag" "fmt" "os" "path/filepath" - "github.com/cristalhq/flagx" "github.com/tkcrm/pgxgen/pkg/logger" "gopkg.in/yaml.v2" ) type Config struct { - Sqlc Sqlc - Pgxgen Pgxgen - loadErrs []error + ConfigPaths Flags + Sqlc Sqlc + Pgxgen Pgxgen + loadErrs []error } -type configFlags struct { - sqlcConfigFilePath string - pgxgenConfigFilePath string -} - -func (c *configFlags) Flags() *flag.FlagSet { - fset := flagx.NewFlagSet("pgxgen config", os.Stderr) - fset.String(&c.sqlcConfigFilePath, "sqlc-config", "", "sqlc.yaml", "absolute or relative path to sqlc.yaml file") - fset.String(&c.pgxgenConfigFilePath, "pgxgen-config", "", "pgxgen.yaml", "absolute or relative path to pgxgen.yaml file") - return fset.AsStdlib() +type Flags struct { + SqlcConfigFilePath string + PgxgenConfigFilePath string } // LoadConfig return common config with sqlc and pgxgen data -func LoadConfig(version string) (Config, error) { +func LoadConfig(cf Flags, version string) (Config, error) { cfg := Config{ - loadErrs: make([]error, 0), - } - - // parse config flags - var cf configFlags - if err := cf.Flags().Parse(os.Args[1:]); err != nil { - return cfg, fmt.Errorf("failed to parse flags: %w", err) + ConfigPaths: cf, + loadErrs: make([]error, 0), } // load sqlc config var sqlcConfig Sqlc - sqlcConfigFile, err := os.ReadFile(cf.sqlcConfigFilePath) + sqlcConfigFile, err := os.ReadFile(cf.SqlcConfigFilePath) if err != nil { cfg.loadErrs = append(cfg.loadErrs, fmt.Errorf("failed to read sqlc config file: %w", err)) } else { @@ -65,7 +52,7 @@ func LoadConfig(version string) (Config, error) { // load pgxgen config var pgxgenConfig Pgxgen - pgxgenConfigFile, err := os.ReadFile(cf.pgxgenConfigFilePath) + pgxgenConfigFile, err := os.ReadFile(cf.PgxgenConfigFilePath) if err != nil { cfg.loadErrs = append(cfg.loadErrs, fmt.Errorf("failed to read pgxgen config file: %w", err)) } else { diff --git a/internal/crud/crud.go b/internal/crud/crud.go index 54feddf..67a946a 100644 --- a/internal/crud/crud.go +++ b/internal/crud/crud.go @@ -54,7 +54,7 @@ func (s *crud) Generate(_ context.Context, args []string) error { } // get catalogs - allCatalogs, err := sqlc.GetCatalogs() + allCatalogs, err := sqlc.GetCatalogs(s.config.ConfigPaths.SqlcConfigFilePath) if err != nil { return err } @@ -138,7 +138,7 @@ func (s *crud) generateSQLForEachTable(crudParams config.CrudParams, outputPaths // Get all tables from postgres tablesData, err := s.getTableMeta(outPath) if err != nil { - return nil, err + return nil, fmt.Errorf("getTableMeta error: %w", err) } // headText := fmt.Sprintf("-- Code generated by pgxgen. DO NOT EDIT.\n-- versions:\n-- pgxgen %s\n\n", s.config.Pgxgen.Version) diff --git a/internal/goconstatnts/goconstatnts.go b/internal/goconstatnts/goconstatnts.go index dd62278..123751f 100644 --- a/internal/goconstatnts/goconstatnts.go +++ b/internal/goconstatnts/goconstatnts.go @@ -70,7 +70,7 @@ func (s *goConstants) GenerateConstants() error { return fmt.Errorf("can not find output dir for path: %s", table.OutputDir) } - catalog, err := s.schema.GetSchema(schemaDir) + catalog, err := s.schema.GetSchema(s.config.ConfigPaths.SqlcConfigFilePath, schemaDir) if err != nil { return err } diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 03a1972..f594592 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -6,7 +6,7 @@ import ( ) type ISchema interface { - GetSchema(outputDir string) (cmd.GetCatalogResultItem, error) + GetSchema(sqlcConfigPath, outputDir string) (cmd.GetCatalogResultItem, error) } type schema struct { @@ -20,12 +20,12 @@ func New() ISchema { } } -func (s *schema) GetSchema(schemaDir string) (cmd.GetCatalogResultItem, error) { +func (s *schema) GetSchema(sqlcConfigPath, schemaDir string) (cmd.GetCatalogResultItem, error) { if item, ok := s.catalogs[schemaDir]; ok { return item, nil } - res, err := sqlc.GetCatalogBySchemaDir(schemaDir) + res, err := sqlc.GetCatalogBySchemaDir(sqlcConfigPath, schemaDir) if err != nil { return cmd.GetCatalogResultItem{}, err } diff --git a/internal/sqlc/sqlc.go b/internal/sqlc/sqlc.go index b9b8904..e37a234 100644 --- a/internal/sqlc/sqlc.go +++ b/internal/sqlc/sqlc.go @@ -43,6 +43,8 @@ func (s *sqlc) Generate(_ context.Context, args []string) error { func (s *sqlc) process(args []string) error { timeStart := time.Now() + args = append(args, "-f", s.config.ConfigPaths.SqlcConfigFilePath) + // generate sqlc code genResult := sqlcpkg.Run(args) if genResult != 0 { diff --git a/pkg/sqlc/cmd/catalog.go b/pkg/sqlc/cmd/catalog.go index 4b7d2a5..7a42b17 100755 --- a/pkg/sqlc/cmd/catalog.go +++ b/pkg/sqlc/cmd/catalog.go @@ -48,7 +48,7 @@ func getConfigPathCustom(stderr io.Writer, filePath string) (string, string) { } } -func GetCatalogs() (res GetCatalogResult, err error) { +func GetCatalogs(configFilePath string) (res GetCatalogResult, err error) { // define variables var b bytes.Buffer stderr := bufio.NewWriter(&b) @@ -59,7 +59,7 @@ func GetCatalogs() (res GetCatalogResult, err error) { } }() - dir, filename := getConfigPathCustom(stderr, "") + dir, filename := getConfigPathCustom(stderr, configFilePath) e := Env{ DryRun: false, } @@ -193,9 +193,9 @@ func GetCatalogByOutputDir(catalogs GetCatalogResult, outputDir string) (GetCata return item, nil } -func GetCatalogBySchemaDir(schemaDir string) (GetCatalogResultItem, error) { +func GetCatalogBySchemaDir(configFilePath, schemaDir string) (GetCatalogResultItem, error) { res := GetCatalogResultItem{} - catalogs, err := GetCatalogs() + catalogs, err := GetCatalogs(configFilePath) if err != nil { return res, fmt.Errorf("get catalogs error: %w", err) } diff --git a/pkg/sqlc/sqlc.go b/pkg/sqlc/sqlc.go index 51dcbef..79e2156 100644 --- a/pkg/sqlc/sqlc.go +++ b/pkg/sqlc/sqlc.go @@ -10,14 +10,14 @@ func Run(args []string) int { return cmd.Do(args, os.Stdin, os.Stdout, os.Stderr) } -func GetCatalogs() (cmd.GetCatalogResult, error) { - return cmd.GetCatalogs() +func GetCatalogs(configFilePath string) (cmd.GetCatalogResult, error) { + return cmd.GetCatalogs(configFilePath) } func GetCatalogByOutputDir(catalogs cmd.GetCatalogResult, outputDir string) (cmd.GetCatalogResultItem, error) { return cmd.GetCatalogByOutputDir(catalogs, outputDir) } -func GetCatalogBySchemaDir(outputDir string) (cmd.GetCatalogResultItem, error) { - return cmd.GetCatalogBySchemaDir(outputDir) +func GetCatalogBySchemaDir(configFilePath, outputDir string) (cmd.GetCatalogResultItem, error) { + return cmd.GetCatalogBySchemaDir(configFilePath, outputDir) }