center_download.go 1.9 KB
Newer Older
1 2 3 4 5
package cmd

import (
	"net/http"

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

8 9 10 11 12 13
	"github.com/jenkins-zh/jenkins-cli/client"
	"github.com/spf13/cobra"
)

// CenterDownloadOption as the options of download command
type CenterDownloadOption struct {
14 15 16
	LTS    bool
	Mirror string

17
	Output       string
LinuxSuRen's avatar
LinuxSuRen 已提交
18
	ShowProgress bool
19 20 21 22 23 24 25 26 27

	RoundTripper http.RoundTripper
}

var centerDownloadOption CenterDownloadOption

func init() {
	centerCmd.AddCommand(centerDownloadCmd)
	centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.LTS, "lts", "", true, "If you want to download Jenkins as LTS")
28 29
	centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Mirror, "mirror", "m", "default", "The mirror site of Jenkins")
	centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.ShowProgress, "progress", "p", true, "If you want to show the download progress")
30 31 32 33 34 35
	centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Output, "output", "o", "jenkins.war", "The file of output")
}

var centerDownloadCmd = &cobra.Command{
	Use:   "download",
	Short: "Download Jenkins",
36 37 38 39 40 41 42 43 44
	Long:  `Download Jenkins from a mirror site. You can get more mirror sites from https://jenkins-zh.cn/tutorial/management/mirror/`,
	Run: func(cmd *cobra.Command, _ []string) {
		config := getConfig()
		mirrorSite := centerDownloadOption.getMirrorSite(config)
		if mirrorSite == "" {
			cmd.PrintErrln("cannot found Jenkins mirror by:", centerDownloadOption.Mirror)
			return
		}

45
		jclient := &client.UpdateCenterManager{
46
			MirrorSite: mirrorSite,
47 48 49 50 51
			JenkinsCore: client.JenkinsCore{
				RoundTripper: centerDownloadOption.RoundTripper,
			},
		}

LinuxSuRen's avatar
LinuxSuRen 已提交
52 53 54
		err := jclient.DownloadJenkins(centerDownloadOption.LTS, centerDownloadOption.ShowProgress,
			centerDownloadOption.Output)
		helper.CheckErr(cmd, err)
55 56
	},
}
57 58 59 60 61 62 63 64 65 66 67

func (c *CenterDownloadOption) getMirrorSite(config *Config) (site string) {
	mirrors := getMirrors()
	for _, mirror := range mirrors {
		if mirror.Name == c.Mirror {
			site = mirror.URL
			return
		}
	}
	return
}