GitLabgitlab-org
cli
main
1package cmdutils
3import (
4 "fmt"
5 "io"
6 "os"
7 "strings"
9 "github.com/spf13/cobra"
10)
12// SecretFile is a credential that a command reads from a file, standard
13// input or an environment variable. It has no flag that takes the value
14// itself, because flag values end up in shell history and the process list.
15type SecretFile struct {
16 flag string
17 label string
18 env string
19 file string
20}
22// AddSecretFileFlag registers flag on cmd. The flag names the file holding
23// the secret that label describes, and origin completes the flag's help with
24// where the secret comes from. Without the flag, the secret comes from the
25// environment variable env, which is added to cmd's help:environment section.
26func AddSecretFileFlag(cmd *cobra.Command, flag, label, env, origin string) *SecretFile {
27 s := &SecretFile{flag: flag, label: label, env: env}
28 cmd.Flags().StringVar(&s.file, flag, "", fmt.Sprintf("Path to a file with the %s %s. Use - to read from standard input. Defaults to the %s environment variable.", label, origin, env))
29 if cmd.Annotations == nil {
30 cmd.Annotations = map[string]string{}
31 }
32 cmd.Annotations["help:environment"] += fmt.Sprintf("%s: the %s %s, used when --%s is not set.\n", env, label, origin, flag)
33 return s
34}
36// IsSet reports whether the secret has a source: a file or a non-empty
37// environment variable.
38func (s *SecretFile) IsSet() bool {
39 return s.file != "" || os.Getenv(s.env) != ""
40}
42// File returns the path the secret is read from, - for standard input, or ""
43// if it comes from the environment variable.
44func (s *SecretFile) File() string {
45 return s.file
46}
48// Env returns the environment variable the secret falls back to.
49func (s *SecretFile) Env() string {
50 return s.env
51}
53// FromStdin reports whether the secret is read from standard input.
54func (s *SecretFile) FromStdin() bool {
55 return s.file == "-"
56}
58// Read returns the secret without surrounding whitespace.
59func (s *SecretFile) Read(stdin io.Reader) (string, error) {
60 var value string
61 switch s.file {
62 case "":
63 value = os.Getenv(s.env)
64 if value == "" {
65 return "", &FlagError{Err: fmt.Errorf("pass --%s or set %s", s.flag, s.env)}
66 }
67 case "-":
68 b, err := io.ReadAll(stdin)
69 if err != nil {
70 return "", fmt.Errorf("failed to read the %s from standard input: %w", s.label, err)
71 }
72 value = string(b)
73 default:
74 b, err := os.ReadFile(s.file)
75 if err != nil {
76 return "", fmt.Errorf("failed to read --%s: %w", s.flag, err)
77 }
78 value = string(b)
79 }
80 value = strings.TrimSpace(value)
81 if value == "" {
82 return "", fmt.Errorf("the %s is empty", s.label)
83 }
84 return value, nil
85}
Diagnostics6 git calls · 231 ms
CallStatusSizeTime0231 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 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 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 · hit2 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=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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=3 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=222 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=3 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=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetch2001.1 KiB222 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1105 git.response.cache=MISS git.response.duration_ms=222 git.fetch.wants=64dbed26dfe0a16c1f38d214eebc1a24b68df3d7 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=1105 git.pkt.payload.max=1078 git.sideband.band1.bytes=1078 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
- a5e2188bf8f1ecd4
- Request
- ece17e4b…
- 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