Move settings bundles into appropriate sub folders

This commit is contained in:
Benedikt Kulmann
2020-04-28 12:31:25 +02:00
parent 92d03ffeb3
commit 4ce01fe8c0
2 changed files with 46 additions and 14 deletions

View File

@@ -2,14 +2,37 @@ package store
import (
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"os"
"path"
)
const folderNameBundles = "bundles"
const folderNameSettings = "settings"
// Builds the folder path for storing settings bundles
func buildFolderPathBundles(mountPath string) string {
folderPath := path.Join(mountPath, folderNameBundles)
ensureFolderExists(folderPath)
return folderPath
}
// Builds a unique file name from the given bundle
func buildFileNameFromBundle(bundle *proto.SettingsBundle) string {
return buildFileNameFromBundleArgs(bundle.Extension, bundle.Key)
func buildFilePathFromBundle(mountPath string, bundle *proto.SettingsBundle) string {
return buildFilePathFromBundleArgs(mountPath, bundle.Extension, bundle.Key)
}
// Builds a unique file name from the given params
func buildFileNameFromBundleArgs(extension string, key string) string {
return extension + "__" + key + ".json"
func buildFilePathFromBundleArgs(mountPath string, extension string, key string) string {
extensionFolder := path.Join(mountPath, folderNameBundles, extension)
if _, err := os.Stat(extensionFolder); os.IsNotExist(err) {
_ = os.MkdirAll(extensionFolder, 0700)
}
return path.Join(extensionFolder, key + ".json")
}
// Checks if the given path is an existing folder and creates one if not existing
func ensureFolderExists(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.MkdirAll(path, 0700)
}
}

View File

@@ -45,19 +45,28 @@ func New(cfg *config.Config) settings.Manager {
// ListByExtension returns all bundles in the mountPath folder belonging to the given extension
func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error) {
records := []*proto.SettingsBundle{}
bundles, err := ioutil.ReadDir(s.mountPath)
bundlesFolder := buildFolderPathBundles(s.mountPath)
extensionFolders, err := ioutil.ReadDir(bundlesFolder)
if err != nil {
s.Logger.Err(err).Msgf("error reading %v", s.mountPath)
s.Logger.Err(err).Msgf("error reading %v", bundlesFolder)
return nil, err
}
s.Logger.Info().Msgf("listing bundles by extension %v", extension)
for _, v := range bundles {
record := proto.SettingsBundle{}
err = s.parseRecordFromFile(&record, path.Join(s.mountPath, v.Name()))
if err == nil && (len(extension) == 0 || extension == record.Extension) {
records = append(records, &record)
var records []*proto.SettingsBundle
for _, extensionFolder := range extensionFolders {
extensionPath := path.Join(bundlesFolder, extensionFolder.Name())
bundleFiles, err := ioutil.ReadDir(extensionPath)
if err == nil {
for _, bundleFile := range bundleFiles {
record := proto.SettingsBundle{}
err = s.parseRecordFromFile(&record, path.Join(extensionPath, bundleFile.Name()))
if err == nil && (len(extension) == 0 || extension == record.Extension) {
records = append(records, &record)
}
}
} else {
s.Logger.Err(err).Msgf("error reading %v", extensionPath)
}
}
@@ -71,7 +80,7 @@ func (s Store) Read(extension string, key string) (*proto.SettingsBundle, error)
return nil, fmt.Errorf(emptyKeyError)
}
filePath := path.Join(s.mountPath, buildFileNameFromBundleArgs(extension, key))
filePath := buildFilePathFromBundleArgs(s.mountPath, extension, key)
record := proto.SettingsBundle{}
if err := s.parseRecordFromFile(&record, filePath); err != nil {
return nil, err
@@ -88,7 +97,7 @@ func (s Store) Write(record *proto.SettingsBundle) (*proto.SettingsBundle, error
return nil, fmt.Errorf(emptyKeyError)
}
filePath := path.Join(s.mountPath, buildFileNameFromBundle(record))
filePath := buildFilePathFromBundle(s.mountPath, record)
if err := s.writeRecordToFile(record, filePath); err != nil {
return nil, err
}