package settings import ( "encoding/json" "os" "path/filepath" "github.com/spf13/afero" ) const ( Local FileSystemType = iota ) type FileSystemType int type Settings struct { Endpoint string Token string StorageProvider StorageSettings } type StorageSettings struct { Type FileSystemType BasePath string } func parseSettings(data []byte) Settings { settings := Settings{} json.Unmarshal(data, &settings) return settings } func LoadSettings(fileSystem afero.Fs) Settings { workingDirectory, _ := os.Getwd() path := filepath.Join(workingDirectory, "settings.json") // Load file and parse file data, err := afero.ReadFile(fileSystem, path) if err == nil { return parseSettings(data) } // If file does not exist, create default settings defaultSettings := Settings{ Endpoint: "127.0.0.1:8000", Token: "changeme", StorageProvider: StorageSettings{ Type: Local, BasePath: "assets", }, } serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm) return defaultSettings }