-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
center_download.go
111 lines (94 loc) · 3.3 KB
/
center_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cmd
import (
"fmt"
"github.com/mitchellh/go-homedir"
"go.uber.org/zap"
"net/http"
"os"
"path/filepath"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)
// CenterDownloadOption as the options of download command
type CenterDownloadOption struct {
LTS bool
Mirror string
Version string
Output string
ShowProgress bool
Formula string
Thread int
RoundTripper http.RoundTripper
}
var centerDownloadOption CenterDownloadOption
func init() {
centerCmd.AddCommand(centerDownloadCmd)
centerDownloadCmd.Flags().BoolVarP(¢erDownloadOption.LTS, "lts", "", true,
i18n.T("If you want to download Jenkins as LTS"))
centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Version, "war-version", "", "",
i18n.T("Version of the Jenkins which you want to download"))
centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Mirror, "mirror", "m", "default",
i18n.T("The mirror site of Jenkins"))
centerDownloadCmd.Flags().BoolVarP(¢erDownloadOption.ShowProgress, "progress", "p", true,
i18n.T("If you want to show the download progress"))
centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Output, "output", "o", "",
i18n.T("The file of output"))
centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Formula, "formula", "", "",
i18n.T("The formula of jenkins.war, only support zh currently"))
centerDownloadCmd.Flags().IntVarP(¢erDownloadOption.Thread, "thread", "t", 0,
"Using multi-thread to download jenkins.war")
}
var centerDownloadCmd = &cobra.Command{
Use: "download",
Short: i18n.T("Download jenkins.war"),
Long: i18n.T(`Download jenkins.war from a mirror site.
You can get more mirror sites from https://jenkins-zh.cn/tutorial/management/mirror/
If you want to download different formulas of jenkins.war, please visit the following project
https://github.com/jenkins-zh/jenkins-formulas`),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
if err = centerDownloadOption.DownloadJenkins(); err == nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "download success, %s\n", centerDownloadOption.Output)
}
return err
},
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
if centerDownloadOption.Output != "" {
return
}
var userHome string
if userHome, err = homedir.Dir(); err != nil {
return
}
centerDownloadOption.Output = fmt.Sprintf("%s/.jenkins-cli/cache/%s/jenkins.war", userHome, centerDownloadOption.Version)
return
},
}
// DownloadJenkins download the Jenkins
func (c *CenterDownloadOption) DownloadJenkins() (err error) {
parentDir := filepath.Dir(c.Output)
if err = os.MkdirAll(parentDir, os.FileMode(0755)); err != nil {
return
}
mirrorSite := getMirror(c.Mirror)
if mirrorSite == "" {
err = fmt.Errorf("cannot found Jenkins mirror by: %s", c.Mirror)
return
}
jClient := &client.UpdateCenterManager{
MirrorSite: mirrorSite,
JenkinsCore: client.JenkinsCore{
RoundTripper: c.RoundTripper,
},
Formula: c.Formula,
LTS: c.LTS,
Version: c.Version,
Output: c.Output,
ShowProgress: c.ShowProgress,
Thread: c.Thread,
}
jenkinsWarURL := jClient.GetJenkinsWarURL()
logger.Info("prepare to download jenkins.war", zap.String("URL", jenkinsWarURL))
err = jClient.DownloadJenkins()
return
}