12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package main
- import (
- "os"
- "fmt"
- "log"
- "encoding/json"
- )
- type Settings struct{
- Token string
- ApiUrl string
- DbHost string
- DbUser string `json:"-"`
- DbPassword string `json:"-"`
- DbName string
- FullLogPath string
- ErrorLogPath string
- Currency string
- }
- func (s *Settings) LoadFromFile(filepath string) {
- b, err := os.ReadFile(filepath)
- if err != nil {
- if err.Error() == fmt.Sprintf("open %s: The system cannot find the file specified.", filepath) {
- err = nil
- path, err := os.Getwd()
- if err != nil {
- log.Panic(err)
- }
- path += "\\"+ filepath
- fmt.Printf("The system cannot find the config file.\nCreating new config file at:\n%s\n",
- path)
- s.SaveToFile(path)
- log.Fatal("Please fill the config file")
- } else {
- log.Panic(err)
- }
- return
- }
- propJson := string(b)
- json.Unmarshal([]byte(propJson), &s)
- s.SaveToFile(filepath)
- }
- func (s *Settings) SaveToFile(filepath string) {
- res, err := json.MarshalIndent(s, "", " ")
- if err != nil {
- log.Panic(err)
- }
- err = os.WriteFile(filepath, res, 0666)
- if err != nil {
- log.Fatal(err)
- }
- }
|