package util import ( "encoding/json" "fmt" "os" ) type Config struct { PhotoPath string `json:"photopath"` } func ReadConfig(path string) (Config, error) { var cfg Config file, err := os.Open(path) if err != nil { return cfg, err } defer file.Close() decoder := json.NewDecoder(file) decoder.DisallowUnknownFields() err = decoder.Decode(&cfg) return cfg, err } func createConfigFile(cfg Config) { data, err := json.MarshalIndent(cfg, "", " ") if err != nil { panic(err) } err = os.WriteFile("config.json", data, 0644) if err != nil { panic(err) } } func CreateConfig() { var cfg Config println("┌ Please Configure your backend !\n│") // Checking PhotoPath var err error = fmt.Errorf("Path") println("├\uf15b Enter photo album path \033[90m(press Enter for './')\033[0m") for err != nil { photopath := InputString("│> ") if photopath == "" { photopath = "./" } _, err = os.ReadDir(photopath) if err != nil { fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") } else { fmt.Println("│\033[32m\uf00c Path correct.\033[0m") cfg.PhotoPath = photopath } } // Ending configuration println("│\n└ Configuration finished !") createConfigFile(cfg) }