diff --git a/app/cmd/common.go b/app/cmd/common.go index 395951794a85d7f4f08be97b2e87ccd36a8e77fd..70bf4b68de7d889c20cb28effbd8ded913092195 100644 --- a/app/cmd/common.go +++ b/app/cmd/common.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" "gopkg.in/yaml.v2" "io" @@ -13,7 +14,6 @@ import ( "go.uber.org/zap" - "github.com/AlecAivazis/survey/v2" "github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/util" @@ -32,6 +32,9 @@ type CommonOption struct { RoundTripper http.RoundTripper Stdio terminal.Stdio + + // EditFileName allow editor has a better performance base on this + EditFileName string } // OutputOption represent the format of output @@ -237,9 +240,16 @@ type Selector interface { // Editor edit a file than return the content func (o *CommonOption) Editor(defaultContent, message string) (content string, err error) { + var fileName string + if o.EditFileName != "" { + fileName = o.EditFileName + } else { + fileName = "*.sh" + } + prompt := &survey.Editor{ Message: message, - FileName: "*.sh", + FileName: fileName, Default: defaultContent, HideDefault: true, AppendDefault: true, @@ -321,3 +331,10 @@ func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *JenkinsSe func GetAliasesDel() []string { return []string{"remove", "del"} } + +// GetEditorHelpText returns the help text related a text editor +func GetEditorHelpText() string { + return `notepad is the default editor of Windows, vim is the default editor of unix. +But if the environment variable "VISUAL" or "EDITOR" exists, jcli will take it. +For example, you can set it under unix like this: export VISUAL=vi` +} diff --git a/app/cmd/config_edit.go b/app/cmd/config_edit.go index 5ba62177466d6a153dcbd0784aea48bf1eef47d5..843176f4e825375df5259b8d9d503fed9b760441 100644 --- a/app/cmd/config_edit.go +++ b/app/cmd/config_edit.go @@ -25,7 +25,8 @@ func init() { var configEditCmd = &cobra.Command{ Use: "edit", Short: i18n.T("Edit a Jenkins config"), - Long: i18n.T(`Edit a Jenkins config`), + Long: i18n.T(fmt.Sprintf(`Edit a Jenkins config +%s`, GetEditorHelpText())), RunE: func(_ *cobra.Command, _ []string) (err error) { current := getCurrentJenkinsFromOptions() configPath := configOptions.ConfigFileLocation @@ -34,6 +35,7 @@ var configEditCmd = &cobra.Command{ if data, err = ioutil.ReadFile(configPath); err == nil { content := string(data) //Help: fmt.Sprintf("Config file path: %s", configPath), + configEditOption.EditFileName = ".jenkins-cli.yaml" content, err = configEditOption.Editor(content, fmt.Sprintf("Edit config item %s", current.Name)) if err == nil { err = ioutil.WriteFile(configPath, []byte(content), 0644) diff --git a/app/cmd/job_edit.go b/app/cmd/job_edit.go index fe88964a423405178e4f59d87d7a259e06846540..e2a3208486f895965d3cf5af19736910d42f82ad 100644 --- a/app/cmd/job_edit.go +++ b/app/cmd/job_edit.go @@ -1,6 +1,8 @@ package cmd import ( + "encoding/base64" + "fmt" "io/ioutil" "net/http" @@ -17,6 +19,7 @@ type JobEditOption struct { Filename string Script string URL string + Sample bool } var jobEditOption JobEditOption @@ -29,14 +32,19 @@ func init() { i18n.T("Filename to files to use to replace pipeline")) jobEditCmd.Flags().StringVarP(&jobEditOption.Script, "script", "s", "", i18n.T("Script to use to replace pipeline. Use script first if you give filename at the meantime.")) + jobEditCmd.Flags().BoolVarP(&jobEditOption.Sample, "sample", "", false, + i18n.T("Give it a sample Jenkinsfile if the target script is empty")) jobEditOption.Stdio = GetSystemStdio() } var jobEditCmd = &cobra.Command{ - Use: "edit ", + Use: "edit", Short: i18n.T("Edit the job of your Jenkins"), - Long: i18n.T(`Edit the job of your Jenkins. We only support to edit the pipeline job.`), - Args: cobra.MinimumNArgs(1), + Long: i18n.T(fmt.Sprintf(`Edit the job of your Jenkins. We only support to edit the pipeline job. +Official Pipeline syntax document is here https://jenkins.io/doc/book/pipeline/syntax/ + +%s`, GetEditorHelpText())), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { name := args[0] var content string @@ -55,6 +63,27 @@ var jobEditCmd = &cobra.Command{ }, } +func (j *JobEditOption) getSampleJenkinsfile() string { + return `pipeline { + agent { + label 'master' + } + stages { + stage('Example') { + steps { + echo 'Hello World' + } + } + } + post { + always { + echo 'I will always say Hello again!' + } + } +} +` +} + //func getPipeline(name string) (script string, err error) { func (j *JobEditOption) getPipeline(jClient *client.JobClient, name string) (script string, err error) { script = j.Script //we take the script from input firstly @@ -77,6 +106,13 @@ func (j *JobEditOption) getPipeline(jClient *client.JobClient, name string) (scr if job != nil { content = job.Script } + + // if the original script is empty, give it a sample script + if content == "" && j.Sample { + content = j.getSampleJenkinsfile() + } + + j.EditFileName = fmt.Sprintf("Jenkinsfile.%s.groovy", base64.StdEncoding.EncodeToString([]byte(name))) script, err = j.Editor(content, "Edit your pipeline script") } return diff --git a/app/cmd/user_edit.go b/app/cmd/user_edit.go index e4ed2923102bcb1a5f0a0ab4cf68a8911e7911c8..f58f1a3273451784a3583f08c494af91c558b4a4 100644 --- a/app/cmd/user_edit.go +++ b/app/cmd/user_edit.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/client" "github.com/spf13/cobra" @@ -25,7 +26,8 @@ func init() { var userEditCmd = &cobra.Command{ Use: "edit", Short: "Edit the user of your Jenkins", - Long: `Edit the user of your Jenkins`, + Long: fmt.Sprintf(`Edit the user of your Jenkins +%s`, GetEditorHelpText()), RunE: func(_ *cobra.Command, _ []string) (err error) { jClient := &client.UserClient{ JenkinsCore: client.JenkinsCore{