-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_options.go
62 lines (51 loc) · 1.15 KB
/
cli_options.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
package main
import (
"fmt"
"io"
"os"
"github.com/ayushg3112/schmp"
)
type cliOptions struct {
mode string
outType string
outFile string
inFiles []string
}
func isOutTypeAllowed(outType string) bool {
return outType == "stdout" || outType == "json"
}
func (c *cliOptions) validate() []string {
valErrors := make([]string, 0)
if !isOutTypeAllowed(c.outType) {
valErrors = append(valErrors, fmt.Sprintf(`out-type "%s" is not allowed`, c.outType))
} else if c.outType != "stdout" && c.outFile == "" {
valErrors = append(valErrors, fmt.Sprintf("`out-file` is required if `out-type` is not stdout"))
}
return valErrors
}
func (c *cliOptions) toProcessingOptions() (schmp.ProcessingOptions, error) {
var err error
options := schmp.ProcessingOptions{
Mode: c.mode,
}
files := make([]io.ReadCloser, 0)
for _, v := range c.inFiles {
file, er := os.Open(v)
if er != nil {
err = er
break
}
files = append(files, file)
}
if err != nil {
for _, v := range files {
v.Close()
}
return options, err
}
options.Sources = make([]io.Reader, len(files))
for i, v := range files {
options.Sources[i] = v
}
return options, err
}