Commit e4a60e15 authored by Christopher Harm's avatar Christopher Harm

Adding ENV configuration via viper

parent 8de40dae
Pipeline #30092 passed with stages
in 39 seconds
......@@ -25,12 +25,9 @@ import (
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var skipCompile bool
var runTests bool
var openServices bool
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
......@@ -77,13 +74,30 @@ func init() {
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
buildCmd.Flags().BoolVar(&skipCompile, "skip-compile", false, "Skip the compile step")
buildCmd.Flags().BoolVar(&runTests, "tests", false, "Run unit tests during the build phase")
buildCmd.Flags().BoolVar(&openServices, "open", false, "Open browser to the services after deploying")
buildCmd.Flags().Bool("skip-compile", false, "Skip the compile step")
viper.SetDefault("build.skipCompile", false)
err := viper.BindPFlag("build.skipCompile", buildCmd.Flags().Lookup("skip-compile"))
if err != nil {
panic("invalid arg")
}
buildCmd.Flags().Bool("tests", false, "Run unit tests during the build phase")
viper.SetDefault("build.runTests", false)
err = viper.BindPFlag("build.runTests", buildCmd.Flags().Lookup("tests"))
if err != nil {
panic("invalid arg")
}
buildCmd.Flags().Bool("open", false, "Open browser to the services after deploying")
viper.SetDefault("build.openServices", false)
err = viper.BindPFlag("build.openServices", buildCmd.Flags().Lookup("open"))
if err != nil {
panic("invalid arg")
}
}
func build(config config.Config) error {
if skipCompile {
if viper.GetBool("build.skipCompile") {
color.Yellow("Skipping Build Step.")
return nil
}
......@@ -93,7 +107,7 @@ func build(config config.Config) error {
switch lang := config.Language; lang {
case "java":
args := []string{"clean", "install", "-Ddependency-check.skip=true"}
if !runTests {
if !viper.GetBool("build.runTests") {
args = append(args, "-DskipTests")
}
environment.RunRequired(true, "mvn", args...)
......@@ -158,7 +172,7 @@ func buildDockerAndDeploy(config config.Config) error {
return err
}
if openServices {
if viper.GetBool("build.openServices") {
err = environment.Run(true, "minikube", "service", releaseName)
if err != nil {
return err
......
......@@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
......@@ -81,10 +82,14 @@ func initConfig() {
}
viper.AutomaticEnv() // read in environment variables that match
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
color.Red(err.Error())
}
}
......
......@@ -17,25 +17,29 @@ package cmd
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var Memory string
var Cpu string
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "start the minikube environment",
Run: func(command *cobra.Command, args []string) {
fmt.Println("start called")
start(Memory, Cpu)
color.Red(viper.GetString("example"))
memory := strconv.Itoa(viper.GetInt("minikube.memory"))
cpu := strconv.Itoa(viper.GetInt("minikube.cpu"))
start(memory, cpu)
},
}
......@@ -51,9 +55,19 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
startCmd.PersistentFlags().StringVarP(&Memory, "memory", "m", "8192", "memory for minikube (default is 8192)")
startCmd.PersistentFlags().StringVarP(&Cpu, "cpr", "c", "6", "cpr for minikube (default is 6)")
startCmd.PersistentFlags().IntP("memory", "m", 0, "memory for minikube (default is 8192)")
viper.SetDefault("minikube.memory", 8192)
err := viper.BindPFlag("minikube.memory", startCmd.PersistentFlags().Lookup("memory"))
if err != nil {
panic("invalid arg")
}
startCmd.PersistentFlags().IntP("cpu", "c", 0, "cpu for minikube (default is 6)")
viper.SetDefault("minikube.cpu", 6)
err = viper.BindPFlag("minikube.cpu", startCmd.PersistentFlags().Lookup("cpu"))
if err != nil {
panic("invalid arg")
}
}
func start(memory, cpu string) {
......@@ -70,7 +84,7 @@ func start(memory, cpu string) {
color.Blue("Setting up Minikube environment.")
fmt.Printf("Using memory: %s and cpr: %s\n", memory, cpu)
fmt.Printf("Using memory: %s and cpu: %s\n", memory, cpu)
err := environment.Run(true, "minikube", "status")
if err != nil {
......
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