Commit 8549a7d4 authored by Christopher Harm's avatar Christopher Harm

Adding run command to setup environemtn from config

parent 1ed5a7e7
Pipeline #29398 passed with stages
in 37 seconds
...@@ -69,7 +69,7 @@ func fluxDeployCi(config config.Config) error { ...@@ -69,7 +69,7 @@ func fluxDeployCi(config config.Config) error {
} else { } else {
releaseName = deployable.Name + "-" + environmentSuffix releaseName = deployable.Name + "-" + environmentSuffix
} }
color.Blue("Updating Flux Release:", releaseName) color.Blue("Updating Flux Release:" + releaseName)
os.Setenv("KUBE_SERVICE_NAME", releaseName) os.Setenv("KUBE_SERVICE_NAME", releaseName)
os.Setenv("CI_PROJECT_NAME", image) os.Setenv("CI_PROJECT_NAME", image)
......
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"os"
"strings"
"git.psu.edu/k8s/devtool/environment"
"git.psu.edu/k8s/devtool/config"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var command string
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "run the application",
Run: func(cmd *cobra.Command, args []string) {
color.Blue("Running your application")
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgYellow).PrintfFunc()("WARNING: no project configuration exists: %s\n", configFile)
}
localConfig := selectLocalConfig(conf.Deployables)
if localConfig != "" {
var helmValues config.HelmValues
helmValues, err = config.ReadHelmValues(localConfig)
if err != nil {
color.Red("Error reading local config file: " + localConfig)
}
for k, v := range helmValues.EnvironmentVariables {
os.Setenv(k, v)
}
}
commandSplit := strings.Fields(command)
environment.RunRequired(true, commandSplit[0], commandSplit[1:]...)
},
}
func init() {
rootCmd.AddCommand(runCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// runCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
runCmd.Flags().StringVarP(&command, "command", "c", "", "The command to run")
runCmd.MarkFlagRequired("command")
}
func selectLocalConfig(deployables []config.Deployable) string {
if len(deployables) == 0 {
return ""
}
//if len(deployables) == 1 {
return deployables[0].LocalConfig
//}
//TODO add support for multiple deployables (prompt user and add arg)
}
...@@ -3,6 +3,8 @@ package config ...@@ -3,6 +3,8 @@ package config
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"gopkg.in/yaml.v2"
) )
type ( type (
...@@ -20,6 +22,9 @@ type ( ...@@ -20,6 +22,9 @@ type (
ChartVersion string `json:"chartVersion"` ChartVersion string `json:"chartVersion"`
LocalConfig string `json:"localConfig"` LocalConfig string `json:"localConfig"`
} }
HelmValues struct {
EnvironmentVariables map[string]string `yaml:"environmentVariables"`
}
) )
func (config Config) Write(filename string) error { func (config Config) Write(filename string) error {
...@@ -40,3 +45,14 @@ func New(filename string) (Config, error) { ...@@ -40,3 +45,14 @@ func New(filename string) (Config, error) {
err = json.Unmarshal(data, &config) err = json.Unmarshal(data, &config)
return config, err return config, err
} }
func ReadHelmValues(filename string) (HelmValues, error) {
data, err := ioutil.ReadFile(filename)
var helmValues HelmValues
if err != nil {
return helmValues, err
}
err = yaml.Unmarshal([]byte(data), &helmValues)
return helmValues, err
}
...@@ -17,4 +17,5 @@ require ( ...@@ -17,4 +17,5 @@ require (
github.com/spf13/viper v1.3.2 github.com/spf13/viper v1.3.2
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c // indirect
gopkg.in/yaml.v2 v2.2.2
) )
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