Skip to content

Commit

Permalink
Fix notify silently fails (#146)
Browse files Browse the repository at this point in the history
* set buffer to file size, split line into multiple chunks if size is greater than char-limit

* fix typo
  • Loading branch information
parrasajad authored Jun 10, 2022
1 parent b947254 commit 173f914
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,31 @@ func (r *Runner) Run() error {
return errors.New("notify works with stdin or file using -data flag")
}

info, err := inFile.Stat()
if err != nil {
return errors.Wrap(err, "could not get file info")
}
br := bufio.NewScanner(inFile)
maxSize := int(info.Size())
buffer := make([]byte, 0, maxSize)
br.Buffer(buffer, maxSize)

if r.options.Bulk {
br.Split(bulkSplitter(r.options.CharLimit))
}
for br.Scan() {
msg := br.Text()
//nolint:errcheck
r.sendMessage(msg)
if len(msg) > r.options.CharLimit {
// send the msg in chunks of length charLimit
for _, chunk := range SplitInChunks(msg, r.options.CharLimit) {
//nolint:errcheck
r.sendMessage(chunk)
}
} else {
//nolint:errcheck
r.sendMessage(msg)
}

}
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions internal/runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"bufio"
"math"
)

var ellipsis = []byte("...")
Expand Down Expand Up @@ -55,3 +56,21 @@ func bulkSplitter(charLimit int) bufio.SplitFunc {
return
}
}

// SplitInChunks splits a string into chunks of size charLimit
func SplitInChunks(data string, charLimit int) []string {
length := len(data)
noOfChunks := int(math.Ceil(float64(length) / float64(charLimit)))
chunks := make([]string, noOfChunks)
var start, stop int

for i := 0; i < noOfChunks; i += 1 {
start = i * charLimit
stop = start + charLimit
if stop > length {
stop = length
}
chunks[i] = data[start:stop]
}
return chunks
}

0 comments on commit 173f914

Please sign in to comment.