package options import ( "time" "github.com/spf13/pflag" ) // PGSQLOptions Mysql配置 type PGSQLOptions struct { Host string `json:"host,omitempty" mapstructure:"host"` Username string `json:"username,omitempty" mapstructure:"username"` Password string `json:"-" mapstructure:"password"` Database string `json:"database" mapstructure:"database"` MaxIdleConnections int `json:"max-idle-connections,omitempty" mapstructure:"max-idle-connections"` MaxOpenConnections int `json:"max-open-connections,omitempty" mapstructure:"max-open-connections"` MaxConnectionLifeTime time.Duration `json:"max-connection-life-time,omitempty" mapstructure:"max-connection-life-time"` LogLevel int `json:"log-level" mapstructure:"log-level"` } func NewPGSQLOptions() *PGSQLOptions { return &PGSQLOptions{ Host: "127.0.0.1:5432", Username: "", Password: "", Database: "", MaxIdleConnections: 100, MaxOpenConnections: 100, MaxConnectionLifeTime: time.Duration(10) * time.Second, LogLevel: 1, // Silent } } func (o *PGSQLOptions) Validate() []error { var errs []error return errs } func (o *PGSQLOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.Host, "pgsql.host", o.Host, ""+ "PGSQL service host address. If left blank, the following related pgsql comopts will be ignored.") fs.StringVar(&o.Username, "pgsql.username", o.Username, ""+ "Username for access to pgsql service.") fs.StringVar(&o.Password, "pgsql.password", o.Password, ""+ "Password for access to pgsql, should be used pair with password.") fs.StringVar(&o.Database, "pgsql.database", o.Database, ""+ "Database name for the server to use.") fs.IntVar(&o.MaxIdleConnections, "pgsql.max-idle-connections", o.MaxOpenConnections, ""+ "Maximum idle connections allowed to connect to pgsql.") fs.IntVar(&o.MaxOpenConnections, "pgsql.max-open-connections", o.MaxOpenConnections, ""+ "Maximum open connections allowed to connect to pgsql.") fs.DurationVar(&o.MaxConnectionLifeTime, "pgsql.max-connection-life-time", o.MaxConnectionLifeTime, ""+ "Maximum connection life time allowed to connect to pgsql.") fs.IntVar(&o.LogLevel, "pgsql.log-mode", o.LogLevel, ""+ "Specify gorm log level.") }