Skip to content

Commit

Permalink
Merge pull request #112 from MichaelMure/complete-config
Browse files Browse the repository at this point in the history
config: add missing functions for completeness
  • Loading branch information
mcuadros committed Oct 9, 2020
2 parents 63d9253 + b40ca79 commit b5b59f5
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 56 deletions.
54 changes: 32 additions & 22 deletions plumbing/format/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,14 @@ func (c *Config) Section(name string) *Section {
return s
}

// AddOption adds an option to a given section and subsection. Use the
// NoSubsection constant for the subsection argument if no subsection is wanted.
func (c *Config) AddOption(section string, subsection string, key string, value string) *Config {
if subsection == "" {
c.Section(section).AddOption(key, value)
} else {
c.Section(section).Subsection(subsection).AddOption(key, value)
}

return c
}

// SetOption sets an option to a given section and subsection. Use the
// NoSubsection constant for the subsection argument if no subsection is wanted.
func (c *Config) SetOption(section string, subsection string, key string, value string) *Config {
if subsection == "" {
c.Section(section).SetOption(key, value)
} else {
c.Section(section).Subsection(subsection).SetOption(key, value)
// HasSection checks if the Config has a section with the specified name.
func (c *Config) HasSection(name string) bool {
for _, s := range c.Sections {
if s.IsName(name) {
return true
}
}

return c
return false
}

// RemoveSection removes a section from a config file.
Expand All @@ -81,7 +67,7 @@ func (c *Config) RemoveSection(name string) *Config {
return c
}

// RemoveSubsection remove s a subsection from a config file.
// RemoveSubsection remove a subsection from a config file.
func (c *Config) RemoveSubsection(section string, subsection string) *Config {
for _, s := range c.Sections {
if s.IsName(section) {
Expand All @@ -97,3 +83,27 @@ func (c *Config) RemoveSubsection(section string, subsection string) *Config {

return c
}

// AddOption adds an option to a given section and subsection. Use the
// NoSubsection constant for the subsection argument if no subsection is wanted.
func (c *Config) AddOption(section string, subsection string, key string, value string) *Config {
if subsection == "" {
c.Section(section).AddOption(key, value)
} else {
c.Section(section).Subsection(subsection).AddOption(key, value)
}

return c
}

// SetOption sets an option to a given section and subsection. Use the
// NoSubsection constant for the subsection argument if no subsection is wanted.
func (c *Config) SetOption(section string, subsection string, key string, value string) *Config {
if subsection == "" {
c.Section(section).SetOption(key, value)
} else {
c.Section(section).Subsection(subsection).SetOption(key, value)
}

return c
}
8 changes: 8 additions & 0 deletions plumbing/format/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func (s *CommonSuite) TestConfig_AddOption(c *C) {
c.Assert(obtained, DeepEquals, expected)
}

func (s *CommonSuite) TestConfig_HasSection(c *C) {
sect := New().
AddOption("section1", "sub1", "key1", "value1").
AddOption("section1", "sub2", "key1", "value1")
c.Assert(sect.HasSection("section1"), Equals, true)
c.Assert(sect.HasSection("section2"), Equals, false)
}

func (s *CommonSuite) TestConfig_RemoveSection(c *C) {
sect := New().
AddOption("section1", NoSubsection, "key1", "value1").
Expand Down
10 changes: 10 additions & 0 deletions plumbing/format/config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func (opts Options) Get(key string) string {
return ""
}

// Has checks if an Option exist with the given key.
func (opts Options) Has(key string) bool {
for _, o := range opts {
if o.IsKey(key) {
return true
}
}
return false
}

// GetAll returns all possible values for the same key.
func (opts Options) GetAll(key string) []string {
result := []string{}
Expand Down
15 changes: 15 additions & 0 deletions plumbing/format/config/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ type OptionSuite struct{}

var _ = Suite(&OptionSuite{})

func (s *OptionSuite) TestOptions_Has(c *C) {
o := Options{
&Option{"k", "v"},
&Option{"ok", "v1"},
&Option{"K", "v2"},
}
c.Assert(o.Has("k"), Equals, true)
c.Assert(o.Has("K"), Equals, true)
c.Assert(o.Has("ok"), Equals, true)
c.Assert(o.Has("unexistant"), Equals, false)

o = Options{}
c.Assert(o.Has("k"), Equals, false)
}

func (s *OptionSuite) TestOptions_GetAll(c *C) {
o := Options{
&Option{"k", "v"},
Expand Down
85 changes: 60 additions & 25 deletions plumbing/format/config/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,6 @@ func (s *Section) IsName(name string) bool {
return strings.EqualFold(s.Name, name)
}

// Option return the value for the specified key. Empty string is returned if
// key does not exists.
func (s *Section) Option(key string) string {
return s.Options.Get(key)
}

// AddOption adds a new Option to the Section. The updated Section is returned.
func (s *Section) AddOption(key string, value string) *Section {
s.Options = s.Options.withAddedOption(key, value)
return s
}

// SetOption adds a new Option to the Section. If the option already exists, is replaced.
// The updated Section is returned.
func (s *Section) SetOption(key string, value string) *Section {
s.Options = s.Options.withSettedOption(key, value)
return s
}

// Remove an option with the specified key. The updated Section is returned.
func (s *Section) RemoveOption(key string) *Section {
s.Options = s.Options.withoutOption(key)
return s
}

// Subsection returns a Subsection from the specified Section. If the
// Subsection does not exists, new one is created and added to Section.
func (s *Section) Subsection(name string) *Subsection {
Expand All @@ -115,6 +90,55 @@ func (s *Section) HasSubsection(name string) bool {
return false
}

// RemoveSubsection removes a subsection from a Section.
func (s *Section) RemoveSubsection(name string) *Section {
result := Subsections{}
for _, s := range s.Subsections {
if !s.IsName(name) {
result = append(result, s)
}
}

s.Subsections = result
return s
}

// Option return the value for the specified key. Empty string is returned if
// key does not exists.
func (s *Section) Option(key string) string {
return s.Options.Get(key)
}

// OptionAll returns all possible values for an option with the specified key.
// If the option does not exists, an empty slice will be returned.
func (s *Section) OptionAll(key string) []string {
return s.Options.GetAll(key)
}

// HasOption checks if the Section has an Option with the given key.
func (s *Section) HasOption(key string) bool {
return s.Options.Has(key)
}

// AddOption adds a new Option to the Section. The updated Section is returned.
func (s *Section) AddOption(key string, value string) *Section {
s.Options = s.Options.withAddedOption(key, value)
return s
}

// SetOption adds a new Option to the Section. If the option already exists, is replaced.
// The updated Section is returned.
func (s *Section) SetOption(key string, value string) *Section {
s.Options = s.Options.withSettedOption(key, value)
return s
}

// Remove an option with the specified key. The updated Section is returned.
func (s *Section) RemoveOption(key string) *Section {
s.Options = s.Options.withoutOption(key)
return s
}

// IsName checks if the name of the subsection is exactly the specified name.
func (s *Subsection) IsName(name string) bool {
return s.Name == name
Expand All @@ -126,6 +150,17 @@ func (s *Subsection) Option(key string) string {
return s.Options.Get(key)
}

// OptionAll returns all possible values for an option with the specified key.
// If the option does not exists, an empty slice will be returned.
func (s *Subsection) OptionAll(key string) []string {
return s.Options.GetAll(key)
}

// HasOption checks if the Subsection has an Option with the given key.
func (s *Subsection) HasOption(key string) bool {
return s.Options.Has(key)
}

// AddOption adds a new Option to the Subsection. The updated Subsection is returned.
func (s *Subsection) AddOption(key string, value string) *Subsection {
s.Options = s.Options.withAddedOption(key, value)
Expand Down

0 comments on commit b5b59f5

Please sign in to comment.