|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/mitchellh/go-homedir" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "golang.org/x/crypto/ssh" |
| 8 | + "gopkg.in/src-d/go-git.v4" |
| 9 | + "gopkg.in/src-d/go-git.v4/plumbing/transport" |
| 10 | + githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" |
| 11 | + gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh" |
| 12 | + "io/ioutil" |
| 13 | + "os" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +// NewConfigPluginFetchCmd create a command for fetching plugin metadata |
| 18 | +func NewConfigPluginFetchCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) { |
| 19 | + pluginFetchCmd := jcliPluginFetchCmd{ |
| 20 | + PluginOrg: pluginOrg, |
| 21 | + PluginRepo: pluginRepo, |
| 22 | + } |
| 23 | + |
| 24 | + cmd = &cobra.Command{ |
| 25 | + Use: "fetch", |
| 26 | + Short: "fetch metadata of plugins", |
| 27 | + Long: fmt.Sprintf(`fetch metadata of plugins |
| 28 | +The official metadata git repository is https://github.com/%s/%s, |
| 29 | +but you can change it by giving a command parameter.`, pluginFetchCmd.PluginOrg, pluginFetchCmd.PluginRepo), |
| 30 | + ValidArgsFunction: NoFileCompletion, |
| 31 | + RunE: pluginFetchCmd.Run, |
| 32 | + } |
| 33 | + |
| 34 | + // add flags |
| 35 | + flags := cmd.Flags() |
| 36 | + flags.StringVarP(&pluginFetchCmd.PluginRepo, "plugin-repo", "", |
| 37 | + fmt.Sprintf("https://github.com/%s/%s", pluginFetchCmd.PluginOrg, pluginFetchCmd.PluginRepo), |
| 38 | + "The plugin git repository URL") |
| 39 | + flags.BoolVarP(&pluginFetchCmd.Reset, "reset", "", true, |
| 40 | + "If you want to reset the git local repo when pulling it") |
| 41 | + flags.StringVarP(&pluginFetchCmd.Username, "username", "u", "", |
| 42 | + "The username of git repository") |
| 43 | + flags.StringVarP(&pluginFetchCmd.Password, "password", "p", "", |
| 44 | + "The password of git repository") |
| 45 | + |
| 46 | + sshKeyFile := fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME")) |
| 47 | + flags.StringVarP(&pluginFetchCmd.SSHKeyFile, "ssh-key-file", "", sshKeyFile, |
| 48 | + "SSH key file") |
| 49 | + return |
| 50 | +} |
| 51 | + |
| 52 | +// Run is the main entry point of plugin fetch command |
| 53 | +func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error) { |
| 54 | + var userHome string |
| 55 | + if userHome, err = homedir.Dir(); err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + pluginRepo := fmt.Sprintf("%s/.jenkins-cli/plugins-repo", userHome) |
| 60 | + c.output = cmd.OutOrStdout() |
| 61 | + |
| 62 | + var r *git.Repository |
| 63 | + if r, err = git.PlainOpen(pluginRepo); err == nil { |
| 64 | + var w *git.Worktree |
| 65 | + if w, err = r.Worktree(); err != nil { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if c.Reset { |
| 70 | + if err = w.Reset(&git.ResetOptions{ |
| 71 | + Mode: git.HardReset, |
| 72 | + }); err != nil { |
| 73 | + return |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + err = w.Pull(c.getPullOptions()) |
| 78 | + if err == git.NoErrAlreadyUpToDate { |
| 79 | + err = nil // consider it's ok |
| 80 | + } |
| 81 | + } else { |
| 82 | + cloneOptions := c.getCloneOptions() |
| 83 | + _, err = git.PlainClone(pluginRepo, false, cloneOptions) |
| 84 | + } |
| 85 | + return |
| 86 | +} |
| 87 | + |
| 88 | +func (c *jcliPluginFetchCmd) getCloneOptions() (cloneOptions *git.CloneOptions) { |
| 89 | + cloneOptions = &git.CloneOptions{ |
| 90 | + URL: c.PluginRepo, |
| 91 | + Progress: c.output, |
| 92 | + Auth: c.getAuth(), |
| 93 | + } |
| 94 | + return |
| 95 | +} |
| 96 | + |
| 97 | +func (c *jcliPluginFetchCmd) getPullOptions() (pullOptions *git.PullOptions) { |
| 98 | + pullOptions = &git.PullOptions{ |
| 99 | + RemoteName: "origin", |
| 100 | + Progress: c.output, |
| 101 | + Auth: c.getAuth(), |
| 102 | + } |
| 103 | + return |
| 104 | +} |
| 105 | + |
| 106 | +func (c *jcliPluginFetchCmd) getAuth() (auth transport.AuthMethod) { |
| 107 | + if c.Username != "" { |
| 108 | + auth = &githttp.BasicAuth{ |
| 109 | + Username: c.Username, |
| 110 | + Password: c.Password, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if strings.HasPrefix(c.PluginRepo, "git@") { |
| 115 | + if sshKey, err := ioutil.ReadFile(c.SSHKeyFile); err == nil { |
| 116 | + signer, _ := ssh.ParsePrivateKey(sshKey) |
| 117 | + auth = &gitssh.PublicKeys{User: "git", Signer: signer} |
| 118 | + } |
| 119 | + } |
| 120 | + return |
| 121 | +} |
0 commit comments