@@ -2,6 +2,27 @@ package config
2
2
3
3
import "strings"
4
4
5
+ // Section is the representation of a section inside git configuration files.
6
+ // Each Section contains Options that are used by both the Git plumbing
7
+ // and the porcelains.
8
+ // Sections can be further divided into subsections. To begin a subsection
9
+ // put its name in double quotes, separated by space from the section name,
10
+ // in the section header, like in the example below:
11
+ //
12
+ // [section "subsection"]
13
+ //
14
+ // All the other lines (and the remainder of the line after the section header)
15
+ // are recognized as option variables, in the form "name = value" (or just name,
16
+ // which is a short-hand to say that the variable is the boolean "true").
17
+ // The variable names are case-insensitive, allow only alphanumeric characters
18
+ // and -, and must start with an alphabetic character:
19
+ //
20
+ // [section "subsection1"]
21
+ // option1 = value1
22
+ // option2
23
+ // [section "subsection2"]
24
+ // option3 = value2
25
+ //
5
26
type Section struct {
6
27
Name string
7
28
Options Options
@@ -17,29 +38,38 @@ type Sections []*Section
17
38
18
39
type Subsections []* Subsection
19
40
41
+ // IsName checks if the name provided is equals to the Section name, case insensitive.
20
42
func (s * Section ) IsName (name string ) bool {
21
43
return strings .ToLower (s .Name ) == strings .ToLower (name )
22
44
}
23
45
46
+ // Option return the value for the specified key. Empty string is returned if
47
+ // key does not exists.
24
48
func (s * Section ) Option (key string ) string {
25
49
return s .Options .Get (key )
26
50
}
27
51
52
+ // AddOption adds a new Option to the Section. The updated Section is returned.
28
53
func (s * Section ) AddOption (key string , value string ) * Section {
29
54
s .Options = s .Options .withAddedOption (key , value )
30
55
return s
31
56
}
32
57
58
+ // SetOption adds a new Option to the Section. If the option already exists, is replaced.
59
+ // The updated Section is returned.
33
60
func (s * Section ) SetOption (key string , value string ) * Section {
34
61
s .Options = s .Options .withSettedOption (key , value )
35
62
return s
36
63
}
37
64
65
+ // Remove an option with the specified key. The updated Section is returned.
38
66
func (s * Section ) RemoveOption (key string ) * Section {
39
67
s .Options = s .Options .withoutOption (key )
40
68
return s
41
69
}
42
70
71
+ // Subsection returns a Subsection from the specified Section. If the
72
+ // Subsection does not exists, new one is created and added to Section.
43
73
func (s * Section ) Subsection (name string ) * Subsection {
44
74
for i := len (s .Subsections ) - 1 ; i >= 0 ; i -- {
45
75
ss := s .Subsections [i ]
@@ -53,6 +83,7 @@ func (s *Section) Subsection(name string) *Subsection {
53
83
return ss
54
84
}
55
85
86
+ // HasSubsection checks if the Section has a Subsection with the specified name.
56
87
func (s * Section ) HasSubsection (name string ) bool {
57
88
for _ , ss := range s .Subsections {
58
89
if ss .IsName (name ) {
@@ -63,24 +94,31 @@ func (s *Section) HasSubsection(name string) bool {
63
94
return false
64
95
}
65
96
97
+ // IsName checks if the name of the subsection is exactly the specified name.
66
98
func (s * Subsection ) IsName (name string ) bool {
67
99
return s .Name == name
68
100
}
69
101
102
+ // Option returns an option with the specified key. If the option does not exists,
103
+ // empty spring will be returned.
70
104
func (s * Subsection ) Option (key string ) string {
71
105
return s .Options .Get (key )
72
106
}
73
107
108
+ // AddOption adds a new Option to the Subsection. The updated Subsection is returned.
74
109
func (s * Subsection ) AddOption (key string , value string ) * Subsection {
75
110
s .Options = s .Options .withAddedOption (key , value )
76
111
return s
77
112
}
78
113
114
+ // SetOption adds a new Option to the Subsection. If the option already exists, is replaced.
115
+ // The updated Subsection is returned.
79
116
func (s * Subsection ) SetOption (key string , value string ) * Subsection {
80
117
s .Options = s .Options .withSettedOption (key , value )
81
118
return s
82
119
}
83
120
121
+ // RemoveOption removes the option with the specified key. The updated Subsection is returned.
84
122
func (s * Subsection ) RemoveOption (key string ) * Subsection {
85
123
s .Options = s .Options .withoutOption (key )
86
124
return s
0 commit comments