1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package cliutil
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/shenwei356/breader"
)
// ReadKVs parse two-column (key\tvalue) tab-delimited file(s).
func ReadKVs(file string, ignoreCase bool) (map[string]string, error) {
type KV [2]string
fn := func(line string) (interface{}, bool, error) {
if len(line) > 0 && line[len(line)-1] == '\n' {
line = line[:len(line)-1]
}
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
if len(line) == 0 {
return nil, false, nil
}
items := strings.Split(line, "\t")
if len(items) < 2 {
return nil, false, nil
}
if ignoreCase {
return KV([2]string{strings.ToLower(items[0]), items[1]}), true, nil
}
return KV([2]string{items[0], items[1]}), true, nil
}
kvs := make(map[string]string)
reader, err := breader.NewBufferedReader(file, 4, 100, fn)
if err != nil {
return kvs, err
}
var items KV
var data interface{}
for chunk := range reader.Ch {
if chunk.Err != nil {
return kvs, err
}
for _, data = range chunk.Data {
items = data.(KV)
kvs[items[0]] = items[1]
}
}
return kvs, nil
}
// DropCR removes last "\r" if it is.
func DropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}
// DropLF removes "\n"
func DropLF(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\n' {
return data[0 : len(data)-1]
}
return data
}
func GetFileList(args []string, checkFile bool) []string {
files := make([]string, 0, 1000)
if len(args) == 0 {
files = append(files, "-")
} else {
for _, file := range args {
if isStdin(file) {
continue
}
if !checkFile {
continue
}
if _, err := os.Stat(file); os.IsNotExist(err) {
CheckError(errors.Wrap(err, file))
}
}
files = args
}
return files
}
func GetFileListFromFile(file string, checkFile bool) ([]string, error) {
fh, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("read file list from '%s': %s", file, err)
}
var _file string
lists := make([]string, 0, 1000)
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
_file = scanner.Text()
if strings.TrimSpace(_file) == "" {
continue
}
if checkFile && !isStdin(_file) {
if _, err = os.Stat(_file); os.IsNotExist(err) {
return lists, fmt.Errorf("check file '%s': %s", _file, err)
}
}
lists = append(lists, _file)
}
if err = scanner.Err(); err != nil {
return nil, fmt.Errorf("read file list from '%s': %s", file, err)
}
return lists, nil
}
|