GitLabgitlab-org

cli

main
ply7 git calls376 ms98 KiB4 cachedprotocol v2
121 lines3.7 KiBRaw
1package tokenduration
3import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "time"
8)
10var durationRegex = regexp.MustCompile(`^(\d+)([dhw])$`)
12// TokenDuration wraps time.Duration to provide custom parsing for token lifetimes.
13// It supports parsing duration strings with h (hours), d (days), and w (weeks) suffixes.
14// This type implements the pflag.Value interface for use with Cobra commands.
15type TokenDuration time.Duration
17// ParseDuration parses a duration string with support for hours (h), days (d), and weeks (w).
18// It accepts formats like: "24h", "30d", "2w", "720h".
19// The duration must be a whole number of days and between 1 and 365 days.
20func ParseDuration(s string) (TokenDuration, error) {
21 var d time.Duration
23 if matches := durationRegex.FindStringSubmatch(s); len(matches) == 3 {
24 num, err := strconv.Atoi(matches[1])
25 if err != nil {
26 return 0, fmt.Errorf("invalid duration value %q: %w", s, err)
27 }
28 var unit time.Duration
29 switch matches[2] {
30 case "d":
31 unit = 24 * time.Hour
32 case "w":
33 unit = 7 * 24 * time.Hour
34 case "h":
35 unit = time.Hour
36 }
37 if num > int(365*24*time.Hour/unit) {
38 return 0, fmt.Errorf("duration must be between 1 and 365 days")
39 }
40 d = time.Duration(num) * unit
41 } else {
42 return 0, fmt.Errorf("invalid duration format: %s (expected formats: 24h, 30d, 2w)", s)
43 }
45 // Validate the parsed duration
46 td := TokenDuration(d)
47 if err := td.validate(); err != nil {
48 return 0, err
49 }
51 return td, nil
54// validate checks if the token duration is valid for token lifetimes.
55// Returns an error if the duration is not a whole number of days or is outside the 1-365 day range.
56func (d TokenDuration) validate() error {
57 duration := time.Duration(d)
59 // Ensure duration is a whole number of days
60 if duration%(24*time.Hour) != 0 {
61 return fmt.Errorf("duration must be in whole days (examples: 1d, 24h, 7d, 168h)")
62 }
64 // Validate duration is between 1 and 365 days
65 if duration < 24*time.Hour || duration > 365*24*time.Hour {
66 return fmt.Errorf("duration must be between 1 and 365 days")
67 }
69 return nil
72// String returns the duration formatted as a string.
73// The String() function is required for Cobra to display the default value.
74func (d TokenDuration) String() string {
75 duration := time.Duration(d)
76 hours := duration.Hours()
78 // Display in the most readable format
79 if hours >= 24 && int(hours)%(24*7) == 0 {
80 // Display as weeks if it's a whole number of weeks
81 weeks := int(hours) / (24 * 7)
82 return fmt.Sprintf("%dw", weeks)
83 } else if hours >= 24 && int(hours)%24 == 0 {
84 // Display as days if it's a whole number of days
85 days := int(hours) / 24
86 return fmt.Sprintf("%dd", days)
87 }
88 // Otherwise display as hours
89 return duration.String()
92// Set parses the given value and sets this TokenDuration.
93// The Set() function is required for Cobra to parse command line arguments.
94func (d *TokenDuration) Set(value string) error {
95 parsed, err := ParseDuration(value)
96 if err != nil {
97 return err
98 }
99 *d = parsed
100 return nil
103// Type returns the type name for help text.
104// The Type() function is required for Cobra to display type information in help.
105func (d *TokenDuration) Type() string {
106 return "duration"
109// Duration returns the underlying time.Duration value.
110func (d TokenDuration) Duration() time.Duration {
111 return time.Duration(d)
114// CalculateExpirationDate calculates an expiration date by adding the duration to today's date.
115// It uses date-only arithmetic (AddDate) since token expiration dates are dates without times.
116// Returns a time.Time at midnight UTC representing the expiration date.
117func (d TokenDuration) CalculateExpirationDate() time.Time {
118 now := time.Now()
119 days := int(d.Duration().Hours() / 24)
120 return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, days)
Diagnostics7 git calls · 376 ms
CallStatusSizeTime0376 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 KiB164 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=152
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=164
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
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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:02001.2 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=1238
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17
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=1238
git.pkt.payload.max=1211
git.sideband.band1.bytes=1211
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1211
git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27ca
Open pack map
fetchtree:0200145 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=b006b6b79422a89e770cae04101ddbce13384b2d
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=145
git.pkt.payload.max=118
git.sideband.band1.bytes=118
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=118
git.pack.trailer=384d4bed6dc05c4fcd238270ac20de656bfa0f7e
Open pack map
fetch2001.5 KiB199 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=199
git.response.bytes=1493
git.response.cache=MISS
git.response.duration_ms=199
git.fetch.wants=90fcc6007dae4dab10ad59a3fcc440bd84807386
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=1493
git.pkt.payload.max=1466
git.sideband.band1.bytes=1466
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
a5e212ca78e0c6f8
Request
ff25e6c5…
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