This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation_options.go
91 lines (72 loc) · 1.79 KB
/
operation_options.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
package mapreduce
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/go-faster/yt/guid"
"github.com/go-faster/yt/mapreduce/spec"
"github.com/go-faster/yt/ypath"
"github.com/go-faster/yt/yt"
)
type OperationOption interface {
isOperationOption()
}
type localFilesOption struct {
paths []string
}
func (l *localFilesOption) isOperationOption() {}
func (l *localFilesOption) uploadLocalFiles(ctx context.Context, p *prepare) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("failed to upload local file: %w", err)
}
}()
for _, filename := range l.paths {
st, err := os.Stat(filename)
if err != nil {
return err
}
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
tmpPath := ypath.Path("//tmp").Child(guid.New().String())
_, err = p.mr.yc.CreateNode(ctx, tmpPath, yt.NodeFile, nil)
if err != nil {
return err
}
w, err := p.mr.yc.WriteFile(ctx, tmpPath, &yt.WriteFileOptions{})
if err != nil {
return err
}
defer w.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
p.spec.VisitUserScripts(func(script *spec.UserScript) {
script.FilePaths = append(script.FilePaths, spec.File{
CypressPath: tmpPath,
Executable: st.Mode()&0o100 != 0,
FileName: filepath.Base(filename),
})
})
}
return nil
}
// WithLocalFile makes local file available inside job sandbox directory.
//
// Filename and file permissions are preserved. All files are uploaded into job sandbox.
func WithLocalFiles(paths []string) OperationOption {
return &localFilesOption{paths: paths}
}
type skipSelfUploadOption struct{}
func (l *skipSelfUploadOption) isOperationOption() {}
func SkipSelfUpload() OperationOption {
return &skipSelfUploadOption{}
}