github.com/kotovmak/go-admin@v1.1.1/adm/db.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/AlecAivazis/survey/v2"
     8  	"github.com/kotovmak/go-admin/modules/config"
     9  	"github.com/kotovmak/go-admin/modules/db"
    10  	"gopkg.in/ini.v1"
    11  )
    12  
    13  type dbInfo struct {
    14  	DriverName string
    15  	Host       string
    16  	Port       string
    17  	File       string
    18  	User       string
    19  	Password   string
    20  	Schema     string
    21  	Database   string
    22  }
    23  
    24  func initSurvey() {
    25  	survey.SelectQuestionTemplate = strings.ReplaceAll(survey.SelectQuestionTemplate,
    26  		"type to filter", "type to filter, enter to select")
    27  	survey.MultiSelectQuestionTemplate = strings.ReplaceAll(survey.MultiSelectQuestionTemplate,
    28  		"enter to select", "space to select")
    29  
    30  	survey.SelectQuestionTemplate = strings.ReplaceAll(survey.SelectQuestionTemplate,
    31  		"Use arrows to move, type to filter, enter to select",
    32  		getWord("Use arrows to move, type to filter, enter to select"))
    33  
    34  	survey.MultiSelectQuestionTemplate = strings.ReplaceAll(survey.MultiSelectQuestionTemplate,
    35  		"Use arrows to move, space to select, type to filter",
    36  		getWord("Use arrows to move, space to select, type to filter"))
    37  }
    38  
    39  func getDBInfoFromINIConfig(cfg *ini.File, connection string) *dbInfo {
    40  
    41  	section := "database"
    42  
    43  	if connection != "default" && connection != "" {
    44  		section = section + "." + connection
    45  	}
    46  
    47  	dbCfgModel, exist := cfg.GetSection(section)
    48  
    49  	if exist == nil {
    50  		return &dbInfo{
    51  			DriverName: dbCfgModel.Key("driver").Value(),
    52  			Host:       dbCfgModel.Key("host").Value(),
    53  			User:       dbCfgModel.Key("username").Value(),
    54  			Port:       dbCfgModel.Key("port").Value(),
    55  			File:       dbCfgModel.Key("file").Value(),
    56  			Password:   dbCfgModel.Key("password").Value(),
    57  			Database:   dbCfgModel.Key("database").Value(),
    58  		}
    59  	}
    60  
    61  	return &dbInfo{}
    62  }
    63  
    64  func askForDBConfig(info *dbInfo) config.DatabaseList {
    65  
    66  	initSurvey()
    67  
    68  	if info.DriverName == "" {
    69  		info.DriverName = singleSelect(getWord("choose a driver"),
    70  			[]string{db.DriverMysql, db.DriverPostgresql, db.DriverSqlite, db.DriverMssql}, db.DriverMysql)
    71  	}
    72  
    73  	if info.DriverName != db.DriverSqlite {
    74  
    75  		defaultPort := "3306"
    76  		defaultUser := "root"
    77  
    78  		if info.DriverName == db.DriverPostgresql {
    79  			defaultPort = "5432"
    80  			defaultUser = "postgres"
    81  		}
    82  
    83  		if info.DriverName == db.DriverMssql {
    84  			defaultPort = "1433"
    85  			defaultUser = "sa"
    86  		}
    87  
    88  		if info.Host == "" {
    89  			info.Host = promptWithDefault("sql address", "127.0.0.1")
    90  		}
    91  
    92  		if info.Port == "" {
    93  			info.Port = promptWithDefault("sql port", defaultPort)
    94  		}
    95  
    96  		if info.User == "" {
    97  			info.User = promptWithDefault("sql username", defaultUser)
    98  		}
    99  
   100  		if info.Password == "" {
   101  			info.Password = promptPassword()
   102  		}
   103  
   104  		if info.Schema == "" && info.DriverName == db.DriverPostgresql {
   105  			info.Schema = promptWithDefault("sql schema", "public")
   106  		}
   107  
   108  		if info.Database == "" {
   109  			info.Database = prompt("sql database name")
   110  		}
   111  
   112  		return map[string]config.Database{
   113  			"default": {
   114  				Host:            info.Host,
   115  				Port:            info.Port,
   116  				User:            info.User,
   117  				Pwd:             info.Password,
   118  				Name:            info.Database,
   119  				MaxIdleConns:    5,
   120  				MaxOpenConns:    10,
   121  				ConnMaxLifetime: time.Hour,
   122  				ConnMaxIdleTime: 0,
   123  				Driver:          info.DriverName,
   124  				File:            "",
   125  			},
   126  		}
   127  	} else {
   128  
   129  		if info.File == "" {
   130  			info.File = promptWithDefault("sql file", "./admin.db")
   131  		}
   132  
   133  		return map[string]config.Database{
   134  			"default": {
   135  				Driver: info.DriverName,
   136  				File:   info.File,
   137  			},
   138  		}
   139  	}
   140  }
   141  
   142  func askForDBConnection(info *dbInfo) db.Connection {
   143  
   144  	var (
   145  		cfg  = askForDBConfig(info)
   146  		conn = db.GetConnectionByDriver(info.DriverName)
   147  	)
   148  
   149  	conn.InitDB(cfg)
   150  
   151  	return conn
   152  }