12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package services
- import (
- "flag"
- "github.com/pkg/errors"
- )
- type DbConfig struct {
- Host string `json:"host,omitempty" yaml:"host,omitempty"`
- Port int `json:"port,omitempty" yaml:"port,omitempty"`
- Schema string `json:"schema,omitempty" yaml:"schema,omitempty"`
- User string `json:"user,omitempty" yaml:"user,omitempty"`
- Pwd string `json:"pwd,omitempty" yaml:"pwd,omitempty"`
- Tables []string `json:"tables,omitempty" yaml:"tables,omitempty"`
- }
- var (
- dbConfigFlag *DbConfig
- cfgFilePathFlag string
- )
- func init() {
- dbConfigFlag = &DbConfig{}
- flag.StringVar(&dbConfigFlag.Host, "h", "", "database host")
- flag.IntVar(&dbConfigFlag.Port, "p", 0, "database port")
- flag.StringVar(&dbConfigFlag.Schema, "s", "", "database schema")
- flag.StringVar(&dbConfigFlag.User, "u", "", "database user")
- flag.StringVar(&dbConfigFlag.Pwd, "P", "", "database password")
- flag.StringVar(&cfgFilePathFlag, "c", "", "database config file path")
- }
- func (cfg *DbConfig) IsZero() bool {
- if cfg == nil {
- return true
- }
- if cfg.Host != "" ||
- cfg.Port != 0 ||
- cfg.Schema != "" ||
- cfg.User != "" ||
- cfg.Pwd != "" {
- return false
- }
- return true
- }
- func (cfg *DbConfig) FixDefaultValues() error {
- if cfg == nil {
- return errors.New("can not fix values of nil object")
- }
- if cfg.Schema == "" {
- return errors.New("can not fix default values with empty database schema")
- }
- if cfg.Pwd == "" {
- return errors.New("can not fix default values with empty database password")
- }
- if cfg.Host == "" {
- cfg.Host = "localhost"
- }
- if cfg.Port == 0 {
- cfg.Port = 3306
- } else if cfg.Port < 1 || cfg.Port > 65535 {
- return errors.Errorf("invalid database port %d", cfg.Port)
- }
- if cfg.User == "" {
- cfg.User = "root"
- }
- return nil
- }
|