1
0
mirror of https://github.com/beego/bee.git synced 2025-07-03 05:00:19 +00:00

beego pro init

This commit is contained in:
yitea
2020-07-04 22:58:03 +08:00
parent bb5e0435c9
commit 8758f6eaa1
17 changed files with 1652 additions and 0 deletions

@ -0,0 +1,65 @@
package command
import (
"bytes"
"fmt"
"os/exec"
"strconv"
"strings"
)
// ExecCmdDirBytes executes system command in given directory
// and return stdout, stderr in bytes type, along with possible error.
func ExecCmdDirBytes(dir, cmdName string, args ...string) ([]byte, []byte, error) {
bufOut := new(bytes.Buffer)
bufErr := new(bytes.Buffer)
cmd := exec.Command(cmdName, args...)
cmd.Dir = dir
cmd.Stdout = bufOut
cmd.Stderr = bufErr
err := cmd.Run()
return bufOut.Bytes(), bufErr.Bytes(), err
}
// ExecCmdBytes executes system command
// and return stdout, stderr in bytes type, along with possible error.
func ExecCmdBytes(cmdName string, args ...string) ([]byte, []byte, error) {
return ExecCmdDirBytes("", cmdName, args...)
}
// ExecCmdDir executes system command in given directory
// and return stdout, stderr in string type, along with possible error.
func ExecCmdDir(dir, cmdName string, args ...string) (string, string, error) {
bufOut, bufErr, err := ExecCmdDirBytes(dir, cmdName, args...)
return string(bufOut), string(bufErr), err
}
// ExecCmd executes system command
// and return stdout, stderr in string type, along with possible error.
func ExecCmd(cmdName string, args ...string) (string, string, error) {
return ExecCmdDir("", cmdName, args...)
}
// 版本对比 v1比v2大返回1小于返回-1等于返回0
func VerCompare(ver1, ver2 string) int {
ver1 = strings.TrimLeft(ver1, "ver") // 清除v,e,r
ver2 = strings.TrimLeft(ver2, "ver") // 清除v,e,r
p1 := strings.Split(ver1, ".")
p2 := strings.Split(ver2, ".")
ver1 = ""
for _, v := range p1 {
iv, _ := strconv.Atoi(v)
ver1 = fmt.Sprintf("%s%04d", ver1, iv)
}
ver2 = ""
for _, v := range p2 {
iv, _ := strconv.Atoi(v)
ver2 = fmt.Sprintf("%s%04d", ver2, iv)
}
return strings.Compare(ver1, ver2)
}

