diff options
| author | Shulhan <ms@kilabit.info> | 2019-07-09 23:32:21 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-07-09 23:32:21 +0700 |
| commit | 038bbbe0ed83f221bb43a39cefe2dc3c743434b8 (patch) | |
| tree | 15bdf0c4be9c653d9647425aef3b94a2f1934e16 /cmd | |
| parent | b412fb5853219fee7c0fcad7bab8a72d846d7bc5 (diff) | |
| download | rescached-038bbbe0ed83f221bb43a39cefe2dc3c743434b8.tar.xz | |
cmd/rescached: make loading configuration file as optional
Previously, if configuration file is not defined it will be default
to "/etc/rescached/rescached.cfg" and if the file is not exist it will
return an error with nil options.
This changes, make the configuration file to be an optional. If its not
exist or error when parsing options, rescached will set it with default
values.
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/rescached/config.go | 18 | ||||
| -rw-r--r-- | cmd/rescached/main.go | 8 |
2 files changed, 11 insertions, 15 deletions
diff --git a/cmd/rescached/config.go b/cmd/rescached/config.go index dc31cb7..238c188 100644 --- a/cmd/rescached/config.go +++ b/cmd/rescached/config.go @@ -6,7 +6,6 @@ package main import ( "crypto/tls" - "fmt" "log" "strconv" "strings" @@ -25,14 +24,14 @@ const ( cfgSecRescached = "rescached" ) -func parseConfig(file string) (opts *rescached.Options, err error) { +func parseConfig(file string) (opts *rescached.Options) { + opts = rescached.NewOptions() + cfg, err := ini.Open(file) if err != nil { - return nil, err + return opts } - opts = rescached.NewOptions() - opts.FilePID, _ = cfg.Get(cfgSecRescached, "", "file.pid", "rescached.pid") opts.FileResolvConf, _ = cfg.Get(cfgSecRescached, "", "file.resolvconf", "") @@ -43,7 +42,7 @@ func parseConfig(file string) (opts *rescached.Options, err error) { err = parseNSParent(cfg, opts) if err != nil { - return nil, err + log.Println("rescached: parseConfig: " + err.Error()) } parseListen(cfg, opts) @@ -59,12 +58,13 @@ func parseConfig(file string) (opts *rescached.Options, err error) { if len(dohCertFile) > 0 && len(dohPrivateKey) > 0 { cert, err := tls.LoadX509KeyPair(dohCertFile, dohPrivateKey) if err != nil { - return nil, fmt.Errorf("rescached: error loading certificate: " + err.Error()) + log.Println("rescached: error loading certificate: " + err.Error()) + } else { + opts.DoHCertificate = &cert } - opts.DoHCertificate = &cert } - return opts, nil + return opts } func parseNSParent(cfg *ini.Ini, opts *rescached.Options) (err error) { diff --git a/cmd/rescached/main.go b/cmd/rescached/main.go index 3e5e8d9..59878d1 100644 --- a/cmd/rescached/main.go +++ b/cmd/rescached/main.go @@ -19,10 +19,7 @@ import ( ) func createRescachedServer(fileConfig string) *rescached.Server { - opts, err := parseConfig(fileConfig) - if err != nil { - log.Fatal(err) - } + opts := parseConfig(fileConfig) rcd, err := rescached.New(opts) if err != nil { @@ -63,12 +60,11 @@ func debugRuntime(rcd *rescached.Server) { func main() { var ( fileConfig string - defConfig = "/etc/rescached/rescached.cfg" ) log.SetFlags(0) - flag.StringVar(&fileConfig, "f", defConfig, "path to configuration") + flag.StringVar(&fileConfig, "f", "", "path to configuration") flag.Parse() rcd := createRescachedServer(fileConfig) |
