1
0
mirror of https://github.com/beego/bee.git synced 2024-12-23 01:30:50 +00:00
bee/version.go

131 lines
3.0 KiB
Go
Raw Normal View History

2014-05-16 17:37:45 +00:00
package main
import (
2014-05-22 02:35:37 +00:00
"bufio"
2016-07-31 21:23:07 +00:00
"bytes"
2014-05-16 17:37:45 +00:00
"fmt"
2014-05-22 02:35:37 +00:00
"io"
"os"
2016-10-22 13:44:22 +00:00
"os/exec"
2014-05-22 02:35:37 +00:00
path "path/filepath"
"regexp"
2016-10-22 13:44:22 +00:00
"strings"
2014-05-16 17:37:45 +00:00
)
var cmdVersion = &Command{
UsageLine: "version",
Short: "Prints the current Bee version",
2014-05-16 17:37:45 +00:00
Long: `
Prints the current Bee, Beego and Go version alongside the platform information.
2014-05-16 17:37:45 +00:00
`,
}
2016-07-31 21:23:07 +00:00
const verboseVersionBanner string = `%s%s______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
2016-07-31 21:23:07 +00:00
\____/ \___| \___| v{{ .BeeVersion }}%s
%s%s
Beego : {{ .BeegoVersion }}
GoVersion : {{ .GoVersion }}
GOOS : {{ .GOOS }}
GOARCH : {{ .GOARCH }}
NumCPU : {{ .NumCPU }}
GOPATH : {{ .GOPATH }}
GOROOT : {{ .GOROOT }}
Compiler : {{ .Compiler }}
Date : {{ Now "Monday, 2 Jan 2006" }}%s
`
2016-07-31 21:23:07 +00:00
const shortVersionBanner = `%s%s______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
2016-07-31 21:23:07 +00:00
\____/ \___| \___| v{{ .BeeVersion }}%s
`
2014-05-16 17:37:45 +00:00
func init() {
cmdVersion.Run = versionCmd
}
2014-08-15 09:38:51 +00:00
func versionCmd(cmd *Command, args []string) int {
ShowVerboseVersionBanner()
2014-08-15 09:38:51 +00:00
return 0
2014-05-22 02:35:37 +00:00
}
2016-07-31 21:23:07 +00:00
// ShowVerboseVersionBanner prints the verbose version banner
func ShowVerboseVersionBanner() {
2016-07-31 21:23:07 +00:00
w := NewColorWriter(os.Stdout)
coloredBanner := fmt.Sprintf(verboseVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m",
"\x1b[32m", "\x1b[1m", "\x1b[0m")
InitBanner(w, bytes.NewBufferString(coloredBanner))
}
2016-07-31 21:23:07 +00:00
// ShowShortVersionBanner prints the short version banner
func ShowShortVersionBanner() {
2016-07-31 21:23:07 +00:00
w := NewColorWriter(os.Stdout)
coloredBanner := fmt.Sprintf(shortVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m")
InitBanner(w, bytes.NewBufferString(coloredBanner))
}
func getBeegoVersion() string {
2014-05-22 02:35:37 +00:00
gopath := os.Getenv("GOPATH")
2016-01-18 02:27:17 +00:00
re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
2014-05-16 17:53:42 +00:00
if err != nil {
2014-05-22 02:35:37 +00:00
return ""
}
if gopath == "" {
err = fmt.Errorf("You need to set GOPATH environment variable")
2014-05-22 02:35:37 +00:00
return ""
}
wgopath := path.SplitList(gopath)
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
}
logger.Error("Error while getting stats of 'beego.go'")
2014-05-22 02:35:37 +00:00
}
fd, err := os.Open(filename)
if err != nil {
logger.Error("Error while reading 'beego.go'")
2014-05-22 02:35:37 +00:00
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]
}
}
2014-05-16 17:53:42 +00:00
}
return "Beego is not installed. Please do consider installing it first: https://github.com/astaxie/beego"
2014-05-16 17:37:45 +00:00
}
2016-10-22 13:44:22 +00:00
func getGoVersion() string {
var (
cmdOut []byte
err error
)
if cmdOut, err = exec.Command("go", "version").Output(); err != nil {
logger.Fatalf("There was an error running 'go version' command: %s", err)
2016-10-22 13:44:22 +00:00
}
return strings.Split(string(cmdOut), " ")[2]
}