diff --git a/cmd/ci_deploy.go b/cmd/ci_deploy.go index e5bc1cd2fcde397fbfcbe992af9782aabbbeed30..682672441ee8c051eeb07e0cc8c5cceccf70b328 100644 --- a/cmd/ci_deploy.go +++ b/cmd/ci_deploy.go @@ -69,7 +69,7 @@ func fluxDeployCi(config config.Config) error { } else { releaseName = deployable.Name + "-" + environmentSuffix } - color.Blue("Updating Flux Release:", releaseName) + color.Blue("Updating Flux Release:" + releaseName) os.Setenv("KUBE_SERVICE_NAME", releaseName) os.Setenv("CI_PROJECT_NAME", image) diff --git a/cmd/run.go b/cmd/run.go new file mode 100644 index 0000000000000000000000000000000000000000..61e5a3d7312a9710c7d5d0486e7beeba37af841a --- /dev/null +++ b/cmd/run.go @@ -0,0 +1,87 @@ +// Copyright © 2019 NAME HERE +// +// 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) + +} diff --git a/config/config.go b/config/config.go index 5e6423278cf7c2ebea75d008bdb8e9e5fcfb0750..9112c945184f5c4b53fd742e8adaa7253738f72f 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,8 @@ package config import ( "encoding/json" "io/ioutil" + + "gopkg.in/yaml.v2" ) type ( @@ -20,6 +22,9 @@ type ( ChartVersion string `json:"chartVersion"` LocalConfig string `json:"localConfig"` } + HelmValues struct { + EnvironmentVariables map[string]string `yaml:"environmentVariables"` + } ) func (config Config) Write(filename string) error { @@ -40,3 +45,14 @@ func New(filename string) (Config, error) { err = json.Unmarshal(data, &config) 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 +} diff --git a/go.mod b/go.mod index ca513bdab48a0f9a50b3f5d3029aa34ea43bcbb3..88d1010e2edfd9f673bd9562fc34dd2e6c9e862c 100644 --- a/go.mod +++ b/go.mod @@ -17,4 +17,5 @@ require ( github.com/spf13/viper v1.3.2 golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c // indirect + gopkg.in/yaml.v2 v2.2.2 )