Skip to content

Commit

Permalink
Add option to check counts
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
hahnjo committed Jul 21, 2019
1 parent 7e8714c commit 08d85f6
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 40 deletions.
9 changes: 6 additions & 3 deletions internal/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func Apply(args []string) {
c := dynconf.NewConfig(r.File)
input := c.GetInput()

orig, modified, err := dynconf.ApplyToFile(r, input)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
orig, modified, errs := dynconf.ApplyToFile(r, input)
if len(errs) != 0 {
fmt.Fprintf(os.Stderr, "Recipe '%s' coult not be applied:\n", file)
for _, e := range errs {
fmt.Printf("error: %s\n", e)
}
os.Exit(1)
}

Expand Down
9 changes: 6 additions & 3 deletions internal/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func Show(args []string) {
c := dynconf.NewConfig(r.File)
input := c.GetInput()

_, content, err := dynconf.ApplyToFile(r, input)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
_, content, errs := dynconf.ApplyToFile(r, input)
if len(errs) != 0 {
fmt.Fprintf(os.Stderr, "Recipe '%s' coult not be applied:\n", file)
for _, e := range errs {
fmt.Printf("error: %s\n", e)
}
os.Exit(1)
}

Expand Down
47 changes: 38 additions & 9 deletions pkg/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ package dynconf

import (
"bytes"
"fmt"
"io/ioutil"
)

func ApplyToFile(r Recipe, filename string) ([]byte, []byte, error) {
func ApplyToFile(r Recipe, filename string) ([]byte, []byte, []error) {
input, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
return nil, nil, []error{err}
}

return input, ApplyToInput(r, input), nil
modified, errs := ApplyToInput(r, input)
return input, modified, errs
}

func isNewLine(c byte) bool {
Expand All @@ -30,11 +32,11 @@ func containsOnlyNewLines(b []byte) bool {
return true
}

func applyReplacement(r ReplaceEntry, line []byte) []byte {
func applyReplacement(r ReplaceEntry, line []byte) ([]byte, int) {
s := r.SearchRegexp
allIndexes := s.FindAllSubmatchIndex(line, -1)
if allIndexes == nil {
return line
return line, 0
}

modified := make([]byte, 0)
Expand All @@ -48,7 +50,7 @@ func applyReplacement(r ReplaceEntry, line []byte) []byte {
// Append rest of line.
modified = append(modified, line[pos:]...)

return modified
return modified, len(allIndexes)
}

func applyAppend(r Recipe, modified []byte) []byte {
Expand All @@ -74,7 +76,7 @@ func applyAppend(r Recipe, modified []byte) []byte {
return modified
}

func ApplyToInput(r Recipe, input []byte) []byte {
func ApplyToInput(r Recipe, input []byte) ([]byte, []error) {
inLen := len(input)

deleteActive := []bool(nil)
Expand All @@ -91,6 +93,15 @@ func ApplyToInput(r Recipe, input []byte) []byte {
}
}

// Count number of matches for deletes and replacements.
errs := make([]error, 0)
deleteCount := []int(nil)
replaceCount := []int(nil)
if r.hasCount {
deleteCount = make([]int, len(r.Delete))
replaceCount = make([]int, len(r.Replace))
}

modified := make([]byte, 0)
// Loop over all lines and modify input.
idx := 0
Expand Down Expand Up @@ -137,14 +148,21 @@ func ApplyToInput(r Recipe, input []byte) []byte {
// Skip line if it matches a pattern that shall be deleted.
for idx, d := range r.Delete {
if (!r.hasContext || deleteActive[idx]) && d.SearchRegexp.Match(line) {
if r.hasCount {
deleteCount[idx]++
}
goto next
}
}

// Check if line matches a pattern that shall be replaced.
for idx, sr := range r.Replace {
if !r.hasContext || replaceActive[idx] {
line = applyReplacement(sr, line)
var count int
line, count = applyReplacement(sr, line)
if r.hasCount {
replaceCount[idx] += count
}
}
}
modified = append(modified, line...)
Expand All @@ -159,5 +177,16 @@ func ApplyToInput(r Recipe, input []byte) []byte {

modified = applyAppend(r, modified)

return modified
for idx, d := range r.Delete {
if d.CheckCount != 0 && d.CheckCount != deleteCount[idx] {
errs = append(errs, fmt.Errorf("Delete pattern '%s' applied %d times, expected %d!", d.Search, deleteCount[idx], d.CheckCount))
}
}
for idx, r := range r.Replace {
if r.CheckCount != 0 && r.CheckCount != replaceCount[idx] {
errs = append(errs, fmt.Errorf("Replace pattern '%s' applied %d times, expected %d!", r.Search, replaceCount[idx], r.CheckCount))
}
}

return modified, errs
}
Loading

0 comments on commit 08d85f6

Please sign in to comment.