Repository navigation
Expand file tree
/
Copy pathcmd_graph.go
More file actions
241 lines (232 loc) · 7.64 KB
/
Copy pathcmd_graph.go
File metadata and controls
241 lines (232 loc) · 7.64 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"github.com/urfave/cli/v2"
)
// The `graph` command group — the low-level API, mirroring dbClient.Graph.
//
// These write raw vertices and edges with no CMDB semantics: no type links, no
// triggers, no schema validation. That is the point — it is the escape hatch
// for inspecting and repairing graph state that the high-level API would refuse
// to express. The `cmdb` group is the typed counterpart.
func graphCommand() *cli.Command {
return &cli.Command{
Name: "graph",
Usage: "low-level graph CRUD (raw vertices and links)",
Subcommands: []*cli.Command{
graphVertexCommand(),
graphLinkCommand(),
},
}
}
// ── graph vertex ──────────────────────────────────────────────────────────────
func graphVertexCommand() *cli.Command {
return &cli.Command{
Name: "vertex",
Usage: "raw vertices",
Subcommands: []*cli.Command{
{
Name: "create",
Usage: "create a vertex",
ArgsUsage: "<id>",
Flags: []cli.Flag{bodyFlag(), jsonFlag()},
Action: func(cCtx *cli.Context) error {
args, err := requireArgs(cCtx, "vertex")
if err != nil {
return err
}
body, err := parseBody(cCtx)
if err != nil {
return err
}
return reportOp(cCtx, "vertex.create", args[0], ops.vertexCreate(args[0], body))
},
},
{
Name: "update",
Usage: "update a vertex body (defaults to the gwalk cursor)",
ArgsUsage: "[id]",
Flags: []cli.Flag{bodyFlag(), replaceFlag(), jsonFlag(),
&cli.BoolFlag{Name: "upsert", Usage: "create the vertex if it does not exist"}},
Action: func(cCtx *cli.Context) error {
id, err := resolveID(cCtx, "vertex")
if err != nil {
return err
}
body, err := parseBody(cCtx)
if err != nil {
return err
}
r := ops.vertexUpdate(id, body, cCtx.Bool("replace"), cCtx.Bool("upsert"))
return reportOp(cCtx, "vertex.update", id, r)
},
},
{
Name: "delete",
Usage: "delete a vertex and all of its links (defaults to the gwalk cursor)",
ArgsUsage: "[id]",
Flags: []cli.Flag{jsonFlag()},
Action: func(cCtx *cli.Context) error {
id, err := resolveID(cCtx, "vertex")
if err != nil {
return err
}
if err := refuseBuiltIn(id); err != nil {
return err
}
return reportOp(cCtx, "vertex.delete", id, ops.vertexDelete(id))
},
},
{
Name: "read",
Usage: "read a vertex with its links (defaults to the gwalk cursor)",
ArgsUsage: "[id]",
Flags: []cli.Flag{jsonFlag()},
Action: func(cCtx *cli.Context) error {
id, err := resolveID(cCtx, "vertex")
if err != nil {
return err
}
data, readErr := ops.vertexRead(id)
return reportRead(cCtx, data, readErr)
},
},
},
}
}
// ── graph link ────────────────────────────────────────────────────────────────
// A link is addressable two ways — by its name (unique among the owner's
// out-links) or by its (to, type) pair (unique per direction). Update, delete
// and read accept either; create needs all four parts.
func graphLinkCommand() *cli.Command {
nameFlag := &cli.StringFlag{Name: "name", Usage: "link name (unique among the source vertex's out-links)"}
toFlag := &cli.StringFlag{Name: "to", Usage: "target vertex id"}
typeFlag := &cli.StringFlag{Name: "type", Usage: "link type"}
// pickSelector resolves which of the two addressing modes the user chose.
pickSelector := func(cCtx *cli.Context) (byName bool, name, to, tp string, err error) {
name, to, tp = cCtx.String("name"), cCtx.String("to"), cCtx.String("type")
switch {
case name != "":
if err = validateID("link name", name); err != nil {
return
}
return true, name, to, tp, nil
case to != "" && tp != "":
if err = validateID("target vertex", to); err != nil {
return
}
return false, name, to, tp, nil
default:
err = usageErr("address the link either with --name, or with both --to and --type")
return
}
}
return &cli.Command{
Name: "link",
Usage: "raw links between vertices",
Subcommands: []*cli.Command{
{
Name: "create",
Usage: "create a link (source defaults to the gwalk cursor)",
ArgsUsage: "[from]",
Flags: []cli.Flag{toFlag, nameFlag, typeFlag, tagsFlag(), bodyFlag(), jsonFlag(),
&cli.BoolFlag{Name: "force", Usage: "overwrite an existing link, bypassing the uniqueness checks"}},
Action: func(cCtx *cli.Context) error {
from, err := resolveID(cCtx, "source vertex")
if err != nil {
return err
}
to, name, tp := cCtx.String("to"), cCtx.String("name"), cCtx.String("type")
if to == "" || tp == "" {
return usageErr("--to and --type are required to create a link")
}
if err := validateID("target vertex", to); err != nil {
return err
}
if name == "" {
// The server's own default: name the link after its target.
name = stripDomain(to)
}
if err := validateID("link name", name); err != nil {
return err
}
body, err := parseBody(cCtx)
if err != nil {
return err
}
r := ops.linkCreate(from, to, name, tp, parseTags(cCtx), body, cCtx.Bool("force"))
return reportOp(cCtx, "link.create", from+" -> "+to, r)
},
},
{
Name: "update",
Usage: "update a link's body or tags (source defaults to the gwalk cursor)",
ArgsUsage: "[from]",
Flags: []cli.Flag{nameFlag, toFlag, typeFlag, tagsFlag(), bodyFlag(), replaceFlag(), jsonFlag()},
Action: func(cCtx *cli.Context) error {
from, err := resolveID(cCtx, "source vertex")
if err != nil {
return err
}
byName, name, to, tp, err := pickSelector(cCtx)
if err != nil {
return err
}
body, err := parseBody(cCtx)
if err != nil {
return err
}
tags := parseTags(cCtx)
replace := cCtx.Bool("replace")
if byName {
r := ops.linkUpdate(from, name, tags, body, replace)
return reportOp(cCtx, "link.update", from+":"+name, r)
}
r := ops.linkUpdateByToType(from, to, tp, tags, body, replace)
return reportOp(cCtx, "link.update", from+" -"+tp+"-> "+to, r)
},
},
{
Name: "delete",
Usage: "delete a link (source defaults to the gwalk cursor)",
ArgsUsage: "[from]",
Flags: []cli.Flag{nameFlag, toFlag, typeFlag, jsonFlag()},
Action: func(cCtx *cli.Context) error {
from, err := resolveID(cCtx, "source vertex")
if err != nil {
return err
}
byName, name, to, tp, err := pickSelector(cCtx)
if err != nil {
return err
}
if byName {
return reportOp(cCtx, "link.delete", from+":"+name, ops.linkDelete(from, name))
}
return reportOp(cCtx, "link.delete", from+" -"+tp+"-> "+to, ops.linkDeleteByToType(from, to, tp))
},
},
{
Name: "read",
Usage: "read a link (source defaults to the gwalk cursor)",
ArgsUsage: "[from]",
Flags: []cli.Flag{nameFlag, toFlag, typeFlag, jsonFlag()},
Action: func(cCtx *cli.Context) error {
from, err := resolveID(cCtx, "source vertex")
if err != nil {
return err
}
byName, name, to, tp, err := pickSelector(cCtx)
if err != nil {
return err
}
if byName {
data, readErr := ops.linkRead(from, name)
return reportRead(cCtx, data, readErr)
}
data, readErr := ops.linkReadByToType(from, to, tp)
return reportRead(cCtx, data, readErr)
},
},
},
}
}