本文整理汇总了Golang中github.com/percona/percona-agent/mysql.DSN.To方法的典型用法代码示例。如果您正苦于以下问题:Golang DSN.To方法的具体用法?Golang DSN.To怎么用?Golang DSN.To使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/percona/percona-agent/mysql.DSN
的用法示例。
在下文中一共展示了DSN.To方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getDSNFromUser
func (i *Installer) getDSNFromUser(dsn *mysql.DSN) error {
// Ask for username
username, err := i.term.PromptString("MySQL username", dsn.Username)
if err != nil {
return err
}
dsn.Username = username
// Ask for password
var password string
if i.flags.Bool["plain-passwords"] {
password, err = i.term.PromptString("MySQL password", "")
} else {
password, err = gopass.GetPass("MySQL password: ")
}
if err != nil {
return err
}
dsn.Password = password
// Ask for hostname / socket path
hostname, err := i.term.PromptStringRequired(
"MySQL host[:port] or socket file",
dsn.To(),
)
if err != nil {
return err
}
if filepath.IsAbs(hostname) {
dsn.Socket = hostname
dsn.Hostname = ""
} else {
f := strings.Split(hostname, ":")
dsn.Hostname = f[0]
if len(f) > 1 {
dsn.Port = f[1]
} else {
dsn.Port = "3306"
}
dsn.Socket = ""
}
return nil
}
示例2: connectMySQL
func (i *Installer) connectMySQL(def *mysql.DSN, creating bool) (mysql.DSN, error) {
dsn := mysql.DSN{}
if creating {
fmt.Println("Specify a root/super MySQL user to create a user for the agent")
} else if def != nil {
fmt.Println("Verify the existing MySQL user to use for the agent")
dsn = *def
if dsn.Username == "" {
user, _ := user.Current()
if user != nil {
dsn.Username = user.Username
}
}
} else {
fmt.Println("Specify an existing MySQL user to use for the agent")
}
CONNECT_MYSQL:
for {
username, err := i.term.PromptStringRequired("MySQL username", dsn.Username)
if err != nil {
return dsn, err
}
dsn.Username = username
var password string
if creating {
password, err = gopass.GetPass("MySQL password: ")
} else {
password, err = i.term.PromptString("MySQL password", dsn.Password)
}
if err != nil {
return dsn, err
}
dsn.Password = password
hostname, err := i.term.PromptStringRequired("MySQL host[:port] or socket file", dsn.To())
if err != nil {
return dsn, err
}
if filepath.IsAbs(hostname) {
dsn.Socket = hostname
dsn.Hostname = ""
} else {
f := strings.Split(hostname, ":")
dsn.Hostname = f[0]
if len(f) > 1 {
dsn.Port = f[1]
} else {
dsn.Port = "3306"
}
dsn.Socket = ""
}
if err := TestMySQLConnection(dsn); err != nil {
fmt.Printf("Error connecting to MySQL %s: %s\n", dsn, err)
again, err := i.term.PromptBool("Try again?", "Y")
if err != nil {
return dsn, err
}
if !again {
return dsn, fmt.Errorf("Failed to connect to MySQL")
}
continue CONNECT_MYSQL
}
fmt.Printf("MySQL connection OK\n")
break
}
return dsn, nil
}