-
Notifications
You must be signed in to change notification settings - Fork 7
/
etc.go
59 lines (48 loc) · 1011 Bytes
/
etc.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/alash3al/re-txt/handlers"
"github.com/urfave/cli/v2"
)
// wrapHandler wrap our handler into cli.ActionFunc
func wrapHandler(fn handlers.HandlerFunc) cli.ActionFunc {
return func(c *cli.Context) error {
sources := [][]byte{}
for _, src := range c.StringSlice("src") {
srcBytes, err := ioutil.ReadFile(src)
if err != nil {
return err
}
sources = append(sources, srcBytes)
}
{
stdin := os.Stdin
stat, err := stdin.Stat()
if err != nil {
return err
}
if (stat.Mode() & os.ModeDevice) == 0 {
stdinBytes, err := ioutil.ReadAll(stdin)
if err != nil {
return err
}
sources = append(sources, stdinBytes)
}
}
result, err := fn(&handlers.Context{
Context: c,
Input: sources,
})
if err != nil {
return err
}
if c.String("dest") != "" {
return ioutil.WriteFile(c.String("dest"), result, 0755)
} else {
fmt.Println(string(result))
}
return nil
}
}