-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadbuf.go
79 lines (67 loc) · 1.69 KB
/
readbuf.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 main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
"github.com/scgolang/sc"
)
// ReadBuf reads a buffer.
type ReadBuf struct {
Channels IntFlags
Num int
flagErrorHandling flag.ErrorHandling
scc *sc.Client
}
// Run runs the command.
func (rb ReadBuf) Run(args []string) error {
fs := flag.NewFlagSet("readbuf", rb.flagErrorHandling)
fs.Var(&rb.Channels, "channel", "channel(s) to read")
fs.IntVar(&rb.Num, "num", 0, "buffer number")
if err := fs.Parse(args); err != nil {
return ErrUsage
}
if len(fs.Args()) == 0 {
return ErrUsage
}
p, err := filepath.Abs(fs.Args()[0])
if err != nil {
return errors.Wrap(err, "making path absolute")
}
// Check that the file exists and is readable.
f, err := os.Open(p)
if err != nil {
return errors.Wrap(err, "opening file for reading")
}
_ = f.Close()
// Read the buffer.
_, err = rb.scc.ReadBuffer(p, int32(rb.Num), rb.Channels...)
return errors.Wrap(err, "reading buffer")
}
// Usage prints a usage message.
func (rb ReadBuf) Usage() {
fmt.Fprint(os.Stderr, `
scc [GLOBAL OPTIONS] readbuf [OPTIONS] FILE
OPTIONS
-channel (OPTIONAL) Channel(s) to read. Can be passed multiple times. Indices start at 0.
-num (REQUIRED) Buffer number.
FILE will be converted to an absolute path.
`)
}
// IntFlags provides a way to pass multiple int flags.
type IntFlags []int
// String converts intflags to a string.
func (ifs IntFlags) String() string {
return fmt.Sprintf("%#v", ifs)
}
// Set sets intflags from the provided string.
func (ifs *IntFlags) Set(s string) error {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*ifs = append(*ifs, int(i))
return nil
}