-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use '~/.config/prm' instead of '~/.prm'
- Loading branch information
Showing
5 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// +build !windows | ||
|
||
package config | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
defaultFileName = ".config/prm" | ||
oldDefaultFileName = ".prm" | ||
) | ||
|
||
// GetPath get the configuration file path. | ||
func GetPath() (string, error) { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
pathOne := filepath.Join(usr.HomeDir, oldDefaultFileName) | ||
pathTwo := filepath.Join(usr.HomeDir, defaultFileName) | ||
|
||
info, err := os.Stat(pathOne) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return pathTwo, nil | ||
} | ||
return "", err | ||
} | ||
|
||
log.Println("WARN: old configuration file detected, migration in progress.") | ||
|
||
err = copyConfigFile(pathOne, pathTwo, info) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = os.Remove(pathOne) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
log.Println("WARN: old configuration file detected, migration done.") | ||
|
||
return pathTwo, nil | ||
} | ||
|
||
func copyConfigFile(src, dst string, info os.FileInfo) error { | ||
baseDir := path.Dir(dst) | ||
err := os.MkdirAll(baseDir, 0700) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
defer safeClose(f.Close) | ||
|
||
if err = os.Chmod(f.Name(), info.Mode()); err != nil { | ||
return err | ||
} | ||
|
||
s, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer safeClose(s.Close) | ||
|
||
_, err = io.Copy(f, s) | ||
return err | ||
} | ||
|
||
func safeClose(fn func() error) { | ||
err := fn() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// +build windows | ||
|
||
package config | ||
|
||
import ( | ||
"os/user" | ||
"path/filepath" | ||
) | ||
|
||
const defaultFileName = ".prm" | ||
|
||
// GetPath get the configuration file path. | ||
func GetPath() (string, error) { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(usr.HomeDir, defaultFileName), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters