Skip to content

Commit

Permalink
Fix ssh config file auto-creation when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
devfans committed May 6, 2019
1 parent b257a57 commit f979062
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f979062

Please sign in to comment.