config.go

 1package repository
 2
 3import (
 4	"errors"
 5	"strconv"
 6	"time"
 7)
 8
 9var (
10	ErrNoConfigEntry       = errors.New("no config entry for the given key")
11	ErrMultipleConfigEntry = errors.New("multiple config entry for the given key")
12)
13
14// Config represent the common function interacting with the repository config storage
15type Config interface {
16	ConfigRead
17	ConfigWrite
18}
19
20type ConfigRead interface {
21	// ReadAll reads all key/value pair matching the key prefix
22	ReadAll(keyPrefix string) (map[string]string, error)
23
24	// ReadBool read a single boolean value from the config
25	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
26	// there is zero or more than one entry for this key
27	ReadBool(key string) (bool, error)
28
29	// ReadBool read a single string value from the config
30	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
31	// there is zero or more than one entry for this key
32	ReadString(key string) (string, error)
33
34	// ReadTimestamp read a single timestamp value from the config
35	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
36	// there is zero or more than one entry for this key
37	ReadTimestamp(key string) (time.Time, error)
38}
39
40type ConfigWrite interface {
41	// Store writes a single key/value pair in the config
42	StoreString(key, value string) error
43
44	// Store writes a key and timestamp value to the config
45	StoreTimestamp(key string, value time.Time) error
46
47	// Store writes a key and boolean value to the config
48	StoreBool(key string, value bool) error
49
50	// RemoveAll removes all key/value pair matching the key prefix
51	RemoveAll(keyPrefix string) error
52}
53
54func ParseTimestamp(s string) (time.Time, error) {
55	timestamp, err := strconv.Atoi(s)
56	if err != nil {
57		return time.Time{}, err
58	}
59
60	return time.Unix(int64(timestamp), 0), nil
61}