-
Notifications
You must be signed in to change notification settings - Fork 4
/
introspect_module.go
94 lines (84 loc) · 1.91 KB
/
introspect_module.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
)
type introspectionArgs struct {
Name string
}
type introspectionFunction struct {
Name string
Args []introspectionArgs
}
type introspectionObject struct {
Name string
Constructor introspectionFunction
Functions []introspectionFunction
}
// IntrospectModule returns an structured representation of objects composing a module.
func (d *DaggerverseCockpit) introspectModule(
ctx context.Context,
module *Directory,
) ([]introspectionObject, error) {
introspectionResult, err := d.
CLI("10.0.2").
Container.
WithWorkdir("/app").
WithMountedDirectory("/app", module).
WithNewFile("/app/introspection.graphql", ContainerWithNewFileOpts{
Contents: introspectionQuery,
}).
WithExec([]string{"query", "--doc", "introspection.graphql"}, ContainerWithExecOpts{ExperimentalPrivilegedNesting: true}).
Stdout(ctx)
if err != nil {
return nil, fmt.Errorf("failed to execute the introspection query: %w", err)
}
result := gjson.Get(introspectionResult, "host.directory.asModule.initialize.objects").Array()
objects := make([]introspectionObject, len(result))
for i, object := range result {
if err := json.Unmarshal([]byte(object.Get("asObject").String()), &objects[i]); err != nil {
return nil, fmt.Errorf("could not unmarshal the module object: %w", err)
}
}
return objects, nil
}
// introspectionQuery is a Dagger GraphQL query used to
// introspect a module.
var introspectionQuery = `
query {
host {
directory(path: ".") {
asModule {
initialize {
description
objects {
asObject {
name
description
constructor {
description
args {
name
description
}
}
functions {
name
description
args {
name
description
}
}
fields {
name
}
}
}
}
}
}
}
}`