2017-03-06 23:58:53 +00:00
|
|
|
package version
|
2014-05-16 17:37:45 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-22 02:35:37 +00:00
|
|
|
"bufio"
|
2016-07-31 21:23:07 +00:00
|
|
|
"bytes"
|
2016-12-24 13:51:14 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
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-12-24 13:51:14 +00:00
|
|
|
"runtime"
|
2016-10-22 13:44:22 +00:00
|
|
|
"strings"
|
2016-12-24 13:51:14 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
"github.com/beego/bee/cmd/commands"
|
|
|
|
beeLogger "github.com/beego/bee/logger"
|
|
|
|
"github.com/beego/bee/logger/colors"
|
2017-04-24 12:51:56 +00:00
|
|
|
"github.com/beego/bee/utils"
|
2016-12-24 13:51:14 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-05-16 17:37:45 +00:00
|
|
|
)
|
|
|
|
|
2016-07-31 21:23:07 +00:00
|
|
|
const verboseVersionBanner string = `%s%s______
|
2016-06-01 12:30:29 +00:00
|
|
|
| ___ \
|
|
|
|
| |_/ / ___ ___
|
|
|
|
| ___ \ / _ \ / _ \
|
|
|
|
| |_/ /| __/| __/
|
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-06-01 12:30:29 +00:00
|
|
|
`
|
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
const shortVersionBanner = `______
|
2016-06-01 12:30:29 +00:00
|
|
|
| ___ \
|
|
|
|
| |_/ / ___ ___
|
|
|
|
| ___ \ / _ \ / _ \
|
|
|
|
| |_/ /| __/| __/
|
2016-12-24 13:51:14 +00:00
|
|
|
\____/ \___| \___| v{{ .BeeVersion }}
|
2016-06-01 12:30:29 +00:00
|
|
|
`
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var CmdVersion = &commands.Command{
|
|
|
|
UsageLine: "version",
|
|
|
|
Short: "Prints the current Bee version",
|
|
|
|
Long: `
|
|
|
|
Prints the current Bee, Beego and Go version alongside the platform information.
|
|
|
|
`,
|
|
|
|
Run: versionCmd,
|
|
|
|
}
|
2016-12-24 13:51:14 +00:00
|
|
|
var outputFormat string
|
|
|
|
|
2020-07-27 05:11:14 +00:00
|
|
|
const version = "1.11.0"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
2014-05-16 17:37:45 +00:00
|
|
|
func init() {
|
2016-12-24 13:51:14 +00:00
|
|
|
fs := flag.NewFlagSet("version", flag.ContinueOnError)
|
|
|
|
fs.StringVar(&outputFormat, "o", "", "Set the output format. Either json or yaml.")
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdVersion.Flag = *fs
|
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdVersion)
|
2014-05-16 17:37:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
func versionCmd(cmd *commands.Command, args []string) int {
|
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
cmd.Flag.Parse(args)
|
|
|
|
stdout := cmd.Out()
|
|
|
|
|
|
|
|
if outputFormat != "" {
|
|
|
|
runtimeInfo := RuntimeInfo{
|
2017-03-06 23:58:53 +00:00
|
|
|
GetGoVersion(),
|
2016-12-24 13:51:14 +00:00
|
|
|
runtime.GOOS,
|
|
|
|
runtime.GOARCH,
|
|
|
|
runtime.NumCPU(),
|
|
|
|
os.Getenv("GOPATH"),
|
|
|
|
runtime.GOROOT(),
|
|
|
|
runtime.Compiler,
|
|
|
|
version,
|
2017-03-06 23:58:53 +00:00
|
|
|
GetBeegoVersion(),
|
2016-12-24 13:51:14 +00:00
|
|
|
}
|
|
|
|
switch outputFormat {
|
|
|
|
case "json":
|
|
|
|
{
|
|
|
|
b, err := json.MarshalIndent(runtimeInfo, "", " ")
|
2017-03-06 23:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Error(err.Error())
|
|
|
|
}
|
2016-12-24 13:51:14 +00:00
|
|
|
fmt.Println(string(b))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
case "yaml":
|
|
|
|
{
|
|
|
|
b, err := yaml.Marshal(&runtimeInfo)
|
2017-03-06 23:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Error(err.Error())
|
|
|
|
}
|
2016-12-24 13:51:14 +00:00
|
|
|
fmt.Println(string(b))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 02:35:37 +00:00
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
coloredBanner := fmt.Sprintf(verboseVersionBanner, "\x1b[35m", "\x1b[1m",
|
|
|
|
"\x1b[0m", "\x1b[32m", "\x1b[1m", "\x1b[0m")
|
|
|
|
InitBanner(stdout, bytes.NewBufferString(coloredBanner))
|
|
|
|
return 0
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
// ShowShortVersionBanner prints the short version banner.
|
2016-06-01 12:30:29 +00:00
|
|
|
func ShowShortVersionBanner() {
|
2017-03-06 23:58:53 +00:00
|
|
|
output := colors.NewColorWriter(os.Stdout)
|
|
|
|
InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner)))
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
func GetBeegoVersion() string {
|
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 ""
|
|
|
|
}
|
2017-04-24 12:51:56 +00:00
|
|
|
wgopath := utils.GetGOPATHs()
|
|
|
|
if len(wgopath) == 0 {
|
2020-06-25 14:29:27 +00:00
|
|
|
beeLogger.Log.Error("GOPATH environment is empty,may be you use `go module`")
|
2014-05-22 02:35:37 +00:00
|
|
|
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
|
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Error("Error while getting stats of 'beego.go'")
|
2014-05-22 02:35:37 +00:00
|
|
|
}
|
|
|
|
fd, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.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
|
|
|
}
|
2016-11-13 14:14:48 +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
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
func GetGoVersion() string {
|
2016-10-22 13:44:22 +00:00
|
|
|
var (
|
|
|
|
cmdOut []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if cmdOut, err = exec.Command("go", "version").Output(); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.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]
|
|
|
|
}
|