From 588617a82108e2e7de96a673dd31e7e224258756 Mon Sep 17 00:00:00 2001 From: Dallin Date: Sat, 16 May 2020 15:47:55 -0600 Subject: [PATCH] add switch command to update enviornment file --- cmd/clone.go | 2 +- cmd/root.go | 8 ++++-- cmd/switch.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/switch_test.go | 34 ++++++++++++++++++++++ test_fixtures/.env | 1 + 5 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 cmd/switch.go create mode 100644 cmd/switch_test.go create mode 100644 test_fixtures/.env diff --git a/cmd/clone.go b/cmd/clone.go index 2c6c0d4..478172b 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -132,7 +132,7 @@ func addDumpToStdin(importCmd *exec.Cmd, file *os.File) { _, err = io.WriteString(stdin, string(bytes)) if err != nil { - log.Fatal(err) + log.Fatal(err.Error()) } stdin.Close() diff --git a/cmd/root.go b/cmd/root.go index a7e4e48..3b2e081 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -88,9 +88,11 @@ func initConfig() { viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { + if err := viper.ReadInConfig(); err != nil { + fmt.Println("Problem using config file:", viper.ConfigFileUsed()) + } + + if (Verbose) { fmt.Println("Using config file:", viper.ConfigFileUsed()) - } else { - fmt.Println(err) } } diff --git a/cmd/switch.go b/cmd/switch.go new file mode 100644 index 0000000..738fa87 --- /dev/null +++ b/cmd/switch.go @@ -0,0 +1,71 @@ +/* +Copyright © 2020 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 ( + "fmt" + "github.com/spf13/cobra" + "io/ioutil" + "log" + "os" + "regexp" +) + +// switchCmd represents the switch command +var switchCmd = &cobra.Command{ + Use: "switch", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + dir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + db_name, _ := cmd.Flags().GetString("db_name") + + RunSwitch(dir, db_name) + }, +} + +func RunSwitch(path string, db_name string) (bool, error) { + + file_path := fmt.Sprintf("%s/%s", path, ".env") + + contents, err := ioutil.ReadFile(file_path) + + if (err != nil) { + return false, err + } + + expr := regexp.MustCompile("DB_DATABASE=(.*)") + + new_contents := expr.ReplaceAllString(string(contents), fmt.Sprintf("DB_DATABASE=%s", db_name)) + + ioutil.WriteFile(file_path, []byte(new_contents), 0644) + + return true, nil +} + +func init() { + switchCmd.Flags().String("db_name", "example_db", "Specify the name of the database to switch to") + rootCmd.AddCommand(switchCmd) +} diff --git a/cmd/switch_test.go b/cmd/switch_test.go new file mode 100644 index 0000000..28565bc --- /dev/null +++ b/cmd/switch_test.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "io/ioutil" + "log" + "os" + "testing" +) + +func TestEnvFileExists(t *testing.T) { + dir, _ := os.Getwd() + + _, err := RunSwitch(dir, "doesntmatter") + + if err == nil { + log.Fatalf("error should have been thrown for missing environment file") + } +} + +func TestReplaceDatabaseNameInEnvFile(t *testing.T) { + ioutil.WriteFile("../test_fixtures/.env", []byte("DB_DATABASE=fakedb1"), 0644) + + _, err := RunSwitch("../test_fixtures", "newfakedb1") + + if (err != nil) { + log.Fatal(err) + } + + contents, _ := ioutil.ReadFile("../test_fixtures/.env") + + if string(contents) != "DB_DATABASE=newfakedb1" { + log.Fatalf("contents does not equal DB_DATABASE=newfakedb1, received %s", string(contents)) + } +} diff --git a/test_fixtures/.env b/test_fixtures/.env new file mode 100644 index 0000000..03ba806 --- /dev/null +++ b/test_fixtures/.env @@ -0,0 +1 @@ +DB_DATABASE=newfakedb1 \ No newline at end of file