Files
gocryptotrader/cmd/gen_sqlboiler_config/main.go
Ryan O'Hara-Reid e99adca86f encoding/json: Add custom JSON package with build tag support for Sonic (#1623)
* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2025-02-20 16:05:55 +11:00

99 lines
2.5 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/repository"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
var (
configFile string
outputFolder string
)
var sqlboilerConfig map[string]driverConfig
type driverConfig struct {
DBName string `json:"dbname,omitempty"`
Host string `json:"host,omitempty"`
Port uint16 `json:"port,omitempty"`
User string `json:"user,omitempty"`
Pass string `json:"pass,omitempty"`
Schema string `json:"schema,omitempty"`
SSLMode string `json:"sslmode,omitempty"`
Blacklist []string `json:"blacklist,omitempty"`
}
func main() {
fmt.Println("GoCryptoTrader SQLBoiler config generation tool")
fmt.Println(core.Copyright)
fmt.Println()
flag.StringVar(&configFile, "config", config.DefaultFilePath(), "config file to load")
flag.StringVar(&outputFolder, "outdir", "", "overwrite default output folder")
flag.Parse()
var cfg config.Config
err := cfg.LoadConfig(configFile, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
convertGCTtoSQLBoilerConfig(&cfg.Database)
jsonOutput, err := json.MarshalIndent(sqlboilerConfig, "", " ")
if err != nil {
fmt.Printf("Marshal failed: %v", err)
os.Exit(1)
}
path := filepath.Join(outputFolder, "sqlboiler.json")
err = os.WriteFile(path, jsonOutput, file.DefaultPermissionOctal)
if err != nil {
fmt.Printf("Write failed: %v", err)
os.Exit(1)
}
fmt.Println("sqlboiler.json file created")
}
func convertGCTtoSQLBoilerConfig(c *database.Config) {
tempConfig := driverConfig{
Blacklist: []string{"goose_db_version"},
}
sqlboilerConfig = make(map[string]driverConfig)
dbType := repository.GetSQLDialect()
if dbType == database.DBPostgreSQL {
dbType = "psql"
}
if dbType == database.DBSQLite || dbType == database.DBSQLite3 {
tempConfig.DBName = getLoadedDBPath()
} else {
tempConfig.User = c.Username
tempConfig.Pass = c.Password
tempConfig.Port = c.Port
tempConfig.Host = c.Host
tempConfig.DBName = c.Database
tempConfig.SSLMode = c.SSLMode
}
sqlboilerConfig[dbType] = tempConfig
}
// getLoadedDBPath gets the path loaded by 'database/drivers/sqlite3'
func getLoadedDBPath() string {
cfg := database.DB.GetConfig()
return filepath.Join(database.DB.DataPath, cfg.Database)
}