-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加方法:Sha256、Sha512、HomePath、CreateMultiDir
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package golibs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// HomePath 获取当前用户根目录 | ||
func HomePath() (string, error) { | ||
user, err := user.Current() | ||
if nil == err { | ||
return user.HomeDir, nil | ||
} | ||
|
||
// cross compile support | ||
if "windows" == runtime.GOOS { | ||
return homeWindows() | ||
} | ||
|
||
// Unix-like system, so just assume Unix | ||
return homeUnix() | ||
} | ||
|
||
func homeUnix() (string, error) { | ||
// First prefer the HOME environmental variable | ||
if home := os.Getenv("HOME"); home != "" { | ||
return home, nil | ||
} | ||
// If that fails, try the shell | ||
var stdout bytes.Buffer | ||
cmd := exec.Command("sh", "-c", "eval echo ~$USER") | ||
cmd.Stdout = &stdout | ||
if err := cmd.Run(); err != nil { | ||
return "", err | ||
} | ||
result := strings.TrimSpace(stdout.String()) | ||
if result == "" { | ||
return "", fmt.Errorf("blank output when reading home directory") | ||
} | ||
return result, nil | ||
} | ||
|
||
func homeWindows() (string, error) { | ||
drive := os.Getenv("HOMEDRIVE") | ||
path := os.Getenv("HOMEPATH") | ||
home := drive + path | ||
if drive == "" || path == "" { | ||
home = os.Getenv("USERPROFILE") | ||
} | ||
if home == "" { | ||
return "", fmt.Errorf("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") | ||
} | ||
return home, nil | ||
} | ||
|
||
//CreateMultiDir 调用os.MkdirAll递归创建文件夹 | ||
func CreateMultiDir(filePath string) error { | ||
if pathIsExist(filePath) { | ||
return nil | ||
} | ||
return os.MkdirAll(filePath, os.ModePerm) | ||
} | ||
|
||
// 判断所给路径文件/文件夹是否存在(返回true是存在) | ||
func pathIsExist(path string) bool { | ||
_, err := os.Stat(path) | ||
if err != nil { | ||
if os.IsExist(err) { | ||
return true | ||
} | ||
return false | ||
} | ||
return true | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package golibs | ||
|
||
const VERSION = "2020.712.900" | ||
const VERSION = "2021.513.1742" |