-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.go
210 lines (170 loc) · 4.93 KB
/
file.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Commands for reading and writing files.
package file
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"github.com/ghetzel/friendscript/utils"
defaults "github.com/mcuadros/go-defaults"
)
type Commands struct {
utils.Module
env utils.Runtime
}
func New(env utils.Runtime) *Commands {
cmd := &Commands{
env: env,
}
cmd.Module = utils.NewDefaultExecutor(cmd)
return cmd
}
type TempArgs struct {
// A string to prefix temporary filenames with
Prefix string `json:"prefix" default:"friendscript-"`
}
func (self *Commands) Temp(args *TempArgs) (*os.File, error) {
if args == nil {
args = &TempArgs{}
}
defaults.SetDefaults(args)
return ioutil.TempFile(``, args.Prefix)
}
type ReadArgs struct {
// Whether to attempt to close the source (if possible) after reading.
Autoclose bool `json:"autoclose" default:"true"`
// The amount of data (in bytes) to read from the readable stream.
Length int64 `json:"length" default:"-1"`
}
type ReadResponse struct {
Data []byte `json:"data"`
// The length of the data (in bytes).
Length int64 `json:"length,omitempty"`
// The amount of time it took to complete reading the data.
Took time.Duration `json:"took"`
}
func (self *Commands) Read(fileOrReader interface{}, args *ReadArgs) (*ReadResponse, error) {
if args == nil {
args = new(ReadArgs)
}
defaults.SetDefaults(args)
var start = time.Now()
// get a readable stream representing the source path we were given
if stream, err := self.env.Open(fileOrReader); err == nil {
var buf = bytes.NewBuffer(nil)
var response = new(ReadResponse)
// if we're supposed to close the source, defer that now
if args.Autoclose {
defer stream.Close()
}
if args.Length >= 0 {
// read the first N bytes
if n, err := io.CopyN(buf, stream, args.Length); err == nil {
response.Length = n
} else {
return nil, err
}
} else {
// read ALL THE BYTES
if n, err := io.Copy(buf, stream); err == nil {
response.Length = n
} else {
return nil, err
}
}
// whatever is in the buffer, that's what you get.
response.Data = buf.Bytes()
response.Took = time.Since(start)
return response, nil
} else {
return nil, err
}
}
type WriteArgs struct {
// The data to write to the destination.
Data io.Reader `json:"data"`
// The data to write as a discrete value.
Value interface{} `json:"value"`
// Whether to attempt to close the destination (if possible) after reading/writing.
Autoclose bool `json:"autoclose" default:"true"`
}
type WriteResponse struct {
// The filesystem path that the data was written to.
Path string `json:"path,omitempty"`
// The size of the data (in bytes).
Size int64 `json:"size,omitempty"`
// The amount of time it took to complete writing the data.
Took time.Duration `json:"took"`
}
// Write a value or a stream of data to a file at the given path. The destination path can be a local
// filesystem path, a URI that uses a custom scheme registered outside of the application, or the string
// "temporary", which will write to a temporary file whose path will be returned in the response.
func (self *Commands) Write(destination interface{}, args *WriteArgs) (*WriteResponse, error) {
if args == nil {
args = new(WriteArgs)
}
defaults.SetDefaults(args)
var start = time.Now()
var response = new(WriteResponse)
var writer io.Writer
if destination != nil {
if filename, ok := destination.(string); ok {
if newPath, w, err := self.env.GetWriterForPath(filename); err == nil {
writer = w
response.Path = newPath
} else {
return nil, err
}
if writer == nil {
if filename == `temporary` {
if temp, err := ioutil.TempFile(``, ``); err == nil {
writer = temp
response.Path = temp.Name()
} else {
return nil, err
}
} else if file, err := os.Create(filename); err == nil {
writer = file
response.Path = filename
} else {
return nil, err
}
}
} else if w, ok := destination.(io.Writer); ok {
writer = w
} else {
return nil, fmt.Errorf("Unsupported destination %T; expected string or io.Writer", destination)
}
}
if writer == nil {
return response, fmt.Errorf("A destination must be specified")
}
if writer != nil {
var err error
if args.Data != nil {
response.Size, err = io.Copy(writer, args.Data)
} else if args.Value != nil {
source := bytes.NewBufferString(fmt.Sprintf("%v", args.Value))
response.Size, err = io.Copy(writer, source)
} else {
err = fmt.Errorf("Must specify source data or a discrete value to write")
}
response.Took = time.Since(start)
// if whatever write (or write attempt) we just did succeeded...
if err == nil {
// if we're supposed to autoclose the destination, give that a shot now
if args.Autoclose {
if closer, ok := writer.(io.Closer); ok {
return response, closer.Close()
}
}
} else {
return response, err
}
} else {
return response, fmt.Errorf("Unable to write to destination")
}
return response, nil
}