From b4cdb18fcd486d5251d69e3ebf893ed8d1135365 Mon Sep 17 00:00:00 2001 From: Christopher Harm Date: Fri, 22 Mar 2019 16:42:56 -0400 Subject: [PATCH] Imporving support for config init command --- cmd/config_init.go | 71 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/cmd/config_init.go b/cmd/config_init.go index 0de207c..464468f 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -16,11 +16,14 @@ package cmd import ( "fmt" + "io/ioutil" "log" "os" "path/filepath" "strings" + "git.psu.edu/k8s/devtool/environment" + "git.psu.edu/k8s/devtool/config" "github.com/fatih/color" @@ -85,7 +88,7 @@ var initCmd = &cobra.Command{ dockerfiles := findDockerfiles() for _, file := range dockerfiles { - color.Yellow("Found Dockerfile: %s", file) + color.New(color.FgYellow).Printf("Found Dockerfile: %s", file) prompt := promptui.Prompt{ Label: "Include in config", Default: "Yes", @@ -121,6 +124,7 @@ var initCmd = &cobra.Command{ } deployable.Name = response + // Ask for Chart selectPrompt := promptui.SelectWithAdd{ Label: "Chart", Items: []string{"cm/eio-swe-service", "cm/eio-swe-cronjob", "cm/angular-client", "cm/eio-swe-jms-processor"}, @@ -132,31 +136,53 @@ var initCmd = &cobra.Command{ } deployable.Chart = response - prompt = promptui.Prompt{ - Label: "Chart Version", + // Ask for Chart Version + availableChartVersions := findChartVersions(response) + + selectPrompt = promptui.SelectWithAdd{ + Label: "Chart Version", + Items: availableChartVersions, + AddLabel: "Other", } - response, err = prompt.Run() + _, response, err = selectPrompt.Run() if err != nil { return } deployable.ChartVersion = response - prompt = promptui.Prompt{ - Label: "Local Config File", + // Ask for Local Config + availableLocalConfigFiles := findLocalConfigs() + + selectPrompt = promptui.SelectWithAdd{ + Label: "Local Config File", + Items: availableLocalConfigFiles, + AddLabel: "Other", } - response, err = prompt.Run() + _, response, err = selectPrompt.Run() if err != nil { return } deployable.LocalConfig = response + // Create the file if it doesn't exist already + _, err = os.OpenFile(response, os.O_RDONLY|os.O_CREATE, 0666) + if err != nil { + color.Yellow("Unable to create config file") + } + conf.Deployables = append(conf.Deployables, deployable) color.Green("Adding Deployment") } + conf.LocalEnvVars = []string{"OAUTH_CLIENT_ID"} + conf.LocalEnvVars = []string{"OAUTH_CLIENT_SECRET", "OAUTH_JWK"} + color.Yellow("Writing configuration") - conf.Write(configFile) + err = conf.Write(configFile) + if err != nil { + color.New(color.FgRed).Printf("Error writing configuration file: %s", configFile) + } }, } @@ -195,3 +221,32 @@ func findDockerfiles() []string { } return dockerfiles } + +func findChartVersions(chartName string) []string { + var versions []string + output := environment.RunAndGetOutputRequired("helm", "search", chartName, "--versions") + for i, line := range output { + if i == 0 { + continue + } + fields := strings.Fields(line) + if len(fields) < 4 { + continue + } + versions = append(versions, fields[1]) + } + return versions +} + +func findLocalConfigs() []string { + files, err := ioutil.ReadDir("config/") + if err != nil { + log.Println(err) + } + + var filenames []string + for _, f := range files { + filenames = append(filenames, "config/"+f.Name()) + } + return filenames +} -- GitLab