plugin_upload.go 3.8 KB
Newer Older
1 2 3
package cmd

import (
4 5 6
	"fmt"
	"io/ioutil"
	"log"
LinuxSuRen's avatar
LinuxSuRen 已提交
7
	"net/http"
8 9 10 11
	"os"
	"path/filepath"
	"strings"

yJunS's avatar
yJunS 已提交
12
	"github.com/jenkins-zh/jenkins-cli/client"
13
	"github.com/jenkins-zh/jenkins-cli/util"
14 15 16
	"github.com/spf13/cobra"
)

17 18 19 20 21 22
// PluginUploadOption will hold the options of plugin cmd
type PluginUploadOption struct {
	Remote         string
	RemoteUser     string
	RemotePassword string
	RemoteJenkins  string
LinuxSuRen's avatar
LinuxSuRen 已提交
23 24 25 26 27
	ShowProgress   bool

	RoundTripper http.RoundTripper

	HookOption
28 29 30 31 32 33

	pluginFilePath string
}

var pluginUploadOption PluginUploadOption

34 35
func init() {
	pluginCmd.AddCommand(pluginUploadCmd)
LinuxSuRen's avatar
LinuxSuRen 已提交
36
	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.ShowProgress, "show-progress", "", true, "Whether show the upload progress")
37 38 39 40
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.Remote, "remote", "r", "", "Remote plugin URL")
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteUser, "remote-user", "", "", "User of remote plugin URL")
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemotePassword, "remote-password", "", "", "Password of remote plugin URL")
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteJenkins, "remote-jenkins", "", "", "Remote Jenkins which will find from config list")
LinuxSuRen's avatar
LinuxSuRen 已提交
41 42 43

	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPreHook, "skip-prehook", "", false, "Whether skip the previous command hook")
	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPostHook, "skip-posthook", "", false, "Whether skip the post command hook")
44 45 46
}

var pluginUploadCmd = &cobra.Command{
47 48 49 50 51
	Use:     "upload",
	Aliases: []string{"up"},
	Short:   "Upload a plugin  to your Jenkins",
	Long:    `Upload a plugin from local filesystem or remote URL to your Jenkins`,
	Example: `  jcli plugin upload --remote https://server/sample.hpi
LinuxSuRen's avatar
LinuxSuRen 已提交
52 53
jcli plugin upload sample.hpi
jcli plugin upload sample.hpi --show-progress=false`,
LinuxSuRen's avatar
LinuxSuRen 已提交
54
	PreRun: func(cmd *cobra.Command, args []string) {
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
		if pluginUploadOption.Remote != "" {
			file, err := ioutil.TempFile(".", "jcli-plugin")
			if err != nil {
				log.Fatal(err)
			}

			defer os.Remove(file.Name())

			if pluginUploadOption.RemoteJenkins != "" {
				if jenkins := findJenkinsByName(pluginUploadOption.RemoteJenkins); jenkins != nil {
					pluginUploadOption.RemoteUser = jenkins.UserName
					pluginUploadOption.RemotePassword = jenkins.Token
				}
			}

			pluginUploadOption.pluginFilePath = fmt.Sprintf("%s.hpi", file.Name())
			downloader := util.HTTPDownloader{
				TargetFilePath: pluginUploadOption.pluginFilePath,
				URL:            pluginUploadOption.Remote,
				UserName:       pluginUploadOption.RemoteUser,
				Password:       pluginUploadOption.RemotePassword,
				ShowProgress:   true,
				Debug:          rootOptions.Debug,
			}

			if err := downloader.DownloadFile(); err != nil {
				log.Fatal(err)
			}
		} else if len(args) == 0 {
LinuxSuRen's avatar
LinuxSuRen 已提交
84 85 86
			if !pluginUploadOption.SkipPreHook {
				executePreCmd(cmd, args, os.Stdout)
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
87

88 89 90 91 92 93 94 95 96 97
			path, _ := os.Getwd()
			dirName := filepath.Base(path)
			dirName = strings.Replace(dirName, "-plugin", "", -1)
			path += fmt.Sprintf("/target/%s.hpi", dirName)

			pluginUploadOption.pluginFilePath = path
		} else {
			pluginUploadOption.pluginFilePath = args[0]
		}
	},
LinuxSuRen's avatar
LinuxSuRen 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	PostRun: func(cmd *cobra.Command, args []string) {
		if pluginUploadOption.SkipPostHook {
			return
		}

		executePostCmd(cmd, args, cmd.OutOrStdout())
	},
	Run: func(cmd *cobra.Command, _ []string) {
		jclient := &client.PluginManager{
			JenkinsCore: client.JenkinsCore{
				RoundTripper: pluginUploadOption.RoundTripper,
				Output:       cmd.OutOrStdout(),
			},
			ShowProgress: pluginUploadOption.ShowProgress,
		}
		getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
114 115 116 117 118
		jclient.Debug = rootOptions.Debug

		if pluginUploadOption.Remote != "" {
			defer os.Remove(pluginUploadOption.pluginFilePath)
		}
119

120
		jclient.Upload(pluginUploadOption.pluginFilePath)
121 122
	},
}