-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
54 lines (46 loc) · 889 Bytes
/
helpers.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
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
"github.com/urfave/cli/v3"
)
func getStdin() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
read := bytes.NewBuffer(make([]byte, 0, 1000))
_, err := io.Copy(read, os.Stdin)
if err == nil {
return read.String()
}
}
return ""
}
func getStdinBytes() []byte {
stdin := getStdin()
if b, err := hex.DecodeString(stdin); err == nil {
return b
}
if b, err := base64.StdEncoding.DecodeString(stdin); err == nil {
return b
}
return []byte(stdin)
}
func readFromStdinOrFile(c *cli.Command) ([]byte, error) {
b := getStdinBytes()
if len(b) > 0 {
return b, nil
}
file := c.Args().First()
if file == "" {
return nil, fmt.Errorf("no files or stdin given")
}
b, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return b, nil
}