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

import (
4
	"fmt"
LinuxSuRen's avatar
LinuxSuRen 已提交
5
	"github.com/jenkins-zh/jenkins-cli/app/helper"
6
	"github.com/jenkins-zh/jenkins-cli/app/i18n"
7 8
	"io/ioutil"
	"log"
LinuxSuRen's avatar
LinuxSuRen 已提交
9
	"net/http"
10 11 12 13
	"os"
	"path/filepath"
	"strings"

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

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

	RoundTripper http.RoundTripper

	HookOption
31 32 33 34 35 36

	pluginFilePath string
}

var pluginUploadOption PluginUploadOption

37 38
func init() {
	pluginCmd.AddCommand(pluginUploadCmd)
39 40
	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.ShowProgress, "show-progress", "", true,
		i18n.T("Whether show the upload progress"))
41 42
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.FileName, "file", "f", "",
		i18n.T("The plugin file path which should end with .hpi"))
43 44 45 46 47 48 49 50 51 52 53 54 55
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.Remote, "remote", "r", "",
		i18n.T("Remote plugin URL"))
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteUser, "remote-user", "", "",
		i18n.T("User of remote plugin URL"))
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemotePassword, "remote-password", "", "",
		i18n.T("Password of remote plugin URL"))
	pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteJenkins, "remote-jenkins", "", "",
		i18n.T("Remote Jenkins which will find from config list"))

	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPreHook, "skip-prehook", "", false,
		i18n.T("Whether skip the previous command hook"))
	pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPostHook, "skip-posthook", "", false,
		i18n.T("Whether skip the post command hook"))
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	if err := pluginUploadCmd.RegisterFlagCompletionFunc("file", pluginUploadOption.HPICompletion); err != nil {
		pluginCmd.PrintErrln(err)
	}
}

func (o *PluginUploadOption) HPICompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
	if len(args) != 0 {
		return nil, cobra.ShellCompDirectiveNoFileComp
	}

	targetFiles := make([]string, 0)
	if files, err := filepath.Glob("*.hpi"); err == nil {
		targetFiles = append(targetFiles, files...)
	}
	if files, err := filepath.Glob("target/*.hpi"); err == nil {
		targetFiles = append(targetFiles, files...)
	}
	return targetFiles, cobra.ShellCompDirectiveNoFileComp
75 76 77
}

var pluginUploadCmd = &cobra.Command{
78 79
	Use:     "upload",
	Aliases: []string{"up"},
80 81
	Short:   i18n.T("Upload a plugin  to your Jenkins"),
	Long:    i18n.T(`Upload a plugin from local filesystem or remote URL to your Jenkins`),
82
	Example: `  jcli plugin upload --remote https://server/sample.hpi
LinuxSuRen's avatar
LinuxSuRen 已提交
83 84
jcli plugin upload sample.hpi
jcli plugin upload sample.hpi --show-progress=false`,
LinuxSuRen's avatar
LinuxSuRen 已提交
85
	PreRun: func(cmd *cobra.Command, args []string) {
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 112 113 114
		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 已提交
115 116 117
			if !pluginUploadOption.SkipPreHook {
				executePreCmd(cmd, args, os.Stdout)
			}
LinuxSuRen's avatar
LinuxSuRen 已提交
118

119 120 121 122 123 124 125 126 127 128
			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 已提交
129 130 131 132 133 134 135
	PostRun: func(cmd *cobra.Command, args []string) {
		if pluginUploadOption.SkipPostHook {
			return
		}

		executePostCmd(cmd, args, cmd.OutOrStdout())
	},
136
	ValidArgsFunction: pluginUploadOption.HPICompletion,
LinuxSuRen's avatar
LinuxSuRen 已提交
137 138 139 140 141 142 143 144
	Run: func(cmd *cobra.Command, _ []string) {
		jclient := &client.PluginManager{
			JenkinsCore: client.JenkinsCore{
				RoundTripper: pluginUploadOption.RoundTripper,
				Output:       cmd.OutOrStdout(),
			},
			ShowProgress: pluginUploadOption.ShowProgress,
		}
145
		getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))
146 147 148 149 150
		jclient.Debug = rootOptions.Debug

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

LinuxSuRen's avatar
LinuxSuRen 已提交
152 153
		err := jclient.Upload(pluginUploadOption.pluginFilePath)
		helper.CheckErr(cmd, err)
154 155
	},
}