package mqtt import ( "encoding/json" "fmt" "io/ioutil" "os" "github.com/go-basic/uuid" ) type Config struct { Broker string `json:"broker"` ClientId string `json:"client_id"` Username string `json:"username"` Password string `json:"password"` } const configPath = "/opt/vasystem/config/mqtt.conf" var Options Config func LoadConfig() { if !pathExists(configPath) { SaveConfig() } else { file, _ := os.Open(configPath) defer file.Close() fd := json.NewDecoder(file) fd.Decode(&Options) fmt.Printf("%v\n", Options) } } func SaveConfig() { Options = Config{ "127.0.0.1:1883", "smart-ai-helmet-manager" + uuid.New(), "admin", "admin", } bytes, err := json.Marshal(Options) if err == nil { ioutil.WriteFile(configPath, bytes, 0744) } } func pathExists(path string) bool { _, err := os.Stat(path) if err == nil { return true } fmt.Printf("%v", err.Error()) return false }