diff options
author | Santiago M. Mola <santi@mola.io> | 2016-10-26 18:41:39 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-26 16:41:39 +0000 |
commit | 61f0188edcea55dbcfa1c3a35da0c34fed10fd54 (patch) | |
tree | 08b59ecd20719f448d64073845bf40cf2d86fd7c /formats/config/section.go | |
parent | 194da90f885d4cb7cf2bf4c84a74e5d559000764 (diff) | |
download | go-git-61f0188edcea55dbcfa1c3a35da0c34fed10fd54.tar.gz |
formats/config: Added encoder/decoder for git config files. (#97)
* WIP: Add config format parser.
* add decoder based on gcfg.
Portions of code taken from:
https://github.com/go-gcfg/gcfg/blob/5b9f94ee80b2331c3982477bd84be8edd857df33/read.go
* add git config encoder and config methods.
* use format/config in storage/filesystem for read
* use format/config in storage/filesystem to write
* formats/config: improve docs.
* formats/config: improve tests.
* formats/config: use our fork of gcfg; improve api.
* formats/config: improve api.
* storage/filesystem: fix gofmt
* formats/config: use NoSubsection constant.
* formats/config: add doc.go
* formats/config: requested sytle changes.
* formats/config: do not use *_test packages.
Diffstat (limited to 'formats/config/section.go')
-rw-r--r-- | formats/config/section.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/formats/config/section.go b/formats/config/section.go new file mode 100644 index 0000000..6d6a891 --- /dev/null +++ b/formats/config/section.go @@ -0,0 +1,76 @@ +package config + +import "strings" + +type Section struct { + Name string + Options Options + Subsections Subsections +} + +type Subsection struct { + Name string + Options Options +} + +type Sections []*Section + +type Subsections []*Subsection + +func (s *Section) IsName(name string) bool { + return strings.ToLower(s.Name) == strings.ToLower(name) +} + +func (s *Subsection) IsName(name string) bool { + return s.Name == name +} + +func (s *Section) Option(key string) string { + return s.Options.Get(key) +} + +func (s *Subsection) Option(key string) string { + return s.Options.Get(key) +} + +func (s *Section) AddOption(key string, value string) *Section { + s.Options = s.Options.withAddedOption(key, value) + return s +} + +func (s *Subsection) AddOption(key string, value string) *Subsection { + s.Options = s.Options.withAddedOption(key, value) + return s +} + +func (s *Section) SetOption(key string, value string) *Section { + s.Options = s.Options.withSettedOption(key, value) + return s +} + +func (s *Section) RemoveOption(key string) *Section { + s.Options = s.Options.withoutOption(key) + return s +} + +func (s *Subsection) SetOption(key string, value string) *Subsection { + s.Options = s.Options.withSettedOption(key, value) + return s +} + +func (s *Subsection) RemoveOption(key string) *Subsection { + s.Options = s.Options.withoutOption(key) + return s +} + +func (s *Section) Subsection(name string) *Subsection { + for i := len(s.Subsections) - 1; i >= 0; i-- { + ss := s.Subsections[i] + if ss.IsName(name) { + return ss + } + } + ss := &Subsection{Name: name} + s.Subsections = append(s.Subsections, ss) + return ss +} |