From f979062a4f13d2750e227dec95be5ccc5bbb9dff Mon Sep 17 00:00:00 2001 From: Stefan Liu Date: Mon, 6 May 2019 16:53:32 +0800 Subject: [PATCH] Fix ssh config file auto-creation when missing --- main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5819a28..b76cf13 100644 --- a/main.go +++ b/main.go @@ -261,11 +261,19 @@ func (c *Config) Save() { configDir, err := filepath.Abs(os.Getenv("HOME") + "/.ssh/config") checkError(err) - _, err = os.Stat(configDir) - checkError(err) + // make sure dir exists + dir := filepath.Dir(configDir) + if _, err = os.Stat(dir); err != nil { + os.MkdirAll(dir, os.ModePerm) + } - err = os.Rename(configDir, configDir+string(time.Now().Format(time.RFC3339))) - checkError(err) + // backup old dir + if _, err = os.Stat(configDir); err == nil { + err = os.Rename(configDir, configDir+string(time.Now().Format(time.RFC3339))) + if err != nil { + log.Fatalf("Failed to rename old config file %v.\n", configDir) + } + } f, err := os.OpenFile(configDir, os.O_RDWR|os.O_CREATE, 0755) checkError(err)