Init v3.2
This commit is contained in:
15
utils/cmd.go
Normal file
15
utils/cmd.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func RunCmdByDir(dir string, cmdName string, arg ...string) (string, error) {
|
||||
cmd := exec.Command(cmdName, arg ...)
|
||||
cmd.Dir = dir
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
13
utils/cmd_test.go
Normal file
13
utils/cmd_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"blog/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunCmdByDir(t *testing.T) {
|
||||
_, err := utils.RunCmdByDir("./", "ping", "127.0.0.1")
|
||||
if err != nil {
|
||||
t.Error("run cmd error", err)
|
||||
}
|
||||
}
|
65
utils/file.go
Normal file
65
utils/file.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func IsDir(name string) bool {
|
||||
if info, err := os.Stat(name); err == nil {
|
||||
return info.IsDir()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
func IsFile(filename string) bool {
|
||||
existed := true
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
existed = false
|
||||
}
|
||||
return existed
|
||||
}
|
||||
|
||||
func MakeDir(dir string) error {
|
||||
if !IsDir(dir) {
|
||||
return os.MkdirAll(dir, os.ModePerm)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveDir(dir string) error {
|
||||
|
||||
if !IsDir(dir) {
|
||||
return errors.New("cannot delete without directory")
|
||||
}
|
||||
|
||||
return os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
func CopyFile(src, dst string) (int64, error) {
|
||||
sourceFileStat, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if !sourceFileStat.Mode().IsRegular() {
|
||||
return 0, fmt.Errorf("%s is not a regular file", src)
|
||||
}
|
||||
|
||||
source, err := os.Open(src)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
destination, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer destination.Close()
|
||||
nBytes, err := io.Copy(destination, source)
|
||||
return nBytes, err
|
||||
}
|
19
utils/git.go
Normal file
19
utils/git.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//通过git url 返回仓库的名字
|
||||
func GetRepoName(gitUrl string) (string, error) {
|
||||
|
||||
if !strings.HasSuffix(gitUrl, ".git") {
|
||||
return "", errors.New("git URL must end with .git!")
|
||||
}
|
||||
|
||||
noSuffixUrl := strings.TrimSuffix(gitUrl, ".git")
|
||||
urlArr := strings.Split(noSuffixUrl, "/")
|
||||
|
||||
return urlArr[ len(urlArr)-1 ], nil
|
||||
}
|
14
utils/git_test.go
Normal file
14
utils/git_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"blog/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetRepoName(t *testing.T) {
|
||||
url := "http://jsit205.vaiwan.cn/JiXieShi/blog.git"
|
||||
name, err := utils.GetRepoName(url)
|
||||
if err != nil || name != "blog" {
|
||||
t.Error("repository name error")
|
||||
}
|
||||
}
|
43
utils/short_url.go
Normal file
43
utils/short_url.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const charset = "A0a12B3b4CDc56Ede7FGf8Hg9IhJKiLjkMNlmOPnQRopqrSstTuvUVwxWXyYzZ"
|
||||
|
||||
func generateCharset(url, hexMd5 string, len, sectionNum int, cb func(url, keyword string) bool) string {
|
||||
for i := 0; i < sectionNum; i++ {
|
||||
sectionHex := hexMd5[i*8 : 8+i*8]
|
||||
bits, _ := strconv.ParseUint(sectionHex, 16, 32)
|
||||
bits = bits & 0x3FFFFFFF
|
||||
keyword := ""
|
||||
for j := 0; j < len; j++ {
|
||||
idx := bits & 0x3D
|
||||
keyword = keyword + string(charset[idx])
|
||||
bits = bits >> 5
|
||||
}
|
||||
if cb(url, keyword) {
|
||||
return keyword
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 起初生成6位的短码,当四组6位短码都重复时,再生成8位的短码,因此总共会有8个短码供你选择。
|
||||
|
||||
func GenerateShortUrl(url string, cb func(url, keyword string) bool) string {
|
||||
if url == "" || cb == nil {
|
||||
return ""
|
||||
}
|
||||
hexMd5 := fmt.Sprintf("%x", md5.Sum([]byte(url)))
|
||||
sections := len(hexMd5) / 8
|
||||
|
||||
keyword := generateCharset(url, hexMd5, 6, sections, cb)
|
||||
if keyword == "" {
|
||||
return generateCharset(url, hexMd5, 8, sections, cb)
|
||||
}
|
||||
return keyword
|
||||
}
|
14
utils/short_url_test.go
Normal file
14
utils/short_url_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"blog/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateShortUrl(t *testing.T) {
|
||||
url := "http://jsit205.vaiwan.cn/JiXieShi/blog.git"
|
||||
shortUrl := utils.GenerateShortUrl(url, func(url, keyword string) bool { return true })
|
||||
if shortUrl != "ItMIz7" {
|
||||
t.Error("generate short URL error")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user