diff --git a/cmd/commands/version/version.go b/cmd/commands/version/version.go index 6470d84..b57511e 100644 --- a/cmd/commands/version/version.go +++ b/cmd/commands/version/version.go @@ -1,16 +1,17 @@ package version import ( + "bufio" "bytes" "encoding/json" "flag" "fmt" - "os" - "os/exec" - "runtime" - "strings" - "gopkg.in/yaml.v2" + "io" + "os" + path "path/filepath" + "regexp" + "runtime" "github.com/beego/bee/v2/cmd/commands" "github.com/beego/bee/v2/config" @@ -26,6 +27,7 @@ const verboseVersionBanner string = `%s%s______ | |_/ /| __/| __/ \____/ \___| \___| v{{ .BeeVersion }}%s %s%s +├── Beego : {{ .BeegoVersion }} ├── GoVersion : {{ .GoVersion }} ├── GOOS : {{ .GOOS }} ├── GOARCH : {{ .GOARCH }} @@ -33,7 +35,7 @@ const verboseVersionBanner string = `%s%s______ ├── GOPATH : {{ .GOPATH }} ├── GOROOT : {{ .GOROOT }} ├── Compiler : {{ .Compiler }} -└── Published : {{ .Published }}%s +└── Date : {{ Now "Monday, 2 Jan 2006" }}%s ` const shortVersionBanner = `______ @@ -78,7 +80,7 @@ func versionCmd(cmd *commands.Command, args []string) int { runtime.GOROOT(), runtime.Compiler, version, - utils.GetLastPublishedTime(), + GetBeegoVersion(), } switch outputFormat { case "json": @@ -114,14 +116,52 @@ func ShowShortVersionBanner() { InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner))) } -func GetGoVersion() string { - var ( - cmdOut []byte - err error - ) - - if cmdOut, err = exec.Command("go", "version").Output(); err != nil { - beeLogger.Log.Fatalf("There was an error running 'go version' command: %s", err) +func GetBeegoVersion() string { + re, err := regexp.Compile(`VERSION = "([0-9.]+)"`) + if err != nil { + return "" } - 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() }