Helper API refactor (#40)
* api cleaup * updates * wtf * updates * snapshot. * tweaks * snapshot * api tweaks. * updates * updates * updates * changes. * updates * updates * sequence => seq * dont need to use curl, just using wget * fixing examples
This commit is contained in:
parent
43212f871f
commit
03708a90ef
100 changed files with 1687 additions and 1055 deletions
57
util/file_util.go
Normal file
57
util/file_util.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
// File contains file utility functions
|
||||
File = fileUtil{}
|
||||
)
|
||||
|
||||
type fileUtil struct{}
|
||||
|
||||
// ReadByLines reads a file and calls the handler for each line.
|
||||
func (fu fileUtil) ReadByLines(filePath string, handler func(line string) error) error {
|
||||
var f *os.File
|
||||
var err error
|
||||
if f, err = os.Open(filePath); err == nil {
|
||||
defer f.Close()
|
||||
var line string
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line = scanner.Text()
|
||||
err = handler(line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
// ReadByChunks reads a file in `chunkSize` pieces, dispatched to the handler.
|
||||
func (fu fileUtil) ReadByChunks(filePath string, chunkSize int, handler func(line []byte) error) error {
|
||||
var f *os.File
|
||||
var err error
|
||||
if f, err = os.Open(filePath); err == nil {
|
||||
defer f.Close()
|
||||
|
||||
chunk := make([]byte, chunkSize)
|
||||
for {
|
||||
readBytes, err := f.Read(chunk)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
readData := chunk[:readBytes]
|
||||
err = handler(readData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue