2019-10-31 21:05:50 +03:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2020-06-23 19:02:54 +03:00
|
|
|
"fmt"
|
2019-10-31 21:05:50 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
var _ Config = &MemConfig{}
|
2019-11-01 20:39:45 +03:00
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
type MemConfig struct {
|
2019-10-31 21:05:50 +03:00
|
|
|
config map[string]string
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func NewMemConfig() *MemConfig {
|
|
|
|
return &MemConfig{
|
|
|
|
config: make(map[string]string),
|
|
|
|
}
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) StoreString(key, value string) error {
|
2021-03-28 23:18:01 +03:00
|
|
|
key = normalizeKey(key)
|
2019-10-31 21:05:50 +03:00
|
|
|
mc.config[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) StoreBool(key string, value bool) error {
|
2019-11-01 20:39:45 +03:00
|
|
|
return mc.StoreString(key, strconv.FormatBool(value))
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) StoreTimestamp(key string, value time.Time) error {
|
2019-11-01 20:39:45 +03:00
|
|
|
return mc.StoreString(key, strconv.Itoa(int(value.Unix())))
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) ReadAll(keyPrefix string) (map[string]string, error) {
|
2021-03-28 23:18:01 +03:00
|
|
|
keyPrefix = normalizeKey(keyPrefix)
|
2019-10-31 21:05:50 +03:00
|
|
|
result := make(map[string]string)
|
|
|
|
for key, val := range mc.config {
|
|
|
|
if strings.HasPrefix(key, keyPrefix) {
|
|
|
|
result[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) ReadString(key string) (string, error) {
|
2019-10-31 21:05:50 +03:00
|
|
|
// unlike git, the mock can only store one value for the same key
|
2021-03-28 23:18:01 +03:00
|
|
|
key = normalizeKey(key)
|
2019-10-31 21:05:50 +03:00
|
|
|
val, ok := mc.config[key]
|
|
|
|
if !ok {
|
|
|
|
return "", ErrNoConfigEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) ReadBool(key string) (bool, error) {
|
2019-10-31 21:05:50 +03:00
|
|
|
// unlike git, the mock can only store one value for the same key
|
2021-03-28 23:18:01 +03:00
|
|
|
val, err := mc.ReadString(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.ParseBool(val)
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) ReadTimestamp(key string) (time.Time, error) {
|
2019-10-31 21:05:50 +03:00
|
|
|
value, err := mc.ReadString(key)
|
|
|
|
if err != nil {
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Time{}, err
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
2019-11-03 18:46:51 +03:00
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
timestamp, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Time{}, err
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Unix(int64(timestamp), 0), nil
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RmConfigs remove all key/value pair matching the key prefix
|
2019-12-08 23:15:06 +03:00
|
|
|
func (mc *MemConfig) RemoveAll(keyPrefix string) error {
|
2021-03-28 23:18:01 +03:00
|
|
|
keyPrefix = normalizeKey(keyPrefix)
|
2020-06-23 19:02:54 +03:00
|
|
|
found := false
|
2019-10-31 21:05:50 +03:00
|
|
|
for key := range mc.config {
|
|
|
|
if strings.HasPrefix(key, keyPrefix) {
|
|
|
|
delete(mc.config, key)
|
2020-06-23 19:02:54 +03:00
|
|
|
found = true
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 19:02:54 +03:00
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("section not found")
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-28 23:18:01 +03:00
|
|
|
|
|
|
|
func normalizeKey(key string) string {
|
|
|
|
// this feels so wrong, but that's apparently how git behave.
|
|
|
|
// only section and final segment are case insensitive, subsection in between are not.
|
|
|
|
s := strings.Split(key, ".")
|
|
|
|
s[0] = strings.ToLower(s[0])
|
|
|
|
s[len(s)-1] = strings.ToLower(s[len(s)-1])
|
|
|
|
return strings.Join(s, ".")
|
|
|
|
}
|