job_artifact_download.go 2.6 KB
Newer Older
1 2 3 4 5 6
package cmd

import (
	"fmt"
	"net/http"
	"path/filepath"
7
	"strconv"
8

LinuxSuRen's avatar
LinuxSuRen 已提交
9 10
	"github.com/jenkins-zh/jenkins-cli/app/helper"

11 12 13 14 15 16 17
	"github.com/jenkins-zh/jenkins-cli/client"
	"github.com/jenkins-zh/jenkins-cli/util"
	"github.com/spf13/cobra"
)

// JobArtifactDownloadOption is the options of job artifact download command
type JobArtifactDownloadOption struct {
18
	ID           string
19
	ShowProgress bool
20
	DownloadDir  string
21

22
	Jenkins      *JenkinsServer
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	RoundTripper http.RoundTripper
}

var jobArtifactDownloadOption JobArtifactDownloadOption

func init() {
	jobArtifactCmd.AddCommand(jobArtifactDownloadCmd)
	jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.ID, "id", "i", "", "ID of the job artifact")
	jobArtifactDownloadCmd.Flags().BoolVarP(&jobArtifactDownloadOption.ShowProgress, "progress", "", true, "Whether show the progress")
	jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.DownloadDir, "download-dir", "", "", "The directory which artifact will be downloaded")
}

var jobArtifactDownloadCmd = &cobra.Command{
	Use:   "download <jobName> [buildID]",
	Short: "Download the artifact of target job",
	Long:  `Download the artifact of target job`,
39
	Args:  cobra.MinimumNArgs(1),
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	Run: func(cmd *cobra.Command, args []string) {
		argLen := len(args)
		var err error
		jobName := args[0]
		buildID := -1

		if argLen >= 2 {
			if buildID, err = strconv.Atoi(args[1]); err != nil {
				cmd.PrintErrln(err)
				return
			}
		}

		jclient := &client.ArtifactClient{
			JenkinsCore: client.JenkinsCore{
				RoundTripper: jobArtifactDownloadOption.RoundTripper,
			},
		}
		jobArtifactDownloadOption.Jenkins = getCurrentJenkinsAndClient(&(jclient.JenkinsCore))

LinuxSuRen's avatar
LinuxSuRen 已提交
60 61
		artifacts, err := jclient.List(jobName, buildID)
		if err == nil {
62 63 64 65 66
			for _, artifact := range artifacts {
				if jobArtifactDownloadOption.ID != "" && jobArtifactDownloadOption.ID != artifact.ID {
					continue
				}

LinuxSuRen's avatar
LinuxSuRen 已提交
67 68
				downloadPath := filepath.Join(jobArtifactDownloadOption.DownloadDir, artifact.Name)
				err = jobArtifactDownloadOption.download(artifact.URL, downloadPath)
69
				if err != nil {
LinuxSuRen's avatar
LinuxSuRen 已提交
70
					break
71 72 73
				}
			}
		}
LinuxSuRen's avatar
LinuxSuRen 已提交
74
		helper.CheckErr(cmd, err)
75 76 77 78 79 80 81 82
	},
}

func (j *JobArtifactDownloadOption) download(url, fileName string) (err error) {
	downloader := util.HTTPDownloader{
		RoundTripper:   j.RoundTripper,
		TargetFilePath: fileName,
		URL:            fmt.Sprintf("%s%s", j.Jenkins.URL, url),
83 84 85 86
		UserName:       j.Jenkins.UserName,
		Password:       j.Jenkins.Token,
		Proxy:          j.Jenkins.Proxy,
		ProxyAuth:      j.Jenkins.ProxyAuth,
87 88 89 90 91
		ShowProgress:   j.ShowProgress,
	}
	err = downloader.DownloadFile()
	return
}