config_runtime.go

 1package repository
 2
 3import (
 4	"strconv"
 5	"strings"
 6)
 7
 8type runtimeConfig struct {
 9	config map[string]string
10}
11
12func newRuntimeConfig(config map[string]string) *runtimeConfig {
13	return &runtimeConfig{config: config}
14}
15
16func (rtc *runtimeConfig) Store(key, value string) error {
17	rtc.config[key] = value
18	return nil
19}
20
21func (rtc *runtimeConfig) ReadAll(keyPrefix string) (map[string]string, error) {
22	result := make(map[string]string)
23	for key, val := range rtc.config {
24		if strings.HasPrefix(key, keyPrefix) {
25			result[key] = val
26		}
27	}
28	return result, nil
29}
30
31func (rtc *runtimeConfig) ReadString(key string) (string, error) {
32	// unlike git, the mock can only store one value for the same key
33	val, ok := rtc.config[key]
34	if !ok {
35		return "", ErrNoConfigEntry
36	}
37
38	return val, nil
39}
40
41func (rtc *runtimeConfig) ReadBool(key string) (bool, error) {
42	// unlike git, the mock can only store one value for the same key
43	val, ok := rtc.config[key]
44	if !ok {
45		return false, ErrNoConfigEntry
46	}
47
48	return strconv.ParseBool(val)
49}
50
51// RmConfigs remove all key/value pair matching the key prefix
52func (rtc *runtimeConfig) RemoveAll(keyPrefix string) error {
53	for key := range rtc.config {
54		if strings.HasPrefix(key, keyPrefix) {
55			delete(rtc.config, key)
56		}
57	}
58	return nil
59}