未验证 提交 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
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`
}
......@@ -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)
......
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 <jobName>",
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
......
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{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册