@ -0,0 +1,231 @@
package git
import (
"errors"
"fmt"
"github.com/beego/bee/internal/pkg/command"
"github.com/beego/bee/internal/pkg/utils"
beeLogger "github.com/beego/bee/logger"
"path/filepath"
"sort"
"strconv"
"strings"
)
// 获取某项目代码库的标签列表
func GetTags(repoPath string, limit int) ([]string, error) {
repo, err := OpenRepository(repoPath)
if err != nil {
return nil, err
}
err = repo.Pull()
if err != nil {
return nil, err
}
list, err := repo.GetTags()
if err != nil {
return nil, err
}
if len(list) > limit {
list = list[0:limit]
}
return list, nil
}
// clone repo
func CloneRepo(url string, dst string) (err error) {
if utils.IsExist(dst) {
return errors.New("dst is not empty, dst is " + dst)
}
if !utils.Mkdir(dst) {
err = errors.New("make dir error, dst is " + dst)
return
}
beeLogger.Log.Info("start git clone from " + url + ", to dst at " + dst)
_, stderr, err := command.ExecCmd("git", "clone", url, dst)
if err != nil {
beeLogger.Log.Error("error git clone from " + url + ", to dst at " + dst)
return concatenateError(err, stderr)
}
return nil
}
// CloneORPullRepo
func CloneORPullRepo(url string, dst string) error {
if !utils.IsDir(dst) {
return CloneRepo(url, dst)
} else {
//projectName, err := getGitProjectName(url)
//if err != nil {
// return err
//}
//fmt.Println("dst------>", dst)
//projectDir := dst + "/" + projectName
utils.Mkdir(dst)
repo, err := OpenRepository(dst)
if err != nil {
return err
}
return repo.Pull()
}
}
//
func CloneRepoBranch(branch string, url string, dst string) error {
_, stderr, err := command.ExecCmd("git", "clone", "-b", branch, url, dst)
if err != nil {
return concatenateError(err, stderr)
}
return nil
}
// SortTag ...
type SortTag struct {
data []string
}
// Len ...
func (t *SortTag) Len() int {
return len(t.data)
}
// Swap ...
func (t *SortTag) Swap(i, j int) {
t.data[i], t.data[j] = t.data[j], t.data[i]
}
// Less ...
func (t *SortTag) Less(i, j int) bool {
return command.VerCompare(t.data[i], t.data[j]) == 1
}
// Sort ...
func (t *SortTag) Sort() []string {
sort.Sort(t)
return t.data
}
// Repository ...
type Repository struct {
Path string
}
// OpenRepository ...
func OpenRepository(repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
} else if !utils.IsDir(repoPath) {
return nil, errors.New("no such file or directory")
}
return &Repository{Path: repoPath}, nil
}
// 拉取代码
func (repo *Repository) Pull() error {
beeLogger.Log.Info("git pull " + repo.Path)
_, stderr, err := command.ExecCmdDir(repo.Path, "git", "pull")
if err != nil {
return concatenateError(err, stderr)
}
return nil
}
// 获取tag列表
func (repo *Repository) GetTags() ([]string, error) {
stdout, stderr, err := command.ExecCmdDir(repo.Path, "git", "tag", "-l")
if err != nil {
return nil, concatenateError(err, stderr)
}
tags := strings.Split(stdout, "\n")
tags = tags[:len(tags)-1]
so := &SortTag{data: tags}
return so.Sort(), nil
}
// 获取两个版本之间的修改日志
func (repo *Repository) GetChangeLogs(startVer, endVer string) ([]string, error) {
// git log --pretty=format:"%cd %cn: %s" --date=iso v1.8.0...v1.9.0
stdout, stderr, err := command.ExecCmdDir(repo.Path, "git", "log", "--pretty=format:%cd %cn: %s", "--date=iso", startVer+"..."+endVer)
if err != nil {
return nil, concatenateError(err, stderr)
}
logs := strings.Split(stdout, "\n")
return logs, nil
}
// 获取两个版本之间的差异文件列表
func (repo *Repository) GetChangeFiles(startVer, endVer string, onlyFile bool) ([]string, error) {
// git diff --name-status -b v1.8.0 v1.9.0
param := "--name-status"
if onlyFile {
param = "--name-only"
}
stdout, stderr, err := command.ExecCmdDir(repo.Path, "git", "diff", param, "-b", startVer, endVer)
if err != nil {
return nil, concatenateError(err, stderr)
}
lines := strings.Split(stdout, "\n")
return lines[:len(lines)-1], nil
}
// 获取两个版本间的新增或修改的文件数量
func (repo *Repository) GetDiffFileCount(startVer, endVer string) (int, error) {
cmd := "git diff --name-status -b " + startVer + " " + endVer + " |grep -v ^D |wc -l"
stdout, stderr, err := command.ExecCmdDir(repo.Path, "/bin/bash", "-c", cmd)
if err != nil {
return 0, concatenateError(err, stderr)
}
count, _ := strconv.Atoi(strings.TrimSpace(stdout))
return count, nil
}
// 导出版本到tar包
func (repo *Repository) Export(startVer, endVer string, filename string) error {
// git archive --format=tar.gz $endVer $(git diff --name-status -b $beginVer $endVer |grep -v ^D |grep -v Upgrade/ |awk '{print $2}') -o $tmpFile
cmd := ""
if startVer == "" {
cmd = "git archive --format=tar " + endVer + " | gzip > " + filename
} else {
cmd = "git archive --format=tar " + endVer + " $(dgit diff --name-status -b " + startVer + " " + endVer + "|grep -v ^D |awk '{print $2}') | gzip > " + filename
}
_, stderr, err := command.ExecCmdDir(repo.Path, "/bin/bash", "-c", cmd)
if err != nil {
return concatenateError(err, stderr)
}
return nil
}
func concatenateError(err error, stderr string) error {
if len(stderr) == 0 {
return err
}
return fmt.Errorf("%v: %s", err, stderr)
}
// getGitProjectName 获取项目名称
func getGitProjectName(url string) (name string, err error) {
if !strings.Contains(url, ".git") {
return "", errors.New("Project address does not contain .git")
}
fileSlice := strings.Split(url, "/")
projectName := fileSlice[len(fileSlice)-1]
if projectName == "" {
return "", errors.New("Project name does not exist")
}
nameSlice := strings.Split(projectName, ".git")
return nameSlice[0], nil
}

@ -0,0 +1,22 @@
package system
import (
"os"
"os/user"
"path/filepath"
)
// Bee System Params ...
var (
Usr, _ = user.Current()
BeegoHome = filepath.Join(Usr.HomeDir, "/.beego")
CurrentDir = getCurrentDirectory()
GoPath = os.Getenv("GOPATH")
)
func getCurrentDirectory() string {
if dir, err := os.Getwd(); err == nil {
return dir
}
return ""
}

@ -0,0 +1,37 @@
package utils
import (
beeLogger "github.com/beego/bee/logger"
"os"
)
// Mkdir ...
func Mkdir(dir string) bool {
if dir == "" {
beeLogger.Log.Fatalf("The directory is empty")
return false
}
err := os.MkdirAll(dir, 0755)
if err != nil {
beeLogger.Log.Fatalf("Could not create the directory: %s", err)
return false
}
beeLogger.Log.Infof("Create %s Success!", dir)
return true
}
// IsDir ...
func IsDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}
// IsExist returns whether a file or directory exists.
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}