settings.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "os"
  4. "fmt"
  5. "log"
  6. "encoding/json"
  7. )
  8. type Settings struct{
  9. Token string
  10. ApiUrl string
  11. DbHost string
  12. DbUser string `json:"-"`
  13. DbPassword string `json:"-"`
  14. DbName string
  15. FullLogPath string
  16. ErrorLogPath string
  17. Currency string
  18. }
  19. func (s *Settings) LoadFromFile(filepath string) {
  20. b, err := os.ReadFile(filepath)
  21. if err != nil {
  22. if err.Error() == fmt.Sprintf("open %s: The system cannot find the file specified.", filepath) {
  23. err = nil
  24. path, err := os.Getwd()
  25. if err != nil {
  26. log.Panic(err)
  27. }
  28. path += "\\"+ filepath
  29. fmt.Printf("The system cannot find the config file.\nCreating new config file at:\n%s\n",
  30. path)
  31. s.SaveToFile(path)
  32. log.Fatal("Please fill the config file")
  33. } else {
  34. log.Panic(err)
  35. }
  36. return
  37. }
  38. propJson := string(b)
  39. json.Unmarshal([]byte(propJson), &s)
  40. s.SaveToFile(filepath)
  41. }
  42. func (s *Settings) SaveToFile(filepath string) {
  43. res, err := json.MarshalIndent(s, "", " ")
  44. if err != nil {
  45. log.Panic(err)
  46. }
  47. err = os.WriteFile(filepath, res, 0666)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. }