credential_create.go 3.4 KB
Newer Older
1 2 3 4
package cmd

import (
	"fmt"
5 6
	"net/http"

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	"github.com/jenkins-zh/jenkins-cli/app/i18n"
	"github.com/jenkins-zh/jenkins-cli/client"
	"github.com/spf13/cobra"
)

// CredentialCreateOption option for credential delete command
type CredentialCreateOption struct {
	Description string
	ID          string
	Store       string

	Username string
	Password string

	Secret string

LinuxSuRen's avatar
LinuxSuRen 已提交
23 24
	Scope string
	Type  string
25 26 27 28 29 30 31 32 33 34 35 36

	RoundTripper http.RoundTripper
}

var credentialCreateOption CredentialCreateOption

func init() {
	credentialCmd.AddCommand(credentialCreateCmd)
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Store, "store", "", "system",
		i18n.T("The store name of Jenkins credentials"))
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Type, "type", "", "basic",
		i18n.T("The type of Jenkins credentials which could be: basic, secret"))
LinuxSuRen's avatar
LinuxSuRen 已提交
37 38 39
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Scope, "scope", "", "GLOBAL",
		i18n.T("The scope of Jenkins credentials which might be GLOBAL or SYSTEM"))
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.ID, "credential-id", "", "",
40
		i18n.T("The ID of Jenkins credentials"))
LinuxSuRen's avatar
LinuxSuRen 已提交
41
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Username, "credential-username", "", "",
42
		i18n.T("The Username of Jenkins credentials"))
LinuxSuRen's avatar
LinuxSuRen 已提交
43
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Password, "credential-password", "", "",
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
		i18n.T("The Password of Jenkins credentials"))
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Description, "desc", "", "",
		i18n.T("The Description of Jenkins credentials"))
	credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Secret, "secret", "", "",
		i18n.T("The Secret of Jenkins credentials"))
}

var credentialCreateCmd = &cobra.Command{
	Use:   "create [store] [id]",
	Short: i18n.T("Create a credential from Jenkins"),
	Long:  i18n.T("Create a credential from Jenkins"),
	PreRunE: func(cmd *cobra.Command, args []string) (err error) {
		if len(args) >= 1 {
			credentialCreateOption.Store = args[0]
		}

		if credentialCreateOption.Store == "" {
			err = fmt.Errorf("the store or id of target credential is empty")
		}
		return
	},
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		jClient := &client.CredentialsManager{
			JenkinsCore: client.JenkinsCore{
				RoundTripper: credentialCreateOption.RoundTripper,
				Debug:        true,
			},
		}
		getCurrentJenkinsAndClient(&(jClient.JenkinsCore))

		switch credentialCreateOption.Type {
		case "basic":
			err = jClient.CreateUsernamePassword(credentialCreateOption.Store, client.UsernamePasswordCredential{
				Username: credentialCreateOption.Username,
				Password: credentialCreateOption.Password,
				Credential: client.Credential{
LinuxSuRen's avatar
LinuxSuRen 已提交
80
					Scope:       credentialCreateOption.Scope,
81 82 83 84 85 86 87 88
					ID:          credentialCreateOption.ID,
					Description: credentialCreateOption.Description,
				},
			})
		case "secret":
			err = jClient.CreateSecret(credentialCreateOption.Store, client.StringCredentials{
				Secret: credentialCreateOption.Secret,
				Credential: client.Credential{
LinuxSuRen's avatar
LinuxSuRen 已提交
89
					Scope:       credentialCreateOption.Scope,
90 91 92 93 94 95 96 97 98
					ID:          credentialCreateOption.ID,
					Description: credentialCreateOption.Description,
				},
			})
		default:
			err = fmt.Errorf("unknow credential type: %s", credentialCreateOption.Type)
		}
		return
	},
99 100 101
	Annotations: map[string]string{
		since: "v0.0.24",
	},
102
}