-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
97 lines (90 loc) · 1.9 KB
/
types.go
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
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
type Input struct {
Origin, Trimmed string
}
func InputsFromFile(file string) (string, []*Input, error) {
origin := fmt.Sprintf("from file %q", file)
f, err := os.Open(file)
if err != nil {
return origin, nil, err
}
scanner := bufio.NewScanner(f)
lines := make([]string, 0, 8)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
r := NewInputs(lines)
if len(r) == 0 {
return origin, nil, errors.New("no valid inputs")
}
return origin, r, nil
}
func InputsFromArgsOrPipeline(args []string, batch bool) (string, []*Input, error) {
var origin string
switch len(args) {
case 0:
//check pipeline input
var ok bool
origin, ok = checkGetPipelineInput()
if !ok {
return "", nil, errors.New("no input from pipeline or args")
}
case 1:
origin = args[0]
default:
return "", nil, errors.New("too many args")
}
var inputs []string
if batch {
first := strings.Split(origin, "\n")
for _, str := range first {
inputs = append(inputs, strings.Split(str, ";")...)
}
} else {
inputs = []string{origin}
}
r := NewInputs(inputs)
if len(r) == 0 {
return origin, nil, errors.New("no valid inputs")
}
return origin, r, nil
}
func checkGetPipelineInput() (string, bool) {
info, err := os.Stdin.Stat()
if err != nil {
return "", false
}
if (info.Mode()&os.ModeNamedPipe) == os.ModeNamedPipe ||
((info.Mode()&os.ModeCharDevice) == os.ModeCharDevice && info.Size() > 0) {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return "", false
}
return string(bytes), true
}
return "", false
}
func NewInputs(inputs []string) []*Input {
r := make([]*Input, 0, len(inputs))
for _, o := range inputs {
t := trimInput(o)
if t != "" {
r = append(r, &Input{
Origin: o,
Trimmed: t,
})
}
}
return r
}
func trimInput(s string) string {
return strings.Trim(s, "'\"\r\n ")
}