Skip to content

Commit

Permalink
Merge pull request #111 from kubernauts/interactive-prompt
Browse files Browse the repository at this point in the history
Add interactive option for cluster install
  • Loading branch information
arashkaffamanesh committed Jul 19, 2019
2 parents 6b4bba4 + 2763a44 commit 5f8b12b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/cli/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"errors"
"log"
"os"
"strings"

Expand Down Expand Up @@ -53,6 +54,17 @@ var provisionerInstallCmd = &cobra.Command{
Long: `This command will provide the infrastructure which is needed to install and run kubernetes on your selected platform.`,
Args: ArgsValidation,
Run: func(cmd *cobra.Command, args []string) {
if provisioner.Interactive {
provisionerName, _ := InteractiveMode()
strArray := strings.Fields(provisionerName)
if val, ok := provisioners[provisionerName]; ok {
val.Init(strArray[1:])
if !provisioner.IOnly {
val.Setup(strArray[1:])
}
}
}

if val, ok := provisioners[args[0]]; ok {
val.Init(args[1:])
if !provisioner.IOnly {
Expand Down Expand Up @@ -132,7 +144,28 @@ Kindly ensure that terraform is installed also.`,
},
}

func InteractiveMode() (string, error) {
provisionerName := common.ProvisionerList()
if _, err := os.Stat("./provisioner/" + provisionerName); err == nil {
return " ", nil
}
err := os.Mkdir("./provisioner", 0755)
if err != nil {
log.Fatalln(err)
}

common.CloneGit("./provisioner", "https://github.com/kubernauts/tk8-provisioner-"+provisionerName, provisionerName)
common.ReplaceGit("./provisioner/" + provisionerName)
return provisionerName, nil

}

func ArgsValidation(cmd *cobra.Command, args []string) error {

if provisioner.Interactive {
return nil

}
if len(args) < 1 {
return errors.New("requires at least one arg")
}
Expand Down Expand Up @@ -167,6 +200,7 @@ func init() {

provisionerInstallCmd.Flags().StringVar(&common.Name, "name", common.Name, "name of the cluster workspace")
provisionerInstallCmd.Flags().BoolVarP(&provisioner.IOnly, "ionly", "i", provisioner.IOnly, "setup only the infrastructure")
provisionerInstallCmd.Flags().BoolVarP(&provisioner.Interactive, "interactive", "a", provisioner.Interactive, "Interactive mode for selecting provisioner")
provisionerScaleCmd.Flags().StringVar(&common.Name, "name", common.Name, "name of the cluster workspace")
provisionerResetCmd.Flags().StringVar(&common.Name, "name", common.Name, "name of the cluster workspace")
provisionerRemoveCmd.Flags().StringVar(&common.Name, "name", common.Name, "name of the cluster workspace")
Expand Down
16 changes: 16 additions & 0 deletions pkg/common/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"bufio"
"fmt"
"github.com/manifoldco/promptui"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -197,3 +199,17 @@ func CopyDir(src string, dst string) (err error) {

return
}


func ProvisionerList() string{
prompt := promptui.Select{
Label: "Select the Tk8 provisioner",
Items: []string{"aws", "cattle-aws", "eks", "rke"},
}
_, result, err := prompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
fmt.Printf("You chose %q\n", result)
return result
}
1 change: 1 addition & 0 deletions pkg/provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var IOnly bool = false
var Interactive bool

type Provisioner interface {
Init(args []string)
Expand Down

0 comments on commit 5f8b12b

Please sign in to comment.