mysql_config.go 919 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package utils
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type ConfigMySql struct {
  7. Host string
  8. Port string
  9. Name string
  10. User string
  11. Password string
  12. }
  13. func IsMySqlConfigExists(file string) bool {
  14. return IsFileExists(file) && !IsDir(file)
  15. }
  16. func MySqlConfigRead(file string) (*ConfigMySql, error) {
  17. f, err := os.Open(file)
  18. if err == nil {
  19. defer f.Close()
  20. dec := json.NewDecoder(f)
  21. conf := ConfigMySql{}
  22. err = dec.Decode(&conf)
  23. if err == nil {
  24. return &conf, err
  25. }
  26. }
  27. return nil, err
  28. }
  29. func MySqlConfigWrite(file string, host string, port string, name string, user string, password string) error {
  30. r, err := json.Marshal(&ConfigMySql{
  31. Host: host,
  32. Port: port,
  33. Name: name,
  34. User: user,
  35. Password: password,
  36. })
  37. if err == nil {
  38. f, err := os.Create(file)
  39. if err == nil {
  40. defer f.Close()
  41. _, err = f.WriteString(string(r))
  42. return err
  43. }
  44. }
  45. return err
  46. }