refactor(be): move printing db info to separate method

This commit is contained in:
Denis Gukov 2022-11-20 01:20:00 +05:00
parent 53ad8fe7d4
commit 7bf59cf234
3 changed files with 25 additions and 20 deletions

View File

@ -1,7 +1,7 @@
package cmd
import (
"fmt"
"github.com/ansible-semaphore/semaphore/util"
"github.com/spf13/cobra"
)
@ -15,6 +15,6 @@ var migrateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
store := createStore("migrate")
defer store.Close("migrate")
fmt.Println("\n db migrations run on startup automatically")
util.Config.PrintDbInfo()
},
}

View File

@ -46,20 +46,8 @@ func runService() {
defer schedulePool.Destroy()
dialect, err := util.Config.GetDialect()
if err != nil {
panic(err)
}
switch dialect {
case util.DbDriverMySQL:
fmt.Printf("MySQL %v@%v %v\n", util.Config.MySQL.Username, util.Config.MySQL.Hostname, util.Config.MySQL.DbName)
case util.DbDriverBolt:
fmt.Printf("BoltDB %v\n", util.Config.BoltDb.Hostname)
case util.DbDriverPostgres:
fmt.Printf("Postgres %v@%v %v\n", util.Config.Postgres.Username, util.Config.Postgres.Hostname, util.Config.Postgres.DbName)
default:
panic(fmt.Errorf("database configuration not found"))
}
util.Config.PrintDbInfo()
fmt.Printf("Tmp Path (projects home) %v\n", util.Config.TmpPath)
fmt.Printf("Semaphore %v\n", util.Version)
fmt.Printf("Interface %v\n", util.Config.Interface)
@ -93,7 +81,7 @@ func runService() {
store.Close("root")
}
err = http.ListenAndServe(util.Config.Interface+util.Config.Port, cropTrailingSlashMiddleware(router))
err := http.ListenAndServe(util.Config.Interface+util.Config.Port, cropTrailingSlashMiddleware(router))
if err != nil {
log.Panic(err)

View File

@ -49,7 +49,7 @@ type ldapMappings struct {
CN string `json:"cn"`
}
//ConfigType mapping between Config and the json file that sets it
// ConfigType mapping between Config and the json file that sets it
type ConfigType struct {
MySQL DbConfig `json:"mysql"`
BoltDb DbConfig `json:"bolt"`
@ -120,7 +120,7 @@ type ConfigType struct {
DemoMode bool `json:"demo_mode"`
}
//Config exposes the application configuration storage for use in the application
// Config exposes the application configuration storage for use in the application
var Config *ConfigType
// ToJSON returns a JSON string of the config
@ -348,6 +348,23 @@ func (d *DbConfig) GetConnectionString(includeDbName bool) (connectionString str
return
}
func (conf *ConfigType) PrintDbInfo() {
dialect, err := conf.GetDialect()
if err != nil {
panic(err)
}
switch dialect {
case DbDriverMySQL:
fmt.Printf("MySQL %v@%v %v\n", conf.MySQL.Username, conf.MySQL.Hostname, conf.MySQL.DbName)
case DbDriverBolt:
fmt.Printf("BoltDB %v\n", conf.BoltDb.Hostname)
case DbDriverPostgres:
fmt.Printf("Postgres %v@%v %v\n", conf.Postgres.Username, conf.Postgres.Hostname, conf.Postgres.DbName)
default:
panic(fmt.Errorf("database configuration not found"))
}
}
func (conf *ConfigType) GetDialect() (dialect DbDriver, err error) {
if conf.Dialect == "" {
switch {
@ -391,7 +408,7 @@ func (conf *ConfigType) GetDBConfig() (dbConfig DbConfig, err error) {
return
}
//GenerateSecrets generates cookie secret during setup
// GenerateSecrets generates cookie secret during setup
func (conf *ConfigType) GenerateSecrets() {
hash := securecookie.GenerateRandomKey(32)
encryption := securecookie.GenerateRandomKey(32)