Skip to content

Commit

Permalink
Merge pull request #298 from alenn-m/master
Browse files Browse the repository at this point in the history
Added config merge by wildcard
  • Loading branch information
moul committed Nov 28, 2018
2 parents ddc933c + 66d6a6a commit 2eea23d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go:
- tip

before_install:
- go get -u github.com/imdario/mergo
- go get -u github.com/axw/gocov/gocov
- go get -u github.com/mattn/goveralls
- go get -u golang.org/x/tools/cmd/cover
Expand Down
33 changes: 33 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/imdario/mergo"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -378,9 +379,41 @@ func (c *Config) LoadConfig(source io.Reader) error {
return err
}
c.applyMissingNames()
c.mergeWildCardEntries()
return nil
}

func (c *Config) mergeWildCardEntries() {
for k, host := range c.Hosts {
if strings.Contains(k, "*") {
continue
}

for key, subHost := range c.Hosts {
if strings.Contains(key, "*") {
keyParts := strings.Split(key, "*")
// if * is in the middle
if keyParts[0] != "" && keyParts[1] != "" {
// if the wildcard matches
if strings.Contains(k, keyParts[0]) && strings.Contains(k, keyParts[1]) {
if err := mergo.Merge(host, subHost); err != nil {
fmt.Println(err.Error())
}
}
} else {
tempKey := strings.Replace(key, "*", "", -1)
// if the wildcard matches
if strings.Contains(k, tempKey) {
if err := mergo.Merge(host, subHost); err != nil {
fmt.Println(err.Error())
}
}
}
}
}
}
}

func (c *Config) applyMissingNames() {
for key, host := range c.Hosts {
if host == nil {
Expand Down

0 comments on commit 2eea23d

Please sign in to comment.