Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at updating initConfig and adding tests #66

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,19 +57,31 @@ 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()
if err != nil {
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')
Expand All @@ -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
}
}

Expand All @@ -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
}
56 changes: 56 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
67 changes: 67 additions & 0 deletions test-resources/config-tests/initial-test/intial-test-lagoon.yml
Original file line number Diff line number Diff line change
@@ -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"