2018-10-16 12:38:32 +00:00
|
|
|
package gonextcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
req "github.com/levigross/grequests"
|
2018-10-26 13:35:50 +00:00
|
|
|
"gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types"
|
2018-10-16 12:38:32 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//AppsConfigI available methods
|
|
|
|
type AppsConfigI interface {
|
|
|
|
List() (apps []string, err error)
|
|
|
|
Keys(id string) (keys []string, err error)
|
|
|
|
Value(id, key string) (string, error)
|
|
|
|
SetValue(id, key, value string) error
|
|
|
|
DeleteValue(id, key, value string) error
|
|
|
|
Get() (map[string]map[string]string, error)
|
|
|
|
Details(appID string) (map[string]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfig contains all Apps Configuration available actions
|
|
|
|
type AppsConfig struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
//List lists all the available apps
|
|
|
|
func (a *AppsConfig) List() (apps []string, err error) {
|
|
|
|
res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.AppConfigResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Keys returns the app's config keys
|
|
|
|
func (a *AppsConfig) Keys(id string) (keys []string, err error) {
|
|
|
|
res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.AppConfigResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Value get the config value for the given app's key
|
|
|
|
func (a *AppsConfig) Value(id, key string) (string, error) {
|
|
|
|
res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var r types.AppcConfigValueResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//SetValue set the config value for the given app's key
|
|
|
|
func (a *AppsConfig) SetValue(id, key, value string) error {
|
2018-10-16 12:38:32 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"value": value,
|
|
|
|
},
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
_, err := a.c.baseRequest(http.MethodPost, routes.appsConfig, ro, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//DeleteValue delete the config value and (!! be careful !!) the key
|
|
|
|
func (a *AppsConfig) DeleteValue(id, key, value string) error {
|
|
|
|
_, err := a.c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Get returns all apps AppConfigDetails
|
|
|
|
func (a *AppsConfig) Get() (map[string]map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-21 12:53:12 +00:00
|
|
|
appsIDs, err := a.List()
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(appsIDs))
|
|
|
|
for i := range appsIDs {
|
|
|
|
go func(id string) {
|
|
|
|
defer wg.Done()
|
2018-10-21 12:53:12 +00:00
|
|
|
d, err := a.Details(id)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[id] = d
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(appsIDs[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Details returns all the config's key, values pair of the app
|
|
|
|
func (a *AppsConfig) Details(appID string) (map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-16 12:38:32 +00:00
|
|
|
var err error
|
|
|
|
var ks []string
|
2018-10-21 12:53:12 +00:00
|
|
|
ks, err = a.Keys(appID)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(ks))
|
|
|
|
for i := range ks {
|
|
|
|
go func(key string) {
|
|
|
|
defer wg.Done()
|
2018-10-21 12:53:12 +00:00
|
|
|
v, err := a.Value(appID, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[key] = v
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(ks[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|