Skip to content

Commit

Permalink
Add ability to specify config name
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien de Lartigue committed Jul 10, 2024
1 parent 1f38d88 commit 521f981
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea

config.yaml
*.yaml
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion example/config.yaml → example/main_example.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Configure your local db
# WARNING: This database will be dropped before restoring!!!
localDb:
host: "127.0.0.1"
port: 5432
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 20 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"log"
"os"
"path"
"path/filepath"
"sort"
)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit 521f981

Please sign in to comment.