This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
/
filesystem.schema.go
186 lines (156 loc) · 3.92 KB
/
filesystem.schema.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
package core
import (
"fmt"
"github.com/dagger/cloak/core/filesystem"
"github.com/dagger/cloak/router"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
bkclient "github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
)
var fsIDResolver = router.ScalarResolver{
Serialize: func(value any) any {
switch v := value.(type) {
case filesystem.FSID, string:
return v
default:
panic(fmt.Sprintf("unexpected fsid type %T", v))
}
},
ParseValue: func(value any) any {
switch v := value.(type) {
case string:
return filesystem.FSID(v)
default:
panic(fmt.Sprintf("unexpected fsid value type %T: %+v", v, v))
}
},
ParseLiteral: func(valueAST ast.Value) any {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return filesystem.FSID(valueAST.Value)
default:
panic(fmt.Sprintf("unexpected fsid literal type: %T", valueAST))
}
},
}
var _ router.ExecutableSchema = &filesystemSchema{}
type filesystemSchema struct {
*baseSchema
}
func (s *filesystemSchema) Name() string {
return "filesystem"
}
func (s *filesystemSchema) Schema() string {
return `
scalar FSID
"""
A reference to a filesystem tree.
For example:
- The root filesystem of a container
- A source code repository
- A directory containing binary artifacts
Rule of thumb: if it fits in a tar archive, it fits in a Filesystem.
"""
type Filesystem {
id: FSID!
"read a file at path"
file(path: String!, lines: Int): String
"copy from a filesystem"
copy(
from: FSID!,
srcPath: String,
destPath: String,
include: [String!]
exclude: [String!]
): Filesystem
"push a filesystem as an image to a registry"
pushImage(ref: String!): Boolean!
}
extend type Core {
"Look up a filesystem by its ID"
filesystem(id: FSID!): Filesystem!
}
`
}
func (s *filesystemSchema) Resolvers() router.Resolvers {
return router.Resolvers{
"FSID": fsIDResolver,
"Core": router.ObjectResolver{
"filesystem": s.filesystem,
},
"Filesystem": router.ObjectResolver{
"file": s.file,
"copy": s.copy,
"pushImage": s.pushImage,
},
}
}
func (s *filesystemSchema) Dependencies() []router.ExecutableSchema {
return nil
}
func (s *filesystemSchema) filesystem(p graphql.ResolveParams) (any, error) {
return filesystem.New(p.Args["id"].(filesystem.FSID)), nil
}
func (s *filesystemSchema) file(p graphql.ResolveParams) (any, error) {
obj, err := filesystem.FromSource(p.Source)
if err != nil {
return nil, err
}
path := p.Args["path"].(string)
output, err := obj.ReadFile(p.Context, s.gw, path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return truncate(string(output), p.Args), nil
}
func (s *filesystemSchema) copy(p graphql.ResolveParams) (any, error) {
obj, err := filesystem.FromSource(p.Source)
if err != nil {
return nil, err
}
st, err := obj.ToState()
if err != nil {
return nil, err
}
contents, err := filesystem.New(p.Args["from"].(filesystem.FSID)).ToState()
if err != nil {
return nil, err
}
src := p.Args["srcPath"].(string)
dest := p.Args["destPath"].(string)
include, _ := p.Args["include"].([]string)
exclude, _ := p.Args["exclude"].([]string)
st = st.File(llb.Copy(contents, src, dest, &llb.CopyInfo{
CopyDirContentsOnly: true,
CreateDestPath: true,
AllowWildcard: true,
IncludePatterns: include,
ExcludePatterns: exclude,
}))
fs, err := s.Solve(p.Context, st)
if err != nil {
return nil, err
}
return fs, err
}
func (s *filesystemSchema) pushImage(p graphql.ResolveParams) (any, error) {
obj, err := filesystem.FromSource(p.Source)
if err != nil {
return nil, err
}
ref, _ := p.Args["ref"].(string)
if ref == "" {
return nil, fmt.Errorf("ref is required for pushImage")
}
if err := s.Export(p.Context, obj, bkclient.ExportEntry{
Type: bkclient.ExporterImage,
Attrs: map[string]string{
"name": ref,
"push": "true",
},
}); err != nil {
return nil, err
}
return true, nil
}