From fee17a1a765ab64380f61cdcfe10c30014b01e00 Mon Sep 17 00:00:00 2001 From: David Hill Date: Mon, 27 Jun 2016 15:00:48 -0400 Subject: [PATCH] Quit when the specified configuration is file not found. (#273) Also, error on an invalid rpc certificate. --- cmd/dcrctl/config.go | 3 ++- cmd/dcrctl/httpclient.go | 5 ++++- config.go | 16 ++++------------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cmd/dcrctl/config.go b/cmd/dcrctl/config.go index 34863d6d..036b8ed3 100644 --- a/cmd/dcrctl/config.go +++ b/cmd/dcrctl/config.go @@ -215,7 +215,8 @@ func loadConfig() (*config, []string, error) { parser := flags.NewParser(&cfg, flags.Default) err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile) if err != nil { - if _, ok := err.(*os.PathError); !ok { + _, ok := err.(*os.PathError) + if !ok || preCfg.ConfigFile != cfg.ConfigFile { fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n", err) fmt.Fprintln(os.Stderr, usageMessage) diff --git a/cmd/dcrctl/httpclient.go b/cmd/dcrctl/httpclient.go index 3554c35d..eaf8cece 100644 --- a/cmd/dcrctl/httpclient.go +++ b/cmd/dcrctl/httpclient.go @@ -44,7 +44,10 @@ func newHTTPClient(cfg *config) (*http.Client, error) { } pool := x509.NewCertPool() - pool.AppendCertsFromPEM(pem) + if ok := pool.AppendCertsFromPEM(pem); !ok { + return nil, fmt.Errorf("invalid certificate file: %v", + cfg.RPCCert) + } tlsConfig = &tls.Config{ RootCAs: pool, InsecureSkipVerify: cfg.TLSSkipVerify, diff --git a/config.go b/config.go index e2b8ded2..f8955f02 100644 --- a/config.go +++ b/config.go @@ -412,20 +412,19 @@ func loadConfig() (*config, []string, error) { } // Load additional config from file. - var configFileError error parser := newConfigParser(&cfg, &serviceOpts, flags.Default) if !(preCfg.SimNet) || preCfg.ConfigFile != defaultConfigFile { err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile) if err != nil { - if _, ok := err.(*os.PathError); !ok { - fmt.Fprintf(os.Stderr, "Error parsing config "+ - "file: %v\n", err) + _, ok := err.(*os.PathError) + if !ok || preCfg.ConfigFile != defaultConfigFile { + fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n", + err) fmt.Fprintln(os.Stderr, usageMessage) return nil, nil, err } - configFileError = err } } @@ -868,13 +867,6 @@ func loadConfig() (*config, []string, error) { } } - // Warn about missing config file only after all other configuration is - // done. This prevents the warning on help messages and invalid - // options. Note this should go directly before the return. - if configFileError != nil { - dcrdLog.Warnf("%v", configFileError) - } - return &cfg, remainingArgs, nil }