forked from FabianVowie/Lithium
Roman Zipp
3 years ago
committed by
Fabian Vowie
No known key found for this signature in database
GPG Key ID: C27317C33B27C410
4 changed files with 125 additions and 4 deletions
@ -0,0 +1,61 @@ |
|||||
|
package settings |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"os" |
||||
|
"path/filepath" |
||||
|
|
||||
|
"github.com/spf13/afero" |
||||
|
) |
||||
|
|
||||
|
const ( |
||||
|
Local FileSystemType = iota |
||||
|
) |
||||
|
|
||||
|
type FileSystemType int |
||||
|
|
||||
|
type Settings struct { |
||||
|
Endpoint string `json:"endpoint"` |
||||
|
Token string `json:"token"` |
||||
|
StorageProvider StorageSettings `json:"storage_provider"` |
||||
|
} |
||||
|
|
||||
|
type StorageSettings struct { |
||||
|
Type FileSystemType `json:"type"` |
||||
|
BasePath string `json:"base_path"` |
||||
|
} |
||||
|
|
||||
|
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 |
||||
|
} |
@ -0,0 +1,48 @@ |
|||||
|
package settings |
||||
|
|
||||
|
import ( |
||||
|
"os" |
||||
|
"path/filepath" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/spf13/afero" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestSettingsParsing(t *testing.T) { |
||||
|
const file string = `{ |
||||
|
"endpoint": "0.0.0.0:8000", |
||||
|
"token": "foobar", |
||||
|
"storage_provider": { |
||||
|
"type": 0, |
||||
|
"base_path": "assets" |
||||
|
} |
||||
|
}` |
||||
|
|
||||
|
t.Run("Settings parsing is successful", func(t *testing.T) { |
||||
|
settings := parseSettings([]byte(file)) |
||||
|
|
||||
|
assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) |
||||
|
assert.Equal(t, "foobar", settings.Token) |
||||
|
assert.Equal(t, "assets", settings.StorageProvider.BasePath) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
func TestSettingsLoading(t *testing.T) { |
||||
|
t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) { |
||||
|
fileSystem := afero.NewMemMapFs() |
||||
|
workingDirectory, _ := os.Getwd() |
||||
|
|
||||
|
path := filepath.Join(workingDirectory, "settings.json") |
||||
|
|
||||
|
// Settings file does not exist in the beginning
|
||||
|
doesFileExist, _ := afero.Exists(fileSystem, path) |
||||
|
assert.False(t, doesFileExist) |
||||
|
|
||||
|
LoadSettings(fileSystem) |
||||
|
|
||||
|
// Settings file should be present after calling LoadSettings
|
||||
|
doesFileExist, _ = afero.Exists(fileSystem, path) |
||||
|
assert.True(t, doesFileExist) |
||||
|
}) |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue