From aa89ba3b420502b236f6716952e0cad342e7a3f9 Mon Sep 17 00:00:00 2001 From: pharr117 Date: Sun, 21 Apr 2024 21:01:02 -0400 Subject: [PATCH] Publicize more indexer funcs for utils --- cmd/index.go | 4 ++-- cmd/root.go | 8 ++++++-- db/utils.go | 8 ++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/index.go b/cmd/index.go index 88e72121..0ce8c661 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -48,7 +48,7 @@ func GetBuiltinIndexer() *indexerPackage.Indexer { // setupIndex loads the configuration from file and command line flags, validates the configuration, and sets up the logger and database connection. func setupIndex(cmd *cobra.Command, args []string) error { - bindFlags(cmd, viperConf) + BindFlags(cmd, viperConf) err := indexer.Config.Validate() if err != nil { @@ -70,7 +70,7 @@ func setupIndex(cmd *cobra.Command, args []string) error { // If DB has not been preset, connect to the database and migrate using the default configuration settings if indexer.DB == nil { - db, err := connectToDBAndMigrate(indexer.Config.Database) + db, err := ConnectToDBAndMigrate(indexer.Config.Database) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) } diff --git a/cmd/root.go b/cmd/root.go index fb512d41..327e11ef 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -89,8 +89,12 @@ func getViperConfig() { viperConf = v } +func GetViperConfig() *viper.Viper { + return viperConf +} + // Set config vars from cpnfig file not already specified on command line. -func bindFlags(cmd *cobra.Command, v *viper.Viper) { +func BindFlags(cmd *cobra.Command, v *viper.Viper) { cmd.Flags().VisitAll(func(f *pflag.Flag) { configName := f.Name @@ -109,7 +113,7 @@ func setupLogger(logLevel string, logPath string, prettyLogging bool) { config.DoConfigureLogger(logPath, logLevel, prettyLogging) } -func connectToDBAndMigrate(dbConfig config.Database) (*gorm.DB, error) { +func ConnectToDBAndMigrate(dbConfig config.Database) (*gorm.DB, error) { database, err := db.PostgresDbConnect(dbConfig.Host, dbConfig.Port, dbConfig.Database, dbConfig.User, dbConfig.Password, strings.ToLower(dbConfig.LogLevel)) if err != nil { config.Log.Fatal("Could not establish connection to the database", err) diff --git a/db/utils.go b/db/utils.go index c2ca6673..abedcef2 100644 --- a/db/utils.go +++ b/db/utils.go @@ -30,3 +30,11 @@ func FindOrCreateAddressByAddress(db *gorm.DB, address string) (models.Address, err := db.Where(&addr).FirstOrCreate(&addr).Error return addr, err } + +func GetChains(db *gorm.DB) ([]models.Chain, error) { + var chains []models.Chain + if err := db.Find(&chains).Error; err != nil { + return nil, err + } + return chains, nil +}