2019-10-31 17:46:09 +03:00
|
|
|
package repository
|
|
|
|
|
2019-11-01 20:39:45 +03:00
|
|
|
import (
|
2020-07-27 01:14:01 +03:00
|
|
|
"errors"
|
2019-11-01 20:39:45 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
2019-10-31 21:05:50 +03:00
|
|
|
|
2020-07-27 01:14:01 +03:00
|
|
|
var (
|
|
|
|
ErrNoConfigEntry = errors.New("no config entry for the given key")
|
|
|
|
ErrMultipleConfigEntry = errors.New("multiple config entry for the given key")
|
|
|
|
)
|
|
|
|
|
2019-10-31 17:46:09 +03:00
|
|
|
// Config represent the common function interacting with the repository config storage
|
|
|
|
type Config interface {
|
2020-09-27 00:00:27 +03:00
|
|
|
ConfigRead
|
|
|
|
ConfigWrite
|
|
|
|
}
|
2019-10-31 17:46:09 +03:00
|
|
|
|
2020-09-27 00:00:27 +03:00
|
|
|
type ConfigRead interface {
|
2019-10-31 17:46:09 +03:00
|
|
|
// ReadAll reads all key/value pair matching the key prefix
|
|
|
|
ReadAll(keyPrefix string) (map[string]string, error)
|
|
|
|
|
|
|
|
// ReadBool read a single boolean value from the config
|
|
|
|
// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
|
|
|
|
// there is zero or more than one entry for this key
|
|
|
|
ReadBool(key string) (bool, error)
|
|
|
|
|
|
|
|
// ReadBool read a single string value from the config
|
|
|
|
// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
|
|
|
|
// there is zero or more than one entry for this key
|
|
|
|
ReadString(key string) (string, error)
|
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
// ReadTimestamp read a single timestamp value from the config
|
|
|
|
// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
|
|
|
|
// there is zero or more than one entry for this key
|
2019-11-03 18:46:51 +03:00
|
|
|
ReadTimestamp(key string) (time.Time, error)
|
2020-09-27 00:00:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigWrite interface {
|
|
|
|
// Store writes a single key/value pair in the config
|
|
|
|
StoreString(key, value string) error
|
|
|
|
|
|
|
|
// Store writes a key and timestamp value to the config
|
|
|
|
StoreTimestamp(key string, value time.Time) error
|
|
|
|
|
|
|
|
// Store writes a key and boolean value to the config
|
|
|
|
StoreBool(key string, value bool) error
|
2019-10-31 21:05:50 +03:00
|
|
|
|
2019-10-31 17:46:09 +03:00
|
|
|
// RemoveAll removes all key/value pair matching the key prefix
|
|
|
|
RemoveAll(keyPrefix string) error
|
|
|
|
}
|
2019-11-01 20:39:45 +03:00
|
|
|
|
2019-11-10 16:46:55 +03:00
|
|
|
func ParseTimestamp(s string) (time.Time, error) {
|
2019-11-01 20:39:45 +03:00
|
|
|
timestamp, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Time{}, err
|
2019-11-01 20:39:45 +03:00
|
|
|
}
|
|
|
|
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Unix(int64(timestamp), 0), nil
|
2019-11-01 20:39:45 +03:00
|
|
|
}
|
2020-09-27 01:54:14 +03:00
|
|
|
|
|
|
|
// mergeConfig is a helper to easily support RepoConfig.AnyConfig()
|
|
|
|
// from two separate local and global Config
|
|
|
|
func mergeConfig(local ConfigRead, global ConfigRead) *mergedConfig {
|
|
|
|
return &mergedConfig{
|
|
|
|
local: local,
|
|
|
|
global: global,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ConfigRead = &mergedConfig{}
|
|
|
|
|
|
|
|
type mergedConfig struct {
|
|
|
|
local ConfigRead
|
|
|
|
global ConfigRead
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mergedConfig) ReadAll(keyPrefix string) (map[string]string, error) {
|
|
|
|
values, err := m.global.ReadAll(keyPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
locals, err := m.local.ReadAll(keyPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, val := range locals {
|
|
|
|
values[k] = val
|
|
|
|
}
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mergedConfig) ReadBool(key string) (bool, error) {
|
|
|
|
v, err := m.local.ReadBool(key)
|
|
|
|
if err == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return m.global.ReadBool(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mergedConfig) ReadString(key string) (string, error) {
|
|
|
|
val, err := m.local.ReadString(key)
|
|
|
|
if err == nil {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return m.global.ReadString(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mergedConfig) ReadTimestamp(key string) (time.Time, error) {
|
|
|
|
val, err := m.local.ReadTimestamp(key)
|
|
|
|
if err == nil {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
return m.global.ReadTimestamp(key)
|
|
|
|
}
|
2020-09-27 21:31:09 +03:00
|
|
|
|
|
|
|
var _ ConfigWrite = &configPanicWriter{}
|
|
|
|
|
|
|
|
type configPanicWriter struct{}
|
|
|
|
|
|
|
|
func (c configPanicWriter) StoreString(key, value string) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c configPanicWriter) StoreTimestamp(key string, value time.Time) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c configPanicWriter) StoreBool(key string, value bool) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c configPanicWriter) RemoveAll(keyPrefix string) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|