-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclient.go
67 lines (56 loc) · 1.78 KB
/
client.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
package editor
import (
"io"
)
// Client is an interface for the entrypoint of editor package
type Client interface {
// Edit reads a HCL file and appies a given filter.
// If filename is `-`, reads the input from stdin.
// If update is true, the outputs is written to the input file, else to stdout.
Edit(filename string, update bool, filter Filter) error
// Derive reads a HCL file and appies a given sink.
// If filename is `-`, reads the input from stdin.
// The outputs is always written to stdout.
Derive(filename string, sink Sink) error
}
// Option is a set of options for Client.
type Option struct {
// InStream is the stdin stream.
InStream io.Reader
// OutStream is the stdout stream.
OutStream io.Writer
// ErrStream is the stderr stream.
ErrStream io.Writer
}
// client implements the Client interface.
type client struct {
o *Option
}
var _ Client = (*client)(nil)
// NewClient creates a new instance of Client.
func NewClient(o *Option) Client {
return &client{
o: o,
}
}
// Edit reads a HCL file and appies a given filter.
// If filename is `-`, reads the input from stdin.
// If update is true, the outputs is written to the input file, else to stdout.
func (c *client) Edit(filename string, update bool, filter Filter) error {
if filename == "-" {
return EditStream(c.o.InStream, c.o.OutStream, filename, filter)
}
if update {
return UpdateFile(filename, filter)
}
return ReadFile(filename, c.o.OutStream, filter)
}
// Derive reads a HCL file and appies a given sink.
// If filename is `-`, reads the input from stdin.
// The outputs is always written to stdout.
func (c *client) Derive(filename string, sink Sink) error {
if filename == "-" {
return DeriveStream(c.o.InStream, c.o.OutStream, filename, sink)
}
return DeriveFile(filename, c.o.OutStream, sink)
}