From 810620e2796211ca20413d8147637a316028a7de Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 25 Aug 2022 11:23:04 +1000 Subject: [PATCH] First pass at updating initConfig and adding tests --- cmd/root.go | 43 +++++++----- cmd/root_test.go | 56 ++++++++++++++++ .../initial-test/intial-test-lagoon.yml | 67 +++++++++++++++++++ 3 files changed, 151 insertions(+), 15 deletions(-) create mode 100644 cmd/root_test.go create mode 100644 test-resources/config-tests/initial-test/intial-test-lagoon.yml diff --git a/cmd/root.go b/cmd/root.go index 6803ced..43ed380 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,9 +2,10 @@ package cmd import ( "fmt" - "github.com/uselagoon/lagoon-sync/assets" "os" + "github.com/uselagoon/lagoon-sync/assets" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -56,7 +57,6 @@ func init() { rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } -// initConfig reads in config file and ENV variables if set. func initConfig() { // Find home directory. home, err := homedir.Dir() @@ -64,11 +64,24 @@ func initConfig() { fmt.Println(err) os.Exit(1) } + + paths := []string{home, "/lagoon", "/tmp"} + + cfgFile, err = processConfigEnv(paths, cfgFile) + if err != nil { + utils.LogFatalError("Unable to read in config file", err) + os.Exit(1) + } +} + +// initConfig reads in config file and ENV variables if set. +func processConfigEnv(paths []string, DefaultConfigFileName string) (string, error) { + // Search config in home directory with name ".lagoon-sync" (without extension). - viper.AddConfigPath(home) - viper.AddConfigPath("/lagoon") - viper.AddConfigPath("/tmp") - viper.SetConfigName(cfgFile) + for _, path := range paths { + viper.AddConfigPath(path) + } + viper.SetConfigName(DefaultConfigFileName) viper.SetConfigType("yaml") // Find default config file for env vars (e.g. 'lagoon-sync-defaults') @@ -87,25 +100,25 @@ func initConfig() { lagoonSyncCfgFile = "/lagoon/.lagoon-sync" } - if cfgFile != "" { + if DefaultConfigFileName != "" { // Use config file from the flag, default for this is '.lagoon.yml' - if utils.FileExists(cfgFile) { - viper.SetConfigName(cfgFile) - viper.SetConfigFile(cfgFile) + if utils.FileExists(DefaultConfigFileName) { + viper.SetConfigName(DefaultConfigFileName) + viper.SetConfigFile(DefaultConfigFileName) } // Set '.lagoon-sync-defaults' as config file is it exists. if utils.FileExists(lagoonSyncDefaultsFile) { viper.SetConfigName(lagoonSyncDefaultsFile) viper.SetConfigFile(lagoonSyncDefaultsFile) - cfgFile = lagoonSyncDefaultsFile + DefaultConfigFileName = lagoonSyncDefaultsFile } // Set '.lagoon-sync' as config file is it exists. if utils.FileExists(lagoonSyncCfgFile) { viper.SetConfigName(lagoonSyncCfgFile) viper.SetConfigFile(lagoonSyncCfgFile) - cfgFile = lagoonSyncCfgFile + DefaultConfigFileName = lagoonSyncCfgFile } } @@ -114,8 +127,8 @@ func initConfig() { // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { utils.LogDebugInfo("Using config file", viper.ConfigFileUsed()) + } else { + return "", err } - if err := viper.ReadInConfig(); err != nil { - utils.LogFatalError("Unable to read in config file", err) - } + return DefaultConfigFileName, nil } diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..6c39c75 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,56 @@ +package cmd + +import "testing" + +func Test_processConfigEnv(t *testing.T) { + type args struct { + paths []string + DefaultConfigFileName string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "Initial test", + args: args{ + paths: []string{"../test-resources/config-tests/initial-test"}, + DefaultConfigFileName: "intial-test-lagoon.yml", + }, + want: "intial-test-lagoon.yml", + wantErr: false, + }, + { + name: "Initial test - Empty path", + args: args{ + paths: []string{}, + DefaultConfigFileName: "../test-resources/config-tests/initial-test/intial-test-lagoon.yml", + }, + want: "../test-resources/config-tests/initial-test/intial-test-lagoon.yml", + wantErr: false, + }, + { + name: "Initial test - Multiple paths", + args: args{ + paths: []string{"../test-resources/sync-test/tests-defaults", "../test-resources/sync-test/tests-lagoon-yml", "../test-resources/config-tests/initial-test"}, + DefaultConfigFileName: ".lagoon.yml", + }, + want: ".lagoon.yml", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := processConfigEnv(tt.args.paths, tt.args.DefaultConfigFileName) + if (err != nil) != tt.wantErr { + t.Errorf("processConfigEnv() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("processConfigEnv() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/test-resources/config-tests/initial-test/intial-test-lagoon.yml b/test-resources/config-tests/initial-test/intial-test-lagoon.yml new file mode 100644 index 0000000..6735aa3 --- /dev/null +++ b/test-resources/config-tests/initial-test/intial-test-lagoon.yml @@ -0,0 +1,67 @@ +# Example .lagoon.yml file with lagoon-sync config added which is used by the sync tool. +docker-compose-yaml: docker-compose.yml + +project: "lagoon-sync" + +lagoon-sync: + mariadb: + config: + hostname: "$MARIADB_HOST" + username: "$MARIADB_USERNAME" + password: "$MARIADB_PASSWORD" + port: "$MARIADB_PORT" + database: "$MARIADB_DATABASE" + ignore-table: + - "table_to_ignore" + ignore-table-data: + - "cache_data" + - "cache_menu" + local: + config: + hostname: "mariadb" + username: "drupal" + password: "drupal" + port: "3306" + database: "drupal" + postgres: + config: + hostname: "$POSTGRES_HOST" + username: "$POSTGRES_USERNAME" + password: "$POSTGRES_PASSWORD" + port: "5432" + database: "$POSTGRES_DATABASE" + exclude-table: + - "table_to_ignore" + exclude-table-data: + - "cache_data" + - "cache_menu" + local: + config: + hostname: "postgres" + username: "drupal" + password: "drupal" + port: "3306" + database: "drupal" + mongodb: + config: + hostname: "$MONGODB_HOST" + port: "$MONGODB_SERVICE_PORT" + database: "MONGODB_DATABASE" + local: + config: + hostname: "$MONGODB_HOST" + port: "27017" + database: "local" + files: + config: + sync-directory: "/app/web/sites/default/files" + local: + config: + sync-directory: "/app/web/sites/default/files" + drupalconfig: + config: + syncpath: "./config/sync" + local: + overrides: + config: + syncpath: "./config/sync"