diff options
author | Michael Muré <batolettre@gmail.com> | 2020-09-27 00:54:14 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-09-29 20:42:21 +0200 |
commit | 71b7eb14010be0c7799b4d5394798c89e379891b (patch) | |
tree | f82d4b7bc6be5165c093d4feb18f5ee642c2c3e6 /repository/config.go | |
parent | c87e9abacfbdc4f221e2e328d4b229d6191f42e9 (diff) | |
download | git-bug-71b7eb14010be0c7799b4d5394798c89e379891b.tar.gz |
repo: implement local/global/any config everywhere
Diffstat (limited to 'repository/config.go')
-rw-r--r-- | repository/config.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/repository/config.go b/repository/config.go index 70d51f11..4ea326b5 100644 --- a/repository/config.go +++ b/repository/config.go @@ -59,3 +59,67 @@ func ParseTimestamp(s string) (time.Time, error) { return time.Unix(int64(timestamp), 0), nil } + +// 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) +} |