Skip to content

Commit

Permalink
add option to skip loading config file
Browse files Browse the repository at this point in the history
  • Loading branch information
mlusetti authored and orgrim committed Sep 2, 2023
1 parent 350a3d7 commit 8f8e8ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
10 changes: 7 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ import (
_ "embed"
"errors"
"fmt"
"github.com/anmitsu/go-shlex"
"github.com/spf13/pflag"
"gopkg.in/ini.v1"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/anmitsu/go-shlex"
"github.com/spf13/pflag"
"gopkg.in/ini.v1"
)

var defaultCfgFile = "/etc/pg_back/pg_back.conf"
Expand All @@ -46,6 +47,7 @@ var defaultCfg string

// options struct holds command line and configuration file options
type options struct {
NoConfigFile bool
BinDirectory string
Directory string
Host string
Expand Down Expand Up @@ -112,6 +114,7 @@ func defaultOptions() options {
}

return options{
NoConfigFile: false,
Directory: "/var/backups/postgresql",
Format: 'c',
DirJobs: 1,
Expand Down Expand Up @@ -245,6 +248,7 @@ func parseCli(args []string) (options, []string, error) {
pflag.PrintDefaults()
}

pflag.BoolVar(&opts.NoConfigFile, "no-config-file", false, "skip reading config file\n")
pflag.StringVarP(&opts.BinDirectory, "bin-directory", "B", "", "PostgreSQL binaries directory. Empty to search $PATH")
pflag.StringVarP(&opts.Directory, "backup-directory", "b", "/var/backups/postgresql", "store dump files there")
pflag.StringVarP(&opts.CfgFile, "config", "c", defaultCfgFile, "alternate config file")
Expand Down
9 changes: 5 additions & 4 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ package main
import (
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/pflag"
"gopkg.in/ini.v1"
"io/ioutil"
"os"
"runtime"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/pflag"
"gopkg.in/ini.v1"
)

func TestValidateDumpFormat(t *testing.T) {
Expand Down
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,23 @@ func run() (retVal error) {
// Enable verbose mode or quiet mode as soon as possible
l.SetVerbosity(cliOpts.Verbose, cliOpts.Quiet)

// Load configuration file and allow the default configuration
// file to be absent
configOpts, err := loadConfigurationFile(cliOpts.CfgFile)
if err != nil {
return err
var cliOptions options

if cliOpts.NoConfigFile {
l.Infoln("*** Skipping reading config file")
cliOptions = defaultOptions()
} else {
// Load configuration file and allow the default configuration
// file to be absent
cliOptions, err = loadConfigurationFile(cliOpts.CfgFile)
if err != nil {
return err
}
}

// override options from the configuration file with ones from
// the command line
opts := mergeCliAndConfigOptions(cliOpts, configOpts, cliOptList)
opts := mergeCliAndConfigOptions(cliOpts, cliOptions, cliOptList)

// Ensure a non-empty passphrase is set when asking for encryption
if (opts.Encrypt || opts.Decrypt) && len(opts.CipherPassphrase) == 0 {
Expand Down

0 comments on commit 8f8e8ee

Please sign in to comment.