From 678576d2ce3b14f7b6b59692669b8acbd7c427ff Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Tue, 20 Feb 2018 11:24:05 -0800 Subject: [PATCH] config: Return handle for created default config (#1130) Previously the file handle for the newly created default config was being closed and thrown away as opposed to returned to the caller to finish setting up config for the rest of the process. This patch changes to return a handle to the newly created config so setup can happen as normal. This fixes a bug where Delve can crash on first run when a config is not present on the system. Fixes #1129 --- pkg/config/config.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 1345aad9..5cc6dad4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -60,8 +60,11 @@ func LoadConfig() *Config { f, err := os.Open(fullConfigFile) if err != nil { - createDefaultConfig(fullConfigFile) - return nil + f, err = createDefaultConfig(fullConfigFile) + if err != nil { + fmt.Printf("Error creating default config file: %v", err) + return nil + } } defer func() { err := f.Close() @@ -107,22 +110,16 @@ func SaveConfig(conf *Config) error { return err } -func createDefaultConfig(path string) { +func createDefaultConfig(path string) (*os.File, error) { f, err := os.Create(path) if err != nil { - fmt.Printf("Unable to create config file: %v.", err) - return + return nil, fmt.Errorf("Unable to create config file: %v.", err) } - defer func() { - err := f.Close() - if err != nil { - fmt.Printf("Closing config file failed: %v.", err) - } - }() err = writeDefaultConfig(f) if err != nil { - fmt.Printf("Unable to write default configuration: %v.", err) + return nil, fmt.Errorf("Unable to write default configuration: %v.", err) } + return f, nil } func writeDefaultConfig(f *os.File) error {