Skip to content

Commit

Permalink
Add a wizard when creating a pipeline (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and ritawilliam committed Apr 6, 2021
1 parent b0e4bf6 commit a785d2a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
62 changes: 61 additions & 1 deletion kubectl-plugin/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,6 +25,7 @@ type pipelineCreateOption struct {
Template string
Type string
SCMType string
Batch bool

// Inner fields
Client dynamic.Interface
Expand Down Expand Up @@ -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"))
Expand All @@ -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":
Expand Down

0 comments on commit a785d2a

Please sign in to comment.