Commit b4cdb18f authored by Christopher Harm's avatar Christopher Harm

Imporving support for config init command

parent 8549a7d4
Pipeline #29407 passed with stages
in 48 seconds
...@@ -16,11 +16,14 @@ package cmd ...@@ -16,11 +16,14 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"git.psu.edu/k8s/devtool/environment"
"git.psu.edu/k8s/devtool/config" "git.psu.edu/k8s/devtool/config"
"github.com/fatih/color" "github.com/fatih/color"
...@@ -85,7 +88,7 @@ var initCmd = &cobra.Command{ ...@@ -85,7 +88,7 @@ var initCmd = &cobra.Command{
dockerfiles := findDockerfiles() dockerfiles := findDockerfiles()
for _, file := range dockerfiles { for _, file := range dockerfiles {
color.Yellow("Found Dockerfile: %s", file) color.New(color.FgYellow).Printf("Found Dockerfile: %s", file)
prompt := promptui.Prompt{ prompt := promptui.Prompt{
Label: "Include in config", Label: "Include in config",
Default: "Yes", Default: "Yes",
...@@ -121,6 +124,7 @@ var initCmd = &cobra.Command{ ...@@ -121,6 +124,7 @@ var initCmd = &cobra.Command{
} }
deployable.Name = response deployable.Name = response
// Ask for Chart
selectPrompt := promptui.SelectWithAdd{ selectPrompt := promptui.SelectWithAdd{
Label: "Chart", Label: "Chart",
Items: []string{"cm/eio-swe-service", "cm/eio-swe-cronjob", "cm/angular-client", "cm/eio-swe-jms-processor"}, 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{ ...@@ -132,31 +136,53 @@ var initCmd = &cobra.Command{
} }
deployable.Chart = response deployable.Chart = response
prompt = promptui.Prompt{ // Ask for Chart Version
Label: "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 { if err != nil {
return return
} }
deployable.ChartVersion = response deployable.ChartVersion = response
prompt = promptui.Prompt{ // Ask for Local Config
Label: "Local Config File", availableLocalConfigFiles := findLocalConfigs()
selectPrompt = promptui.SelectWithAdd{
Label: "Local Config File",
Items: availableLocalConfigFiles,
AddLabel: "Other",
} }
response, err = prompt.Run() _, response, err = selectPrompt.Run()
if err != nil { if err != nil {
return return
} }
deployable.LocalConfig = response 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) conf.Deployables = append(conf.Deployables, deployable)
color.Green("Adding Deployment") color.Green("Adding Deployment")
} }
conf.LocalEnvVars = []string{"OAUTH_CLIENT_ID"}
conf.LocalEnvVars = []string{"OAUTH_CLIENT_SECRET", "OAUTH_JWK"}
color.Yellow("Writing configuration") 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 { ...@@ -195,3 +221,32 @@ func findDockerfiles() []string {
} }
return dockerfiles 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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment