未验证 提交 f182fd5b 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Allow users to edit a empty pipeline with a sample (#361)

* Allow users to edit a empty pipeline with a sample

* encode the filename
上级 2e106d9a
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal" "github.com/AlecAivazis/survey/v2/terminal"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io" "io"
...@@ -13,7 +14,6 @@ import ( ...@@ -13,7 +14,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
...@@ -32,6 +32,9 @@ type CommonOption struct { ...@@ -32,6 +32,9 @@ type CommonOption struct {
RoundTripper http.RoundTripper RoundTripper http.RoundTripper
Stdio terminal.Stdio Stdio terminal.Stdio
// EditFileName allow editor has a better performance base on this
EditFileName string
} }
// OutputOption represent the format of output // OutputOption represent the format of output
...@@ -237,9 +240,16 @@ type Selector interface { ...@@ -237,9 +240,16 @@ type Selector interface {
// Editor edit a file than return the content // Editor edit a file than return the content
func (o *CommonOption) Editor(defaultContent, message string) (content string, err error) { 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{ prompt := &survey.Editor{
Message: message, Message: message,
FileName: "*.sh", FileName: fileName,
Default: defaultContent, Default: defaultContent,
HideDefault: true, HideDefault: true,
AppendDefault: true, AppendDefault: true,
...@@ -321,3 +331,10 @@ func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *JenkinsSe ...@@ -321,3 +331,10 @@ func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *JenkinsSe
func GetAliasesDel() []string { func GetAliasesDel() []string {
return []string{"remove", "del"} 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`
}
...@@ -25,7 +25,8 @@ func init() { ...@@ -25,7 +25,8 @@ func init() {
var configEditCmd = &cobra.Command{ var configEditCmd = &cobra.Command{
Use: "edit", Use: "edit",
Short: i18n.T("Edit a Jenkins config"), 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) { RunE: func(_ *cobra.Command, _ []string) (err error) {
current := getCurrentJenkinsFromOptions() current := getCurrentJenkinsFromOptions()
configPath := configOptions.ConfigFileLocation configPath := configOptions.ConfigFileLocation
...@@ -34,6 +35,7 @@ var configEditCmd = &cobra.Command{ ...@@ -34,6 +35,7 @@ var configEditCmd = &cobra.Command{
if data, err = ioutil.ReadFile(configPath); err == nil { if data, err = ioutil.ReadFile(configPath); err == nil {
content := string(data) content := string(data)
//Help: fmt.Sprintf("Config file path: %s", configPath), //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)) content, err = configEditOption.Editor(content, fmt.Sprintf("Edit config item %s", current.Name))
if err == nil { if err == nil {
err = ioutil.WriteFile(configPath, []byte(content), 0644) err = ioutil.WriteFile(configPath, []byte(content), 0644)
......
package cmd package cmd
import ( import (
"encoding/base64"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -17,6 +19,7 @@ type JobEditOption struct { ...@@ -17,6 +19,7 @@ type JobEditOption struct {
Filename string Filename string
Script string Script string
URL string URL string
Sample bool
} }
var jobEditOption JobEditOption var jobEditOption JobEditOption
...@@ -29,14 +32,19 @@ func init() { ...@@ -29,14 +32,19 @@ func init() {
i18n.T("Filename to files to use to replace pipeline")) i18n.T("Filename to files to use to replace pipeline"))
jobEditCmd.Flags().StringVarP(&jobEditOption.Script, "script", "s", "", 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.")) 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() jobEditOption.Stdio = GetSystemStdio()
} }
var jobEditCmd = &cobra.Command{ var jobEditCmd = &cobra.Command{
Use: "edit <jobName>", Use: "edit",
Short: i18n.T("Edit the job of your Jenkins"), 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.`), Long: i18n.T(fmt.Sprintf(`Edit the job of your Jenkins. We only support to edit the pipeline job.
Args: cobra.MinimumNArgs(1), 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) { RunE: func(cmd *cobra.Command, args []string) (err error) {
name := args[0] name := args[0]
var content string var content string
...@@ -55,6 +63,27 @@ var jobEditCmd = &cobra.Command{ ...@@ -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 getPipeline(name string) (script string, err error) {
func (j *JobEditOption) getPipeline(jClient *client.JobClient, 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 script = j.Script //we take the script from input firstly
...@@ -77,6 +106,13 @@ func (j *JobEditOption) getPipeline(jClient *client.JobClient, name string) (scr ...@@ -77,6 +106,13 @@ func (j *JobEditOption) getPipeline(jClient *client.JobClient, name string) (scr
if job != nil { if job != nil {
content = job.Script 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") script, err = j.Editor(content, "Edit your pipeline script")
} }
return return
......
package cmd package cmd
import ( import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -25,7 +26,8 @@ func init() { ...@@ -25,7 +26,8 @@ func init() {
var userEditCmd = &cobra.Command{ var userEditCmd = &cobra.Command{
Use: "edit", Use: "edit",
Short: "Edit the user of your Jenkins", 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) { RunE: func(_ *cobra.Command, _ []string) (err error) {
jClient := &client.UserClient{ jClient := &client.UserClient{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册