From a785d2a16d4f844c0b164d523d11b24f062f044d Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie <1450685+LinuxSuRen@users.noreply.github.com> Date: Tue, 6 Apr 2021 20:31:32 +0800 Subject: [PATCH] Add a wizard when creating a pipeline (#100) Co-authored-by: rick <450685+LinuxSuRen@users.noreply.github.com> --- go.mod | 1 + go.sum | 3 +- kubectl-plugin/pipeline/create.go | 62 ++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 0455629..9fbe1ab 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 // indirect + golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c k8s.io/api v0.19.4 diff --git a/go.sum b/go.sum index 7c18d73..a6e4a59 100644 --- a/go.sum +++ b/go.sum @@ -501,8 +501,9 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/kubectl-plugin/pipeline/create.go b/kubectl-plugin/pipeline/create.go index 38bb1a0..43ef400 100644 --- a/kubectl-plugin/pipeline/create.go +++ b/kubectl-plugin/pipeline/create.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "github.com/AlecAivazis/survey/v2" "github.com/Masterminds/sprig" "github.com/linuxsuren/ks/kubectl-plugin/common" "github.com/linuxsuren/ks/kubectl-plugin/types" @@ -24,6 +25,7 @@ type pipelineCreateOption struct { Template string Type string SCMType string + Batch bool // Inner fields Client dynamic.Interface @@ -63,6 +65,7 @@ KubeSphere supports multiple types Pipeline. Currently, this CLI only support th "The type of pipeline, could be pipeline, multi_branch_pipeline") flags.StringVarP(&opt.SCMType, "scm-type", "", "", "The SCM type of pipeline, could be gitlab, github") + flags.BoolVarP(&opt.Batch, "batch", "b", false, "Create pipeline as batch mode") _ = cmd.RegisterFlagCompletionFunc("template", common.ArrayCompletion("java", "go", "simple", "multi-branch-gitlab")) _ = cmd.RegisterFlagCompletionFunc("type", common.ArrayCompletion("pipeline", "multi-branch-pipeline")) @@ -80,7 +83,64 @@ KubeSphere supports multiple types Pipeline. Currently, this CLI only support th return } -func (o *pipelineCreateOption) preRunE(_ *cobra.Command, args []string) (err error) { +func (o *pipelineCreateOption) wizard(_ *cobra.Command, _ []string) (err error) { + if o.Batch { + // without wizard in batch mode + return + } + + if o.Workspace == "" { + if o.Workspace, err = getInput("Please input the workspace name"); err != nil { + return + } + } + + if o.Project == "" { + if o.Project, err = getInput("Please input the project name"); err != nil { + return + } + } + + if o.Template == "" { + if o.Template, err = chooseOneFromArray([]string{"java", "go", "simple", "multi-branch-gitlab"}); err != nil { + return + } + } + + if o.Name == "" { + if o.Name, err = getInput("Please input the Pipeline name"); err != nil { + return + } + } + return +} + +func chooseOneFromArray(options []string) (result string, err error) { + prompt := &survey.Select{ + Message: "Please select:", + Options: options, + } + err = survey.AskOne(prompt, &result) + return +} + +func getInput(title string) (result string, err error) { + prompt := &survey.Input{ + Message: title, + } + err = survey.AskOne(prompt, &result) + return +} + +func (o *pipelineCreateOption) preRunE(cmd *cobra.Command, args []string) (err error) { + if o.Name == "" && len(args) > 0 { + o.Name = args[0] + } + + if err = o.wizard(cmd, args); err != nil { + return + } + switch o.Template { case "": case "java":