Add artifact download command (#910)

* feat: add artifact download command

* fix(command/ci/artifact): add error return in functions

* fix: add error return in copy function
This commit is contained in:
João Pedro Pedroza 2022-01-18 22:25:54 -03:00 committed by GitHub
parent 882611d958
commit 8d15282524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package api
import (
"bytes"
"errors"
"io"
"sort"
@ -371,3 +372,18 @@ var CreatePipeline = func(client *gitlab.Client, projectID interface{}, opts *gi
pipe, _, err := client.Pipelines.CreatePipeline(projectID, opts)
return pipe, err
}
var DownloadArtifactJob = func(client *gitlab.Client, repo string, ref string, opts *gitlab.DownloadArtifactsFileOptions) (*bytes.Reader, error) {
if client == nil {
client = apiClient.Lab()
}
if opts == nil {
opts = &gitlab.DownloadArtifactsFileOptions{}
}
jobs, _, err := client.Jobs.DownloadArtifactsFile(repo, ref, opts, nil)
if err != nil {
return nil, err
}
return jobs, nil
}

View File

@ -0,0 +1,81 @@
package ci
import (
"archive/zip"
"io"
"os"
"github.com/MakeNowJust/heredoc"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
func NewCmdRun(f *cmdutils.Factory) *cobra.Command {
var jobArtifactCmd = &cobra.Command{
Use: "artifact <refName> <jobName> [flags]",
Short: `Download all Artifacts from the last pipeline`,
Aliases: []string{"push"},
Example: heredoc.Doc(`
$ glab ci artifact main build
$ glab ci artifact main deploy --path="artifacts/"
`),
Long: ``,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
repo, err := f.BaseRepo()
if err != nil {
return err
}
apiClient, err := f.HttpClient()
if err != nil {
return err
}
p, err := cmd.Flags().GetString("path")
if err != nil {
return err
}
artifact, err := api.DownloadArtifactJob(apiClient, repo.FullName(), args[0], &gitlab.DownloadArtifactsFileOptions{Job: &args[1]})
if err != nil {
return err
}
zr, err := zip.NewReader(artifact, artifact.Size())
if err != nil {
return err
}
if err := os.Mkdir(p, 0755); err != nil {
return err
}
for _, v := range zr.File {
if v.FileInfo().IsDir() {
if err := os.Mkdir(p+v.Name, v.Mode()); err != nil {
return err
}
} else {
srcFile, err := zr.Open(v.Name)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.OpenFile(p+v.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, v.Mode())
if err != nil {
return err
}
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
}
}
return nil
},
}
jobArtifactCmd.Flags().StringP("path", "p", "", "Path to download the Artifact files (default ./)")
return jobArtifactCmd
}

View File

@ -1,6 +1,7 @@
package ci
import (
jobArtifactCmd "github.com/profclems/glab/commands/ci/artifact"
pipeDeleteCmd "github.com/profclems/glab/commands/ci/delete"
legacyCICmd "github.com/profclems/glab/commands/ci/legacyci"
ciLintCmd "github.com/profclems/glab/commands/ci/lint"
@ -34,5 +35,6 @@ func NewCmdCI(f *cmdutils.Factory) *cobra.Command {
ciCmd.AddCommand(pipeStatusCmd.NewCmdStatus(f))
ciCmd.AddCommand(pipeRetryCmd.NewCmdRetry(f))
ciCmd.AddCommand(pipeRunCmd.NewCmdRun(f))
ciCmd.AddCommand(jobArtifactCmd.NewCmdRun(f))
return ciCmd
}