GitLabgitlab-org
cli
main
1package upload
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
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/glrepo"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
19)
21type options struct {
22 packageName string
23 packageVersion string
24 fileName string
25 inputFilePath string
27 io *iostreams.IOStreams
28 gitlabClient func() (*gitlab.Client, error)
29 baseRepo func() (glrepo.Interface, error)
30}
32func NewCmd(f cmdutils.Factory) *cobra.Command {
33 opts := &options{
34 io: f.IO(),
35 gitlabClient: f.GitLabClient,
36 baseRepo: f.BaseRepo,
37 }
38 cmd := &cobra.Command{
39 Use: "upload <file> --name <package> --version <version> [flags]",
40 Short: `Upload a file to a project's package registry.`,
41 Long: heredoc.Docf(`
42 Uploaded files are stored as generic packages: arbitrary files identified
43 by a package name, version, and file name.
45 The file is stored under the given package name and version. By default
46 it keeps its original file name; use %[1]s--filename%[1]s to store it under a
47 different name.
49 By default, the file is uploaded to the current project. Use %[1]s--repo%[1]s
50 to target another project.
51 `, "`"),
52 Example: heredoc.Doc(`
53 # Upload a file as version 1.0.0 of package 'my-package'
54 glab packages upload ./build/app.zip --name my-package --version 1.0.0
56 # Store the file under a different name
57 glab packages upload ./build/app.zip --name my-package --version 1.0.0 --filename release.zip
59 # Use the 'ul' alias and upload to another project
60 glab packages ul ./build/app.zip -n my-package --version 1.0.0 -R owner/repo
61 `),
62 Aliases: []string{"ul"},
63 Args: cobra.ExactArgs(1),
64 Annotations: map[string]string{
65 mcpannotations.Destructive: "true",
66 },
67 RunE: func(cmd *cobra.Command, args []string) error {
68 if err := opts.complete(cmd, args); err != nil {
69 return err
70 }
72 return opts.run(cmd.Context())
73 },
74 }
76 fl := cmd.Flags()
77 fl.StringP("name", "n", "", "Name of the package.")
78 fl.StringP("version", "v", "", "Version of the package.")
79 fl.String("filename", "", "Name to store the file under. Defaults to the local file name.")
80 cobra.CheckErr(cmd.MarkFlagRequired("name"))
81 cobra.CheckErr(cmd.MarkFlagRequired("version"))
83 return cmd
84}
86func (o *options) complete(cmd *cobra.Command, args []string) error {
87 fl := cmd.Flags()
89 name, err := fl.GetString("name")
90 if err != nil {
91 return err
92 }
93 o.packageName = name
95 version, err := fl.GetString("version")
96 if err != nil {
97 return err
98 }
99 o.packageVersion = version
101 fileName, err := fl.GetString("filename")
102 if err != nil {
103 return err
104 }
105 o.fileName = fileName
107 o.inputFilePath = args[0]
108 if o.fileName == "" {
109 o.fileName = filepath.Base(o.inputFilePath)
110 }
112 return nil
113}
115func (o *options) run(ctx context.Context) (err error) {
116 client, err := o.gitlabClient()
117 if err != nil {
118 return err
119 }
121 repo, err := o.baseRepo()
122 if err != nil {
123 return err
124 }
126 reader, err := os.Open(o.inputFilePath)
127 if err != nil {
128 return fmt.Errorf("unable to read file at %s: %w", o.inputFilePath, err)
129 }
130 defer func() { err = errors.Join(err, reader.Close()) }()
132 color := o.io.Color()
133 o.io.LogInfof("%s Uploading package file %s=%s %s=%s %s=%s %s=%s\n",
134 color.ProgressIcon(),
135 color.Blue("repo"), repo.FullName(),
136 color.Blue("package"), o.packageName,
137 color.Blue("version"), o.packageVersion,
138 color.Blue("file"), o.fileName)
140 file, _, err := client.GenericPackages.PublishPackageFile(repo.FullName(), o.packageName, o.packageVersion, o.fileName, reader, nil, gitlab.WithContext(ctx))
141 if err != nil {
142 return fmt.Errorf("failed to upload package file: %w", err)
143 }
145 assetPath, err := client.GenericPackages.FormatPackageURL(repo.FullName(), o.packageName, o.packageVersion, o.fileName)
146 if err != nil {
147 return fmt.Errorf("failed to format package URL: %w", err)
148 }
149 assetURL := client.BaseURL().JoinPath(assetPath)
151 o.io.LogInfof(color.Bold("%s Package file %s uploaded.\n"), color.GreenCheck(), o.fileName)
152 o.io.LogInfof(" %s %s\n", color.Blue("url"), o.io.Hyperlink(assetURL.String(), assetURL.String()))
153 o.io.LogInfof(" %s %s\n", color.Blue("sha256"), file.FileSHA256)
154 return nil
155}
Diagnostics8 git calls · 239 ms
CallStatusSizeTime0239 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 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=2 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=3 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 · hit3 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=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=44Open pack map
fetchtree:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=2 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200263 B · 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=263 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=4445ce4c6a6710742ed094287b245091ae79204f 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=263 git.pkt.payload.max=236 git.sideband.band1.bytes=236 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=236 git.pack.trailer=4126ef8ff4c560b52ccdd91c9d0a185a77d21c2dOpen pack map
fetchtree:0200170 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=170 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=bfc069d2dfe2ce3db69d52f879104796dcf0d973 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=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=8c86856ad221ec721b73fe1683288421113cbaedOpen pack map
fetch2001.6 KiB225 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=225 git.response.bytes=1677 git.response.cache=MISS git.response.duration_ms=225 git.fetch.wants=96cce98dd3a085674bad27de809064cca6cf0080 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=1677 git.pkt.payload.max=1650 git.sideband.band1.bytes=1650 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
- a5e2c2acd9f0aedd
- Request
- 66e7b186…
- 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