Skip to content

Commit

Permalink
Merge pull request #129 from WSMathias/create-select-list
Browse files Browse the repository at this point in the history
Show select list for crete command
  • Loading branch information
vs4vijay committed Jul 28, 2020
2 parents 5035ff2 + 59388e4 commit d1cd627
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
74 changes: 60 additions & 14 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"path/filepath"

survey "github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
common "github.com/srijanone/vega/pkg/common"
vega "github.com/srijanone/vega/pkg/core"
Expand All @@ -16,6 +17,7 @@ type createCmd struct {
starterkit string
dest string
repo string
path string
}

func newCreateCmd(out io.Writer) *cobra.Command {
Expand Down Expand Up @@ -43,10 +45,32 @@ func newCreateCmd(out io.Writer) *cobra.Command {
flags := createCmd.Flags()
flags.StringVarP(&cCmd.starterkit, "starterkit", "s", "", "name of the vega starterkit to scaffold the app")
flags.StringVarP(&cCmd.repo, "repo", "r", "default", "name of the starterkit repo")
cobra.MarkFlagRequired(flags, "starterkit")
// cobra.MarkFlagRequired(flags, "starterkit")
return createCmd
}

func askUserChoice(starterkits []vega.StarterKit) (vega.StarterKit, error) {
var sk vega.StarterKit
prompt := &survey.Select{
Message: "Select starterkit which you want to install:",
}
for _, starterkit := range starterkits {
prompt.Options = append(prompt.Options, starterkit.Name)
}
var skName = ""
err := survey.AskOne(prompt, &skName)
if err != nil {
return sk, err
}
for _, starterkit := range starterkits {
if skName == starterkit.Name {
sk = starterkit
break
}
}
return sk, nil
}

func (cCmd *createCmd) execute() error {
// TODO: Check if starterkits files are already there or not properly
dockerfileExists, err := common.Exists(filepath.Join(cCmd.dest, vega.DockerfileName))
Expand All @@ -59,27 +83,49 @@ func (cCmd *createCmd) execute() error {
return nil
}

path := filepath.Join(cCmd.home.StarterKits(), cCmd.repo)
repoPath := filepath.Join(cCmd.home.StarterKits(), cCmd.repo)
starterkitRepo := vega.StarterKitRepo{
Name: cCmd.repo,
Path: path,
Path: repoPath,
}

starterkits, err := starterkitRepo.Find(cCmd.starterkit)
if err != nil {
return err
}
var starterkit vega.StarterKit

if len(starterkits) == 1 {
starterkit := starterkits[0]
starterkit.Create(cCmd.dest)
} else if len(starterkits) > 0 {
// TODO: display proper list of matching kits
return fmt.Errorf("multiple starterkit named %s found: %v", cCmd.starterkit, starterkits)
if cCmd.starterkit == "" {
starterkits, err := starterkitRepo.StarterKitList()
if err != nil {
fmt.Fprintln(cCmd.out, "No starterkit found")
}
starterkit, err = askUserChoice(starterkits)
if err != nil {
return fmt.Errorf("Bad choice")
}
} else {
return fmt.Errorf("no starterkit found with name %s in %s repo", cCmd.starterkit, cCmd.repo)
starterkits, err := starterkitRepo.Find(cCmd.starterkit)
if err != nil || len(starterkits) == 0 {
fmt.Fprintln(cCmd.out, "No starterkit found")
return fmt.Errorf("No starterkit named %s found", cCmd.starterkit)
}
if len(starterkits) == 1 {
starterkit = starterkits[0]
fmt.Println(starterkit)
} else if len(starterkits) > 0 {
fmt.Fprintf(cCmd.out, "multiple starterkit named %s found:\n", cCmd.starterkit)
starterkit, err = askUserChoice(starterkits)
if err != nil {
return fmt.Errorf("Bad choice")
}
} else {
return fmt.Errorf("no starterkit found with name %s in %s repo", cCmd.starterkit, cCmd.repo)
}

}

err = starterkit.Create(cCmd.dest)
if err != nil {
fmt.Fprintln(cCmd.out, "Failed create starterkit")
return err
}
fmt.Fprintln(cCmd.out, "Ready for development")
return nil
}
2 changes: 1 addition & 1 deletion cmd/starterkit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cmd *starterkitListCmd) execute() error {
}
fmt.Fprintln(cmd.out, "Available starterkits:")
for _, starterkit := range starterkits {
fmt.Fprintf(cmd.out, " %10s (%s)\n", starterkit.Name, starterkit.Path)
fmt.Fprintf(cmd.out, " %s \n", starterkit.Name)
}
return nil
}

0 comments on commit d1cd627

Please sign in to comment.