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	// Store writes a single key/value pair in the config
17	StoreString(key, value string) error
18
19	// Store writes a key and timestamp value to the config
20	StoreTimestamp(key string, value time.Time) error
21
22	// Store writes a key and boolean value to the config
23	StoreBool(key string, value bool) error
24
25	// ReadAll reads all key/value pair matching the key prefix
26	ReadAll(keyPrefix string) (map[string]string, error)
27
28	// ReadBool read a single boolean value from the config
29	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
30	// there is zero or more than one entry for this key
31	ReadBool(key string) (bool, error)
32
33	// ReadBool read a single string value from the config
34	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
35	// there is zero or more than one entry for this key
36	ReadString(key string) (string, error)
37
38	// ReadTimestamp read a single timestamp value from the config
39	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
40	// there is zero or more than one entry for this key
41	ReadTimestamp(key string) (time.Time, error)
42
43	// RemoveAll removes all key/value pair matching the key prefix
44	RemoveAll(keyPrefix string) error
45}
46
47func ParseTimestamp(s string) (time.Time, error) {
48	timestamp, err := strconv.Atoi(s)
49	if err != nil {
50		return time.Time{}, err
51	}
52
53	return time.Unix(int64(timestamp), 0), nil
54}