1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package config
import (
"strings"
)
// LookupResult is a value returned by Lookup/LookupAll.
type LookupResult struct {
Kind Kind
Value string
}
// String returns the explicit string value.
func (result LookupResult) String() (string, error) {
switch result.Kind {
case KindString:
return result.Value, nil
case KindValueless:
return "", ErrValueless
case KindMissing:
return "", ErrMissing
default:
return "", ErrMissing
}
}
// Bool interprets this lookup result using Git config boolean rules.
func (result LookupResult) Bool() (bool, error) {
switch result.Kind {
case KindString:
return parseBool(result.Value)
case KindValueless:
return true, nil
case KindMissing:
return false, ErrMissing
default:
return false, ErrMissing
}
}
// Int interprets this lookup result as a Git integer value.
func (result LookupResult) Int() (int, error) {
switch result.Kind {
case KindString:
return parseInt(result.Value)
case KindValueless:
return 0, ErrValueless
case KindMissing:
return 0, ErrMissing
default:
return 0, ErrMissing
}
}
// Int64 interprets this lookup result as a Git int64 value.
func (result LookupResult) Int64() (int64, error) {
switch result.Kind {
case KindString:
return parseInt64(result.Value)
case KindValueless:
return 0, ErrValueless
case KindMissing:
return 0, ErrMissing
default:
return 0, ErrMissing
}
}
// Lookup retrieves the first value for a given section, optional subsection,
// and key.
func (config *Config) Lookup(section, subsection, key string) LookupResult {
section = strings.ToLower(section)
key = strings.ToLower(key)
for _, entry := range config.entries {
if strings.EqualFold(entry.Section, section) &&
entry.Subsection == subsection &&
strings.EqualFold(entry.Key, key) {
return LookupResult{
Kind: entry.Kind,
Value: entry.Value,
}
}
}
return LookupResult{
Kind: KindMissing,
Value: "",
}
}
// LookupAll retrieves all values for a given section, optional subsection,
// and key.
func (config *Config) LookupAll(section, subsection, key string) []LookupResult {
section = strings.ToLower(section)
key = strings.ToLower(key)
var values []LookupResult
for _, entry := range config.entries {
if strings.EqualFold(entry.Section, section) &&
entry.Subsection == subsection &&
strings.EqualFold(entry.Key, key) {
values = append(values, LookupResult{
Kind: entry.Kind,
Value: entry.Value,
})
}
}
return values
}
|