12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package utils
- import (
- "encoding/json"
- "os"
- )
- type ConfigMySql struct {
- Host string
- Port string
- Name string
- User string
- Password string
- }
- func IsMySqlConfigExists(homedir string) bool {
- f, err := os.Open(homedir + "/config/mysql.json")
- if err == nil {
- defer f.Close()
- st, err := os.Stat(homedir + "/config/mysql.json")
- if err == nil {
- if !st.Mode().IsDir() {
- return true
- }
- }
- }
- return false
- }
- func MySqlConfigRead(homedir string) (*ConfigMySql, error) {
- f, err := os.Open(homedir + "/config/mysql.json")
- if err == nil {
- defer f.Close()
- dec := json.NewDecoder(f)
- conf := ConfigMySql{}
- err = dec.Decode(&conf)
- if err == nil {
- return &conf, err
- }
- }
- return nil, err
- }
- func MySqlConfigWrite(homedir string, host string, port string, name string, user string, password string) error {
- r, err := json.Marshal(&ConfigMySql{
- Host: host,
- Port: port,
- Name: name,
- User: user,
- Password: password,
- })
- if err == nil {
- f, err := os.Create(homedir + "/config/mysql.json")
- if err == nil {
- defer f.Close()
- _, err = f.WriteString(string(r))
- return err
- }
- }
- return err
- }
|