mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-02-09 05:28:56 -06:00
Parse bundles from files for list calls.
Side effect: get rid of code duplication for listing extensions.
This commit is contained in:
@@ -42,18 +42,10 @@ func (g Service) GetSettingsBundle(c context.Context, req *proto.GetSettingsBund
|
||||
}
|
||||
|
||||
func (g Service) ListSettingsBundles(c context.Context, req *proto.ListSettingsBundlesRequest, res *proto.ListSettingsBundlesResponse) error {
|
||||
r, err := listSettingsBundles(g, req.Extension)
|
||||
r, err := g.manager.ListByExtension(req.Extension)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res.SettingsBundles = r
|
||||
return nil
|
||||
}
|
||||
|
||||
func listSettingsBundles(g Service, extension string) ([]*proto.SettingsBundle, error) {
|
||||
if len(extension) == 0 {
|
||||
return g.manager.ListAll()
|
||||
} else {
|
||||
return g.manager.ListByExtension(extension)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,5 @@ type RegisterFunc func(*config.Config) Manager
|
||||
type Manager interface {
|
||||
Read(extension string, key string) (*proto.SettingsBundle, error)
|
||||
Write(bundle *proto.SettingsBundle) (*proto.SettingsBundle, error)
|
||||
ListAll() ([]*proto.SettingsBundle, error)
|
||||
ListByExtension(extension string) ([]*proto.SettingsBundle, error)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package store
|
||||
|
||||
import (
|
||||
"github.com/owncloud/ocis-settings/pkg/proto/v0"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Builds a unique file name from the given bundle
|
||||
@@ -14,12 +13,3 @@ func buildFileNameFromBundle(bundle *proto.SettingsBundle) string {
|
||||
func buildFileNameFromBundleArgs(extension string, key string) string {
|
||||
return extension + "__" + key + ".json"
|
||||
}
|
||||
|
||||
// Extracts extension and key from the given fileName and builds a (minimalistic) bundle from it
|
||||
func parseBundleFromFileName(fileName string) *proto.SettingsBundle {
|
||||
parts := strings.Split(strings.Replace(fileName, ".json", "", 1), "__")
|
||||
return &proto.SettingsBundle{
|
||||
Key: parts[1],
|
||||
Extension: parts[0],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,23 +43,6 @@ func New(cfg *config.Config) settings.Manager {
|
||||
return &s
|
||||
}
|
||||
|
||||
// ListAll returns all the bundles in the mountPath folder
|
||||
func (s Store) ListAll() ([]*proto.SettingsBundle, error) {
|
||||
records := []*proto.SettingsBundle{}
|
||||
bundles, err := ioutil.ReadDir(s.mountPath)
|
||||
if err != nil {
|
||||
s.Logger.Err(err).Msgf("error reading %v", s.mountPath)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.Logger.Info().Msg("listing bundles")
|
||||
for _, v := range bundles {
|
||||
records = append(records, parseBundleFromFileName(v.Name()))
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// 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{}
|
||||
@@ -71,9 +54,10 @@ func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error
|
||||
|
||||
s.Logger.Info().Msgf("listing bundles by extension %v", extension)
|
||||
for _, v := range bundles {
|
||||
record := parseBundleFromFileName(v.Name())
|
||||
if record.Extension == extension {
|
||||
records = append(records, record)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user