-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
job_artifact_download.go
96 lines (83 loc) · 2.74 KB
/
job_artifact_download.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cmd
import (
"fmt"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http"
"path/filepath"
"strconv"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client"
httpdownloader "github.com/linuxsuren/http-downloader/pkg"
"github.com/spf13/cobra"
)
// JobArtifactDownloadOption is the options of job artifact download command
type JobArtifactDownloadOption struct {
ID string
ShowProgress bool
DownloadDir string
Jenkins *appCfg.JenkinsServer
RoundTripper http.RoundTripper
}
var jobArtifactDownloadOption JobArtifactDownloadOption
func init() {
jobArtifactCmd.AddCommand(jobArtifactDownloadCmd)
jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.ID, "id", "i", "",
i18n.T("ID of the job artifact"))
jobArtifactDownloadCmd.Flags().BoolVarP(&jobArtifactDownloadOption.ShowProgress, "progress", "", true,
i18n.T("Whether show the progress"))
jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.DownloadDir, "download-dir", "", "",
i18n.T("The directory which artifact will be downloaded"))
}
var jobArtifactDownloadCmd = &cobra.Command{
Use: "download <jobName> [buildID]",
Short: i18n.T("Download the artifact of target job"),
Long: i18n.T(`Download the artifact of target job`),
Args: cobra.MinimumNArgs(1),
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 = getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))
artifacts, err := jclient.List(jobName, buildID)
if err == nil {
for _, artifact := range artifacts {
if jobArtifactDownloadOption.ID != "" && jobArtifactDownloadOption.ID != artifact.ID {
continue
}
downloadPath := filepath.Join(jobArtifactDownloadOption.DownloadDir, artifact.Name)
err = jobArtifactDownloadOption.download(artifact.URL, downloadPath)
if err != nil {
break
}
}
}
helper.CheckErr(cmd, err)
},
}
func (j *JobArtifactDownloadOption) download(url, fileName string) (err error) {
downloader := httpdownloader.HTTPDownloader{
RoundTripper: j.RoundTripper,
TargetFilePath: fileName,
URL: fmt.Sprintf("%s%s", j.Jenkins.URL, url),
UserName: j.Jenkins.UserName,
Password: j.Jenkins.Token,
Proxy: j.Jenkins.Proxy,
ProxyAuth: j.Jenkins.ProxyAuth,
ShowProgress: j.ShowProgress,
}
err = downloader.DownloadFile()
return
}