MCPcopy Create free account
hub / github.com/crowdsecurity/crowdsec / Only

Method Only

pkg/database/ent/machine_query.go:134–147  ·  view source on GitHub ↗

Only returns a single Machine entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Machine entity is found. Returns a *NotFoundError when no Machine entities are found.

(ctx context.Context)

Source from the content-addressed store, hash-verified

132// Returns a *NotSingularError when more than one Machine entity is found.
133// Returns a *NotFoundError when no Machine entities are found.
134func (_q *MachineQuery) Only(ctx context.Context) (*Machine, error) {
135 nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
136 if err != nil {
137 return nil, err
138 }
139 switch len(nodes) {
140 case 1:
141 return nodes[0], nil
142 case 0:
143 return nil, &NotFoundError{machine.Label}
144 default:
145 return nil, &NotSingularError{machine.Label}
146 }
147}
148
149// OnlyX is like Only, but panics if an error occurs.
150func (_q *MachineQuery) OnlyX(ctx context.Context) *Machine {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected