Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom config paths #49

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions cmd/pgxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/assets/templates/constants_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 12 additions & 25 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/goconstatnts/goconstatnts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type ISchema interface {
GetSchema(outputDir string) (cmd.GetCatalogResultItem, error)
GetSchema(sqlcConfigPath, outputDir string) (cmd.GetCatalogResultItem, error)
}

type schema struct {
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions internal/sqlc/sqlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlc/cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -59,7 +59,7 @@ func GetCatalogs() (res GetCatalogResult, err error) {
}
}()

dir, filename := getConfigPathCustom(stderr, "")
dir, filename := getConfigPathCustom(stderr, configFilePath)
e := Env{
DryRun: false,
}
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlc/sqlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading