Skip to content

Commit

Permalink
Prepare for recursion support
Browse files Browse the repository at this point in the history
  • Loading branch information
mkatychev committed Feb 2, 2021
1 parent 9a5fc1b commit 55aa171
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 265 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ var3 = {path = [[], ".jsonMap"], type = "json"}
## further references

[TOML spec](https://toml.io/en/v1.0.0-rc.3#keyvalue-pair)

[envsubst](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) cheatsheet:
[yq expressions](https://mikefarah.gitbook.io/yq/)


| __Expression__ | __Meaning__ |
Expand All @@ -113,7 +113,7 @@ var3 = {path = [[], ".jsonMap"], type = "json"}
| `${#var}` | String length of `$var`
| `${var:n}` | Offset `$var` `n` characters from start
| `${var: -n}` | Offset `$var` `n` characters from end
| `${var:n:len}` | Offset `$var` `n` characters of `len` length
| `${var:n:len}` | Offset `$var` `n` characters with max length of `len`
| `${var#pattern}` | Strip shortest `pattern` match from start
| `${var##pattern}` | Strip longest `pattern` match from start
| `${var%pattern}` | Strip shortest `pattern` match from end
Expand Down
10 changes: 3 additions & 7 deletions cmd/cogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,13 @@ func main() {

format, err := conf.validate()
ifErr(err)

cfgMap, err := cogs.Generate(conf.Ctx, conf.File, format)
ifErr(err)

cfgMap, err = conf.filterCfgMap(cfgMap)
cfgMap, err := cogs.Generate(conf.Ctx, conf.File, format, conf.filterCfgMap)
ifErr(err)

switch format {
case cogs.JSON:
b, err = json.MarshalIndent(cfgMap, "", " ")
output = string(b)
output = string(b) + "\n"
case cogs.YAML:
b, err = yaml.Marshal(cfgMap)
output = string(b)
Expand Down Expand Up @@ -111,6 +107,6 @@ func main() {
}
ifErr(err)

fmt.Fprintln(os.Stdout, output)
fmt.Fprint(os.Stdout, output)
}
}
28 changes: 3 additions & 25 deletions cmd/cogs/optparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,15 @@ func modKeys(cfgMap map[string]interface{}, modFn ...func(string) string) map[st
return newCfgMap
}

// exclude produces a laundered map with exclusionList values missing
func exclude(exclusionList []string, cfgMap map[string]interface{}) map[string]interface{} {
newCfgMap := make(map[string]interface{})

for k := range cfgMap {
if inList(k, exclusionList) {
continue
}
newCfgMap[k] = cfgMap[k]
}
return newCfgMap
}

func inList(s string, ss []string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}

// filterCfgMap retains only key names passed to --keys
func (c *Conf) filterCfgMap(cfgMap map[string]interface{}) (map[string]interface{}, error) {
func (c *Conf) filterCfgMap(cfgMap cogs.CfgMap) (cogs.CfgMap, error) {

// --not runs before --keys!
// make sure to avoid --not=key_name --key=key_name, ya dingus!
var notList []string
if c.Not != "" {
notList = strings.Split(c.Not, ",")
cfgMap = exclude(notList, cfgMap)
cfgMap = cogs.Exclude(notList, cfgMap)
}
if c.Keys == "" {
return cfgMap, nil
Expand All @@ -101,7 +79,7 @@ func (c *Conf) filterCfgMap(cfgMap map[string]interface{}) (map[string]interface
var ok bool
if newCfgMap[key], ok = cfgMap[key]; !ok {
hint := ""
if inList(key, notList) {
if cogs.InList(key, notList) {
hint = fmt.Sprintf("\n\n--not=%s and --keys=%s were called\n"+
"avoid trying to include and exclude the same value, ya dingus!", key, key)
}
Expand Down
Loading

0 comments on commit 55aa171

Please sign in to comment.