-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AUT-10341 Add support for multiple dirs when reading config from fs (#4)
* add support for multiple dirs when reading config from fs * readme * fix passing multiple paths as env variable * readme * logs
- Loading branch information
1 parent
3a8f3de
commit 40c0f16
Showing
7 changed files
with
138 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ on: | |
- master | ||
- dev | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/cloudentity/acp-client-go/clients/hub/models" | ||
"github.com/imdario/mergo" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type MultiStorageConfiguration struct { | ||
DirPath []string `json:"dir_path"` | ||
} | ||
|
||
var DefaultMultiStorageConfig = MultiStorageConfiguration{ | ||
DirPath: []string{"data"}, | ||
} | ||
|
||
func InitMultiStorage(config MultiStorageConfiguration) (*MultiStorage, error) { | ||
var storages []Storage | ||
|
||
if len(config.DirPath) == 0 { | ||
return nil, errors.New("at least one dir_path is required") | ||
} | ||
|
||
for _, config := range config.DirPath { | ||
storages = append(storages, InitStorage(Configuration{ | ||
DirPath: config, | ||
})) | ||
} | ||
|
||
return &MultiStorage{ | ||
Storages: storages, | ||
Config: config, | ||
}, nil | ||
} | ||
|
||
type MultiStorage struct { | ||
Storages []Storage | ||
Config MultiStorageConfiguration | ||
} | ||
|
||
var _ Storage = &MultiStorage{} | ||
|
||
// Store for simplicity stores data in first storage only, it is responsibility of the user to move entities to other storages | ||
func (m *MultiStorage) Store(workspace string, data *models.TreeServer) error { | ||
return m.Storages[0].Store(workspace, data) | ||
} | ||
|
||
// Read data from all storages and merge them | ||
func (m *MultiStorage) Read(workspace string) (models.TreeServer, error) { | ||
var ( | ||
data models.TreeServer | ||
err error | ||
) | ||
|
||
for i := len(m.Storages) - 1; i >= 0; i-- { | ||
var data2 models.TreeServer | ||
|
||
if data2, err = m.Storages[i].Read(workspace); err != nil { | ||
return data, errors.Wrap(err, "failed to read data from storage") | ||
} | ||
|
||
if err = mergo.Merge(&data, data2, mergo.WithOverride); err != nil { | ||
return data, errors.Wrap(err, "failed to merge data") | ||
} | ||
|
||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters