-
Notifications
You must be signed in to change notification settings - Fork 557
/
file.go
100 lines (82 loc) · 2.74 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
package schema
import (
"context"
"path/filepath"
"github.com/dagger/dagger/core"
"github.com/dagger/dagger/dagql"
)
type fileSchema struct {
srv *dagql.Server
}
var _ SchemaResolvers = &fileSchema{}
func (s *fileSchema) Install() {
dagql.Fields[*core.Query]{
dagql.Func("file", s.file).
Deprecated("Use `loadFileFromID` instead."),
}.Install(s.srv)
dagql.Fields[*core.File]{
Syncer[*core.File]().
Doc(`Force evaluation in the engine.`),
dagql.Func("contents", s.contents).
Doc(`Retrieves the contents of the file.`),
dagql.Func("size", s.size).
Doc(`Retrieves the size of the file, in bytes.`),
dagql.Func("name", s.name).
Doc(`Retrieves the name of the file.`),
dagql.Func("export", s.export).
Impure("Writes to the local host.").
Doc(`Writes the file to a file path on the host.`).
ArgDoc("path", `Location of the written directory (e.g., "output.txt").`).
ArgDoc("allowParentDirPath",
`If allowParentDirPath is true, the path argument can be a directory
path, in which case the file will be created in that directory.`),
dagql.Func("withTimestamps", s.withTimestamps).
Doc(`Retrieves this file with its created/modified timestamps set to the given time.`).
ArgDoc("timestamp", `Timestamp to set dir/files in.`,
`Formatted in seconds following Unix epoch (e.g., 1672531199).`),
}.Install(s.srv)
}
type fileArgs struct {
ID core.FileID
}
func (s *fileSchema) file(ctx context.Context, parent *core.Query, args fileArgs) (*core.File, error) {
val, err := args.ID.Load(ctx, s.srv)
if err != nil {
return nil, err
}
return val.Self, nil
}
func (s *fileSchema) contents(ctx context.Context, file *core.File, args struct{}) (dagql.String, error) {
content, err := file.Contents(ctx)
if err != nil {
return "", err
}
return dagql.NewString(string(content)), nil
}
func (s *fileSchema) size(ctx context.Context, file *core.File, args struct{}) (dagql.Int, error) {
info, err := file.Stat(ctx)
if err != nil {
return 0, err
}
return dagql.NewInt(int(info.Size_)), nil
}
func (s *fileSchema) name(ctx context.Context, file *core.File, args struct{}) (dagql.String, error) {
return dagql.NewString(filepath.Base(file.File)), nil
}
type fileExportArgs struct {
Path string
AllowParentDirPath bool `default:"false"`
}
func (s *fileSchema) export(ctx context.Context, parent *core.File, args fileExportArgs) (dagql.Boolean, error) {
err := parent.Export(ctx, args.Path, args.AllowParentDirPath)
if err != nil {
return false, err
}
return true, nil
}
type fileWithTimestampsArgs struct {
Timestamp int
}
func (s *fileSchema) withTimestamps(ctx context.Context, parent *core.File, args fileWithTimestampsArgs) (*core.File, error) {
return parent.WithTimestamps(ctx, args.Timestamp)
}