This commit is contained in:
Ming Deng 2021-03-29 23:11:14 +08:00 committed by Deng Ming
parent d7fcaf0921
commit d04f11c57a
1 changed files with 56 additions and 16 deletions

View File

@ -1,16 +1,17 @@
package version package version
import ( import (
"bufio"
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"os"
"os/exec"
"runtime"
"strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io"
"os"
path "path/filepath"
"regexp"
"runtime"
"github.com/beego/bee/v2/cmd/commands" "github.com/beego/bee/v2/cmd/commands"
"github.com/beego/bee/v2/config" "github.com/beego/bee/v2/config"
@ -26,6 +27,7 @@ const verboseVersionBanner string = `%s%s______
| |_/ /| __/| __/ | |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}%s \____/ \___| \___| v{{ .BeeVersion }}%s
%s%s %s%s
Beego : {{ .BeegoVersion }}
GoVersion : {{ .GoVersion }} GoVersion : {{ .GoVersion }}
GOOS : {{ .GOOS }} GOOS : {{ .GOOS }}
GOARCH : {{ .GOARCH }} GOARCH : {{ .GOARCH }}
@ -33,7 +35,7 @@ const verboseVersionBanner string = `%s%s______
GOPATH : {{ .GOPATH }} GOPATH : {{ .GOPATH }}
GOROOT : {{ .GOROOT }} GOROOT : {{ .GOROOT }}
Compiler : {{ .Compiler }} Compiler : {{ .Compiler }}
Published : {{ .Published }}%s Date : {{ Now "Monday, 2 Jan 2006" }}%s
` `
const shortVersionBanner = `______ const shortVersionBanner = `______
@ -78,7 +80,7 @@ func versionCmd(cmd *commands.Command, args []string) int {
runtime.GOROOT(), runtime.GOROOT(),
runtime.Compiler, runtime.Compiler,
version, version,
utils.GetLastPublishedTime(), GetBeegoVersion(),
} }
switch outputFormat { switch outputFormat {
case "json": case "json":
@ -114,14 +116,52 @@ func ShowShortVersionBanner() {
InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner))) InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner)))
} }
func GetGoVersion() string { func GetBeegoVersion() string {
var ( re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
cmdOut []byte if err != nil {
err error return ""
)
if cmdOut, err = exec.Command("go", "version").Output(); err != nil {
beeLogger.Log.Fatalf("There was an error running 'go version' command: %s", err)
} }
return strings.Split(string(cmdOut), " ")[2] wgopath := utils.GetGOPATHs()
if len(wgopath) == 0 {
beeLogger.Log.Error("GOPATH environment is empty,may be you use `go module`")
return ""
}
for _, wg := range wgopath {
wg, _ = path.EvalSymlinks(path.Join(wg, "src", "github.com", "astaxie", "beego"))
filename := path.Join(wg, "beego.go")
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
continue
}
beeLogger.Log.Error("Error while getting stats of 'beego.go'")
}
fd, err := os.Open(filename)
if err != nil {
beeLogger.Log.Error("Error while reading 'beego.go'")
continue
}
reader := bufio.NewReader(fd)
for {
byteLine, _, er := reader.ReadLine()
if er != nil && er != io.EOF {
return ""
}
if er == io.EOF {
break
}
line := string(byteLine)
s := re.FindStringSubmatch(line)
if len(s) >= 2 {
return s[1]
}
}
}
return "Beego is not installed. Please do consider installing it first: https://github.com/beego/beego/v2. " +
"If you are using go mod, and you don't install the beego under $GOPATH/src/github.com/astaxie, just ignore this."
}
func GetGoVersion() string {
return runtime.Version()
} }