diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-03 17:10:43 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-03 17:10:43 +0100 |
commit | 3f7fbc6c49e50eb22e3de8a5143817fa7c0c54dd (patch) | |
tree | 0bf81a6dd858278000d1d7f7afc578d993fba791 /formats/config | |
parent | 94f5e9c949963893d1c3d3e987a591ee15265327 (diff) | |
download | go-git-3f7fbc6c49e50eb22e3de8a5143817fa7c0c54dd.tar.gz |
storage/filesystem: implement missing functionality. (#110)
* storage/filesystem: added ObjectStorage Set.
* storage/filesystem: now passes all tests, except those specific to transactions.
* formats/config: Encoder now encodes subsections with no options.
* formats/config: add HasSubsection on Section.
* dotgit: add Ref method to get specific reference.
Diffstat (limited to 'formats/config')
-rw-r--r-- | formats/config/common_test.go | 12 | ||||
-rw-r--r-- | formats/config/encoder.go | 65 | ||||
-rw-r--r-- | formats/config/section.go | 10 |
3 files changed, 67 insertions, 20 deletions
diff --git a/formats/config/common_test.go b/formats/config/common_test.go index 0bc4d2d..365b53f 100644 --- a/formats/config/common_test.go +++ b/formats/config/common_test.go @@ -13,7 +13,7 @@ type CommonSuite struct{} var _ = Suite(&CommonSuite{}) func (s *CommonSuite) TestConfig_SetOption(c *C) { - obtained := New().SetOption("section", "", "key1", "value1") + obtained := New().SetOption("section", NoSubsection, "key1", "value1") expected := &Config{ Sections: []*Section{ { @@ -25,7 +25,7 @@ func (s *CommonSuite) TestConfig_SetOption(c *C) { }, } c.Assert(obtained, DeepEquals, expected) - obtained = obtained.SetOption("section", "", "key1", "value1") + obtained = obtained.SetOption("section", NoSubsection, "key1", "value1") c.Assert(obtained, DeepEquals, expected) obtained = New().SetOption("section", "subsection", "key1", "value1") @@ -50,7 +50,7 @@ func (s *CommonSuite) TestConfig_SetOption(c *C) { } func (s *CommonSuite) TestConfig_AddOption(c *C) { - obtained := New().AddOption("section", "", "key1", "value1") + obtained := New().AddOption("section", NoSubsection, "key1", "value1") expected := &Config{ Sections: []*Section{ { @@ -66,10 +66,10 @@ func (s *CommonSuite) TestConfig_AddOption(c *C) { func (s *CommonSuite) TestConfig_RemoveSection(c *C) { sect := New(). - AddOption("section1", "", "key1", "value1"). - AddOption("section2", "", "key1", "value1") + AddOption("section1", NoSubsection, "key1", "value1"). + AddOption("section2", NoSubsection, "key1", "value1") expected := New(). - AddOption("section1", "", "key1", "value1") + AddOption("section1", NoSubsection, "key1", "value1") c.Assert(sect.RemoveSection("other"), DeepEquals, sect) c.Assert(sect.RemoveSection("section2"), DeepEquals, expected) } diff --git a/formats/config/encoder.go b/formats/config/encoder.go index 4a68e44..88bdf65 100644 --- a/formats/config/encoder.go +++ b/formats/config/encoder.go @@ -7,7 +7,7 @@ import ( // An Encoder writes config files to an output stream. type Encoder struct { - io.Writer + w io.Writer } // NewEncoder returns a new encoder that writes to w. @@ -18,21 +18,58 @@ func NewEncoder(w io.Writer) *Encoder { // Encode writes the config in git config format to the stream of the encoder. func (e *Encoder) Encode(cfg *Config) error { for _, s := range cfg.Sections { - if len(s.Options) > 0 { - fmt.Fprintf(e, "[%s]\n", s.Name) - for _, o := range s.Options { - fmt.Fprintf(e, "\t%s = %s\n", o.Key, o.Value) - } + if err := e.encodeSection(s); err != nil { + return err } - for _, ss := range s.Subsections { - if len(ss.Options) > 0 { - //TODO: escape - fmt.Fprintf(e, "[%s \"%s\"]\n", s.Name, ss.Name) - for _, o := range ss.Options { - fmt.Fprintf(e, "\t%s = %s\n", o.Key, o.Value) - } - } + } + + return nil +} + +func (e *Encoder) encodeSection(s *Section) error { + if len(s.Options) > 0 { + if err := e.printf("[%s]\n", s.Name); err != nil { + return err + } + + if err := e.encodeOptions(s.Options); err != nil { + return err + } + } + + for _, ss := range s.Subsections { + if err := e.encodeSubsection(s.Name, ss); err != nil { + return err + } + } + + return nil +} + +func (e *Encoder) encodeSubsection(sectionName string, s *Subsection) error { + //TODO: escape + if err := e.printf("[%s \"%s\"]\n", sectionName, s.Name); err != nil { + return err + } + + if err := e.encodeOptions(s.Options); err != nil { + return err + } + + return nil +} + +func (e *Encoder) encodeOptions(opts Options) error { + for _, o := range opts { + if err := e.printf("\t%s = %s\n", o.Key, o.Value); err != nil { + return err } } + return nil } + +func (e *Encoder) printf(msg string, args ...interface{}) error { + _, err := fmt.Fprintf(e.w, msg, args...) + return err +} diff --git a/formats/config/section.go b/formats/config/section.go index 6d6a891..552ce74 100644 --- a/formats/config/section.go +++ b/formats/config/section.go @@ -74,3 +74,13 @@ func (s *Section) Subsection(name string) *Subsection { s.Subsections = append(s.Subsections, ss) return ss } + +func (s *Section) HasSubsection(name string) bool { + for _, ss := range s.Subsections { + if ss.IsName(name) { + return true + } + } + + return false +} |