GitLabgitlab-org

cli

main
ply8 git calls229 ms99 KiB6 cachedprotocol v2
159 lines4.7 KiBRaw
1package update
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "fmt"
8 "os"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/spf13/cobra"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/cmdutils"
16 "gitlab.com/gitlab-org/cli/internal/commands/securefile/helpers"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
22type options struct {
23 forceUpdate bool
24 fileName string
25 inputFilePath string
27 io *iostreams.IOStreams
28 gitlabClient func() (*gitlab.Client, error)
29 baseRepo func() (glrepo.Interface, error)
32func NewCmdUpdate(f cmdutils.Factory) *cobra.Command {
33 opts := &options{
34 io: f.IO(),
35 gitlabClient: f.GitLabClient,
36 baseRepo: f.BaseRepo,
37 }
38 securefileUpdateCmd := &cobra.Command{
39 Use: "update <name> <path>",
40 Short: `Update a secure file in a project.`,
41 Long: heredoc.Docf(`
42 Update a secure file in a project, identified by its name.
43 The command asks for confirmation before updating; use %[1]s-y%[1]s to skip
44 the prompt in scripts.
46 By default, the file is updated in the current project. Use %[1]s--repo%[1]s
47 to target another project.
49 If the file content is unchanged, no update is performed.
51 Updating a secure file changes its ID. When you download the file afterward, reference it by %[1]s--name%[1]s instead of %[1]s--id%[1]s.
52 `, "`"),
53 Aliases: []string{"overwrite"},
54 Args: cobra.ExactArgs(2),
55 Example: heredoc.Doc(`
56 # Update a secure file
57 glab securefile update "file.txt" securefiles/localfile.txt
59 # Skip the confirmation prompt
60 glab securefile update "file.txt" securefiles/localfile.txt -y
62 # Use the 'overwrite' alias
63 glab securefile overwrite "file.txt" securefiles/localfile.txt
64 `),
65 Annotations: map[string]string{
66 mcpannotations.Destructive: "true",
67 },
68 RunE: func(cmd *cobra.Command, args []string) error {
69 opts.complete(args)
71 if err := opts.validate(); err != nil {
72 return err
73 }
75 return opts.run(cmd.Context())
76 },
77 }
79 securefileUpdateCmd.Flags().BoolVarP(&opts.forceUpdate, "yes", "y", false, "Skip the confirmation prompt.")
81 return securefileUpdateCmd
84func (o *options) complete(args []string) {
85 o.fileName = args[0]
86 o.inputFilePath = args[1]
89func (o *options) validate() error {
90 if !o.forceUpdate && !o.io.PromptEnabled() {
91 return &cmdutils.FlagError{Err: fmt.Errorf("--yes or -y flag is required when not running interactively")}
92 }
94 return nil
97func (o *options) run(ctx context.Context) error {
98 client, err := o.gitlabClient()
99 if err != nil {
100 return err
101 }
103 repo, err := o.baseRepo()
104 if err != nil {
105 return err
106 }
108 content, err := os.ReadFile(o.inputFilePath)
109 if err != nil {
110 return fmt.Errorf("unable to read file at %s: %w", o.inputFilePath, err)
111 }
113 secureFile, err := helpers.GetSecureFileByName(client, o.fileName, repo.FullName())
114 if err != nil {
115 return err
116 }
118 color := o.io.Color()
120 if secureFile.ChecksumAlgorithm == "sha256" {
121 if checksum := fmt.Sprintf("%x", sha256.Sum256(content)); checksum == secureFile.Checksum {
122 o.io.LogInfof(color.Bold("%s Secure file %s is already up to date.\n"), color.GreenCheck(), o.fileName)
123 return nil
124 }
125 }
127 if !o.forceUpdate && o.io.PromptEnabled() {
128 o.io.LogInfof("This action will update the existing secure file %s immediately, and its ID will change. Any scripts using `securefile download --id` will stop working; use `--name` instead.\n\n", o.fileName)
129 err = o.io.Confirm(ctx, &o.forceUpdate, fmt.Sprintf("Are you ABSOLUTELY SURE you wish to update this secure file %s?", o.fileName))
130 if err != nil {
131 return cmdutils.WrapError(err, "could not prompt")
132 }
133 }
135 if !o.forceUpdate {
136 return cmdutils.CancelError()
137 }
139 o.io.LogInfof("%s Updating secure file %s=%s %s=%s\n",
140 color.ProgressIcon(),
141 color.Blue("repo"), repo.FullName(),
142 color.Blue("fileName"), o.fileName)
144 _, err = client.SecureFiles.RemoveSecureFile(repo.FullName(), secureFile.ID)
145 if err != nil {
146 return fmt.Errorf("error removing secure file: %w", err)
147 }
149 newFile, _, err := client.SecureFiles.CreateSecureFile(repo.FullName(), bytes.NewReader(content), &gitlab.CreateSecureFileOptions{Name: new(o.fileName)})
150 if err != nil {
151 return fmt.Errorf("the existing secure file %q was removed but the new version could not be uploaded, so it must be re-created manually: %w", o.fileName, err)
152 }
154 o.io.LogInfof(color.Bold("%s Secure file %s updated.\n"), color.GreenCheck(), o.fileName)
155 o.io.LogInfof("%s The secure file ID changed to %d. Any scripts using `securefile download --id` must be updated, or switch to `securefile download --name %s`.\n",
156 color.WarnIcon(), newFile.ID, o.fileName)
158 return nil
Diagnostics8 git calls · 229 ms
CallStatusSizeTime0229 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 KiB · hit3 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=HIT
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
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=2
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.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=4273
git.response.cache=HIT
git.response.duration_ms=5
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.8 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=1857
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
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=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200353 B · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=353
git.response.cache=HIT
git.response.duration_ms=6
git.fetch.filter=tree:0
git.fetch.wants=d9f8b91b7f75f6920fea2e149ed3fba5f61766da
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=353
git.pkt.payload.max=326
git.sideband.band1.bytes=326
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=326
git.pack.trailer=353875a8d3dc1310585685b9eadd95754bcf8d98
Open pack map
fetchtree:0200168 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=168
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=949005f4c1ca04bd3bf906de0361634d5c007b13
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=168
git.pkt.payload.max=141
git.sideband.band1.bytes=141
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=141
git.pack.trailer=04b3a92a8b1b8e1a408688519159674239efbfa2
Open pack map
fetch2001.9 KiB207 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=207
git.response.bytes=1896
git.response.cache=MISS
git.response.duration_ms=207
git.fetch.wants=670f2e3c8be2a7523f52dfd13a728bfbf2ffce49
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=1896
git.pkt.payload.max=1869
git.sideband.band1.bytes=1869
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
a5e252e939eb1ae0
Request
4a9dec39…
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