GitLabgitlab-org
cli
main
1package iostreams
3import (
4 "encoding/json"
5 "fmt"
6 "io"
8 "github.com/itchyny/gojq"
9)
11// JQFilter is the value backing the --jq flag. It implements pflag.Value
12// so cobra parses and validates the jq expression as part of flag handling
13// (Set runs once, at parse time). IOStreams holds a pointer to one of these
14// and PrintJSON delegates filtering to Apply when the filter is active.
15type JQFilter struct {
16 expr string
17 query *gojq.Query
18}
20// Set parses and stores the jq expression. It satisfies pflag.Value.
21func (j *JQFilter) Set(s string) error {
22 if s == "" {
23 j.expr = ""
24 j.query = nil
25 return nil
26 }
27 q, err := gojq.Parse(s)
28 if err != nil {
29 return fmt.Errorf("invalid --jq expression: %w", err)
30 }
31 j.expr = s
32 j.query = q
33 return nil
34}
36// String returns the stored expression. It satisfies pflag.Value.
37func (j *JQFilter) String() string { return j.expr }
39// Type is the cobra-displayed value type. It satisfies pflag.Value.
40func (j *JQFilter) Type() string { return "string" }
42// IsActive reports whether an expression has been supplied.
43func (j *JQFilter) IsActive() bool { return j.query != nil }
45// Apply runs the compiled jq query over v and writes results to w. Strings
46// produced by the filter are written raw (matching jq -r); other values are
47// emitted as compact JSON, one result per line.
48func (j *JQFilter) Apply(w io.Writer, v any) error {
49 if j.query == nil {
50 return fmt.Errorf("Apply called on inactive JQFilter")
51 }
53 // Round-trip through Marshal/Unmarshal so gojq operates on plain
54 // map/slice types instead of arbitrary structs.
55 data, err := json.Marshal(v) //nolint:forbidigo // internal to JQFilter; preparing input for gojq
56 if err != nil {
57 return err
58 }
59 var input any
60 if err := json.Unmarshal(data, &input); err != nil {
61 return err
62 }
64 iter := j.query.Run(input)
65 for {
66 result, ok := iter.Next()
67 if !ok {
68 break
69 }
70 if errResult, isErr := result.(error); isErr {
71 return fmt.Errorf("--jq error: %w", errResult)
72 }
73 // jq -r style: bare strings without surrounding quotes.
74 if str, isStr := result.(string); isStr {
75 if _, err := fmt.Fprintln(w, str); err != nil {
76 return err
77 }
78 continue
79 }
80 out, err := json.Marshal(result) //nolint:forbidigo // internal to JQFilter; encoding gojq result
81 if err != nil {
82 return err
83 }
84 if _, err := fmt.Fprintln(w, string(out)); err != nil {
85 return err
86 }
87 }
88 return nil
89}
Diagnostics6 git calls · 212 ms
CallStatusSizeTime0212 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:0200432 B · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=206 git.response.bytes=432 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=a79e8e89fbedf0d57724da455f6326f6e79de3b5 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=432 git.pkt.payload.max=405 git.sideband.band1.bytes=405 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=405 git.pack.trailer=6b5f558fa7a37603ce66aafb676f53aedcb02370Open pack map
fetch2001.1 KiB206 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1090 git.response.cache=MISS git.response.duration_ms=206 git.fetch.wants=c8c08d009861b82d973fe3ccb74b2dfdfee0dbc1 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1090 git.pkt.payload.max=1063 git.sideband.band1.bytes=1063 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2c2fc7a00aedd
- Request
- cda02e06…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format