forked from ebhomengo/niki
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"git.gocasts.ir/ebhomengo/niki/benefactorapp"
|
|
cfgloader "git.gocasts.ir/ebhomengo/niki/pkg/cfg_loader"
|
|
"git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
|
|
database "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
|
|
serviceConfigMysql "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
|
|
"git.gocasts.ir/ebhomengo/niki/pkg/path"
|
|
"github.com/spf13/cobra"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
var RootCmd = &cobra.Command{
|
|
Use: "benefactor_service",
|
|
Short: "A CLI for benefactor Service",
|
|
Long: `benefactor Service CLI is a tool to manage and run
|
|
the benefactor service, including migrations and server startup.`,
|
|
}
|
|
|
|
func getMysqlConfig() mysql.Config {
|
|
var cfg = loadAppConfig()
|
|
|
|
mysqlConfig := serviceConfigMysql.Config{
|
|
Username: cfg.MySQLDB.Username,
|
|
Password: cfg.MySQLDB.Password,
|
|
Port: cfg.MySQLDB.Port,
|
|
Host: cfg.MySQLDB.Host,
|
|
DBName: cfg.MySQLDB.DBName,
|
|
}
|
|
|
|
return mysqlConfig
|
|
}
|
|
|
|
func getDB(cfg database.Config) *mysql.DB {
|
|
|
|
db := serviceConfigMysql.New(cfg)
|
|
defer func() {
|
|
if err := db.CloseStatements(); err != nil {
|
|
fmt.Printf("Error closing statements: %v\n", err)
|
|
}
|
|
}()
|
|
return db
|
|
}
|
|
func loadAppConfig() benefactorapp.Config {
|
|
var cfg benefactorapp.Config
|
|
|
|
projectRoot, err := path.PathProjectRoot()
|
|
if err != nil {
|
|
log.Fatalf("Error finding project root: %v", err)
|
|
}
|
|
|
|
yamlPath := os.Getenv("CONFIG_PATH")
|
|
|
|
if yamlPath == "" {
|
|
defaultConfig := filepath.Join(projectRoot, "deploy", "benefactor", "development", "config.yml")
|
|
if _, err := os.Stat(defaultConfig); err == nil {
|
|
yamlPath = defaultConfig
|
|
} else {
|
|
yamlPath = filepath.Join(projectRoot, "deploy", "benefactor", "development", "config.local.yml")
|
|
}
|
|
}
|
|
|
|
options := cfgloader.Option{
|
|
Prefix: "BENEFACTOR_",
|
|
Delimiter: ".",
|
|
Separator: "__",
|
|
YamlFilePath: yamlPath,
|
|
CallbackEnv: nil,
|
|
}
|
|
|
|
if err := cfgloader.Load(options, &cfg); err != nil {
|
|
log.Fatalf("Failed to load benefactor config: %v", err)
|
|
}
|
|
|
|
return cfg
|
|
}
|