/
local.go
49 lines (37 loc) · 1.03 KB
/
local.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
package gcs
import (
"io"
"os"
"github.com/pkg/errors"
)
type LocalFileHelper struct{}
// NewReader creates a new Reader for local file.
func (h *LocalFileHelper) NewReader(uri string) (io.Reader, error) {
reader, err := os.Open(uri)
if err != nil {
return nil, errors.WithStack(errors.Wrapf(err, "Clould not read: %v", uri))
}
return reader, nil
}
// NewWriter creates a new Writer for the local file.
//
// TODO(jlewi): Can we add options to control filemode?
func (h *LocalFileHelper) NewWriter(uri string) (io.Writer, error) {
_, err := os.Stat(uri)
if err == nil || !os.IsNotExist(err) {
return nil, errors.WithStack(errors.Errorf("Can't write %v; It already exists", uri))
}
writer, err := os.Create(uri)
if err != nil {
return nil, errors.WithStack(errors.Wrapf(err, "Clould not write: %v", uri))
}
return writer, nil
}
// Exists checks whether the file exists.
func (h *LocalFileHelper) Exists(uri string) (bool, error) {
_, err := os.Stat(uri)
if os.IsNotExist(err) {
return false, nil
}
return true, nil
}