diff --git a/.gitignore b/.gitignore index 36fc180..ddf75f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea -config.yaml \ No newline at end of file +*.yaml \ No newline at end of file diff --git a/README.md b/README.md index af5875e..20cf060 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,16 @@ mv psqlbu-v0.x.0-[darwin/linux]-[amd64/arm64] psqlbu Add the directory to your $PATH to allow the command to be run from anywhere. ## Configuration -The util requires a `config.yaml` to be located in the same directory as the executable. +The util requires a configuration file to be located in the same directory as the executable, followed by the location where the command was run. By default, it will search for `main.yaml`, but a different file can be specified (see Flags) -An example config can be found [here](https://github.com/anytimesoon/psql-remote-backup-restore/blob/main/example/config.yaml) +An example config can be found [here](https://github.com/anytimesoon/psql-remote-backup-restore/blob/main/example/main_example.yaml) + +## Flags + +`-config [fileName]` - where file name is the name of the file without the `.yaml` extension. For example, to use the configuration in the file `custom_config.yaml` you should run +``` +psqlbu -config custom_config +``` ## Args There are two run time arguments which allow the user to bypass either the backup or restore portion of the operation. These will override the config. diff --git a/example/config.yaml b/example/main_example.yaml similarity index 88% rename from example/config.yaml rename to example/main_example.yaml index 6ee0185..ea78732 100644 --- a/example/config.yaml +++ b/example/main_example.yaml @@ -1,4 +1,5 @@ # Configure your local db +# WARNING: This database will be dropped before restoring!!! localDb: host: "127.0.0.1" port: 5432 @@ -35,7 +36,7 @@ backupOptions: - "--no-owner" - "--no-privileges" -# Only run the restore functions (optional, default True) +# Only run the restore functions (optional, default False) shouldRestore: True # Only run the backup functions (optional, default True) diff --git a/main.go b/main.go index 44d4816..b589b6d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "io/fs" "log" "os" + "path" "path/filepath" "sort" ) @@ -32,12 +33,22 @@ var ( ) func init() { - viper.SetConfigName("config") - viper.AddConfigPath(".") - err := viper.ReadInConfig() + configName := flag.String("config", "main", "custom config name") + flag.Parse() + ex, err := os.Executable() + if err != nil { + log.Fatal(err) + } + dir := path.Dir(ex) + log.Print(dir) + + viper.SetConfigName(*configName) + viper.AddConfigPath(dir) + viper.AddConfigPath("./") + err = viper.ReadInConfig() if err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { - log.Fatalln("Could not find config file. Please make sure it is in the same directory as the executable and named config.yaml.", err) + log.Fatalf("Could not find config file. Please make sure it is in the same directory as the executable and named %s.yaml.\n %s", *configName, err) } else { log.Fatalln("Failed to read config file.", err) } @@ -61,19 +72,21 @@ func init() { findExecutables() pathToBackups = viper.GetString("directories.backups") - if pathToBackups[len(pathToBackups)-1:len(pathToBackups)] != "/" { + if pathToBackups == "" { + pathToBackups = dir + "backups/" + } + if pathToBackups[len(pathToBackups)-1:] != "/" { pathToBackups = pathToBackups + "/" } backupOptions = viper.GetStringSlice("backupOptions") restoreOptions = viper.GetStringSlice("restoreOptions") - viper.SetDefault("shouldRestore", true) + viper.SetDefault("shouldRestore", false) shouldRestore = viper.GetBool("shouldRestore") viper.SetDefault("shouldBackup", true) shouldBackup = viper.GetBool("shouldBackup") - flag.Parse() args := flag.Args() for _, arg := range args { switch arg {