| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Section is the representation of a section inside git configuration files. |
| 9 |
// Each Section contains Options that are used by both the Git plumbing |
| 10 |
// and the porcelains. |
| 11 |
// Sections can be further divided into subsections. To begin a subsection |
| 12 |
// put its name in double quotes, separated by space from the section name, |
| 13 |
// in the section header, like in the example below: |
| 14 |
// |
| 15 |
// [section "subsection"] |
| 16 |
// |
| 17 |
// All the other lines (and the remainder of the line after the section header) |
| 18 |
// are recognized as option variables, in the form "name = value" (or just name, |
| 19 |
// which is a short-hand to say that the variable is the boolean "true"). |
| 20 |
// The variable names are case-insensitive, allow only alphanumeric characters |
| 21 |
// and -, and must start with an alphabetic character: |
| 22 |
// |
| 23 |
// [section "subsection1"] |
| 24 |
// option1 = value1 |
| 25 |
// option2 |
| 26 |
// [section "subsection2"] |
| 27 |
// option3 = value2 |
| 28 |
// |
| 29 |
type Section struct { |
| 30 |
Name string |
| 31 |
Options Options |
| 32 |
Subsections Subsections |
| 33 |
} |
| 34 |
|
| 35 |
type Subsection struct { |
| 36 |
Name string |
| 37 |
Options Options |
| 38 |
} |
| 39 |
|
| 40 |
type Sections []*Section |
| 41 |
|
| 42 |
func (s Sections) GoString() string { |
| 43 |
var strs []string |
| 44 |
for _, ss := range s { |
| 45 |
strs = append(strs, fmt.Sprintf("%#v", ss)) |
| 46 |
} |
| 47 |
|
| 48 |
return strings.Join(strs, ", ") |
| 49 |
} |
| 50 |
|
| 51 |
type Subsections []*Subsection |
| 52 |
|
| 53 |
func (s Subsections) GoString() string { |
| 54 |
var strs []string |
| 55 |
for _, ss := range s { |
| 56 |
strs = append(strs, fmt.Sprintf("%#v", ss)) |
| 57 |
} |
| 58 |
|
| 59 |
return strings.Join(strs, ", ") |
| 60 |
} |
| 61 |
|
| 62 |
// IsName checks if the name provided is equals to the Section name, case insensitive. |
| 63 |
func (s *Section) IsName(name string) bool { |
| 64 |
return strings.EqualFold(s.Name, name) |
| 65 |
} |
| 66 |
|
| 67 |
// Subsection returns a Subsection from the specified Section. If the |
| 68 |
// Subsection does not exists, new one is created and added to Section. |
| 69 |
func (s *Section) Subsection(name string) *Subsection { |
| 70 |
for i := len(s.Subsections) - 1; i >= 0; i-- { |
| 71 |
ss := s.Subsections[i] |
| 72 |
if ss.IsName(name) { |
| 73 |
return ss |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
ss := &Subsection{Name: name} |
| 78 |
s.Subsections = append(s.Subsections, ss) |
| 79 |
return ss |
| 80 |
} |
| 81 |
|
| 82 |
// HasSubsection checks if the Section has a Subsection with the specified name. |
| 83 |
func (s *Section) HasSubsection(name string) bool { |
| 84 |
for _, ss := range s.Subsections { |
| 85 |
if ss.IsName(name) { |
| 86 |
return true |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return false |
| 91 |
} |
| 92 |
|
| 93 |
// RemoveSubsection removes a subsection from a Section. |
| 94 |
func (s *Section) RemoveSubsection(name string) *Section { |
| 95 |
result := Subsections{} |
| 96 |
for _, s := range s.Subsections { |
| 97 |
if !s.IsName(name) { |
| 98 |
result = append(result, s) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
s.Subsections = result |
| 103 |
return s |
| 104 |
} |
| 105 |
|
| 106 |
// Option returns the value for the specified key. Empty string is returned if |
| 107 |
// key does not exists. |
| 108 |
func (s *Section) Option(key string) string { |
| 109 |
return s.Options.Get(key) |
| 110 |
} |
| 111 |
|
| 112 |
// OptionAll returns all possible values for an option with the specified key. |
| 113 |
// If the option does not exists, an empty slice will be returned. |
| 114 |
func (s *Section) OptionAll(key string) []string { |
| 115 |
return s.Options.GetAll(key) |
| 116 |
} |
| 117 |
|
| 118 |
// HasOption checks if the Section has an Option with the given key. |
| 119 |
func (s *Section) HasOption(key string) bool { |
| 120 |
return s.Options.Has(key) |
| 121 |
} |
| 122 |
|
| 123 |
// AddOption adds a new Option to the Section. The updated Section is returned. |
| 124 |
func (s *Section) AddOption(key string, value string) *Section { |
| 125 |
s.Options = s.Options.withAddedOption(key, value) |
| 126 |
return s |
| 127 |
} |
| 128 |
|
| 129 |
// SetOption adds a new Option to the Section. If the option already exists, is replaced. |
| 130 |
// The updated Section is returned. |
| 131 |
func (s *Section) SetOption(key string, value string) *Section { |
| 132 |
s.Options = s.Options.withSettedOption(key, value) |
| 133 |
return s |
| 134 |
} |
| 135 |
|
| 136 |
// Remove an option with the specified key. The updated Section is returned. |
| 137 |
func (s *Section) RemoveOption(key string) *Section { |
| 138 |
s.Options = s.Options.withoutOption(key) |
| 139 |
return s |
| 140 |
} |
| 141 |
|
| 142 |
// IsName checks if the name of the subsection is exactly the specified name. |
| 143 |
func (s *Subsection) IsName(name string) bool { |
| 144 |
return s.Name == name |
| 145 |
} |
| 146 |
|
| 147 |
// Option returns an option with the specified key. If the option does not exists, |
| 148 |
// empty spring will be returned. |
| 149 |
func (s *Subsection) Option(key string) string { |
| 150 |
return s.Options.Get(key) |
| 151 |
} |
| 152 |
|
| 153 |
// OptionAll returns all possible values for an option with the specified key. |
| 154 |
// If the option does not exists, an empty slice will be returned. |
| 155 |
func (s *Subsection) OptionAll(key string) []string { |
| 156 |
return s.Options.GetAll(key) |
| 157 |
} |
| 158 |
|
| 159 |
// HasOption checks if the Subsection has an Option with the given key. |
| 160 |
func (s *Subsection) HasOption(key string) bool { |
| 161 |
return s.Options.Has(key) |
| 162 |
} |
| 163 |
|
| 164 |
// AddOption adds a new Option to the Subsection. The updated Subsection is returned. |
| 165 |
func (s *Subsection) AddOption(key string, value string) *Subsection { |
| 166 |
s.Options = s.Options.withAddedOption(key, value) |
| 167 |
return s |
| 168 |
} |
| 169 |
|
| 170 |
// SetOption adds a new Option to the Subsection. If the option already exists, is replaced. |
| 171 |
// The updated Subsection is returned. |
| 172 |
func (s *Subsection) SetOption(key string, value ...string) *Subsection { |
| 173 |
s.Options = s.Options.withSettedOption(key, value...) |
| 174 |
return s |
| 175 |
} |
| 176 |
|
| 177 |
// RemoveOption removes the option with the specified key. The updated Subsection is returned. |
| 178 |
func (s *Subsection) RemoveOption(key string) *Subsection { |
| 179 |
s.Options = s.Options.withoutOption(key) |
| 180 |
return s |
| 181 |
} |