diff options
author | amine <hilalyamine@gmail.com> | 2019-10-31 19:05:50 +0100 |
---|---|---|
committer | amine <hilalyamine@gmail.com> | 2019-10-31 19:05:50 +0100 |
commit | 7f177c4750b4acf70cc3fd3d43c19685179e527b (patch) | |
tree | b3a896099d508c679f736ecf32dc70039149fe11 /repository/config_mem.go | |
parent | ab935674a26f2eef5d8014c615b9b5bc1f402135 (diff) | |
download | git-bug-7f177c4750b4acf70cc3fd3d43c19685179e527b.tar.gz |
repository: add ReadTimestamp methods and improve naming
Diffstat (limited to 'repository/config_mem.go')
-rw-r--r-- | repository/config_mem.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/repository/config_mem.go b/repository/config_mem.go new file mode 100644 index 00000000..e8809f5e --- /dev/null +++ b/repository/config_mem.go @@ -0,0 +1,74 @@ +package repository + +import ( + "strconv" + "strings" + "time" +) + +type memConfig struct { + config map[string]string +} + +func newMemConfig(config map[string]string) *memConfig { + return &memConfig{config: config} +} + +func (mc *memConfig) Store(key, value string) error { + mc.config[key] = value + return nil +} + +func (mc *memConfig) ReadAll(keyPrefix string) (map[string]string, error) { + result := make(map[string]string) + for key, val := range mc.config { + if strings.HasPrefix(key, keyPrefix) { + result[key] = val + } + } + return result, nil +} + +func (mc *memConfig) ReadString(key string) (string, error) { + // unlike git, the mock can only store one value for the same key + val, ok := mc.config[key] + if !ok { + return "", ErrNoConfigEntry + } + + return val, nil +} + +func (mc *memConfig) ReadBool(key string) (bool, error) { + // unlike git, the mock can only store one value for the same key + val, ok := mc.config[key] + if !ok { + return false, ErrNoConfigEntry + } + + return strconv.ParseBool(val) +} + +func (mc *memConfig) ReadTimestamp(key string) (*time.Time, error) { + value, err := mc.ReadString(key) + if err != nil { + return nil, err + } + timestamp, err := strconv.Atoi(value) + if err != nil { + return nil, err + } + + t := time.Unix(int64(timestamp), 0) + return &t, nil +} + +// RmConfigs remove all key/value pair matching the key prefix +func (mc *memConfig) RemoveAll(keyPrefix string) error { + for key := range mc.config { + if strings.HasPrefix(key, keyPrefix) { + delete(mc.config, key) + } + } + return nil +} |