-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.go
79 lines (66 loc) · 1.36 KB
/
cat.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
package command
import (
"flag"
"fmt"
"io"
"io/fs"
"github.com/jarxorg/fssh"
)
type cat struct {
flagSet *flag.FlagSet
}
func newCat() fssh.Command {
return &cat{}
}
func (c *cat) Name() string {
return "cat"
}
func (c *cat) Description() string {
return "concatenate and print files"
}
func (c *cat) FlagSet() *flag.FlagSet {
if c.flagSet == nil {
s := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
s.Usage = func() {}
c.flagSet = s
}
return c.flagSet
}
func (c *cat) Reset() {
}
func (c *cat) Exec(sh *fssh.Shell) error {
args := c.FlagSet().Args()
if len(args) == 0 {
c.Usage(sh.Stderr)
return nil
}
fsys, name, err := sh.SubFS(args[0])
if err != nil {
return err
}
bin, err := fs.ReadFile(fsys, name)
if err != nil {
return err
}
fmt.Fprintf(sh.Stdout, "%s", bin)
if len(bin) > 0 && bin[len(bin)-1] != '\n' {
fmt.Fprint(sh.Stdout, "\n")
}
return nil
}
func (c *cat) AutoCompleter() fssh.AutoCompleterFunc {
return c.autoComplete
}
func (c *cat) autoComplete(sh *fssh.Shell, arg string) ([]string, error) {
return sh.PrefixMatcher.MatchFiles(sh, arg)
}
func (c *cat) Usage(w io.Writer) {
name := c.Name()
fmt.Fprintf(w, "Usage:\n %s [file]\n", name)
fmt.Fprintln(w, "Examples:")
fmt.Fprintf(w, " %s FILE\n", name)
fmt.Fprintf(w, " %s (s3|gs)://BUCKET/DIR/FILE\n", name)
}
func init() {
fssh.RegisterNewCommandFunc(newCat)
}