1
0
mirror of https://github.com/beego/bee.git synced 2024-11-13 04:50:54 +00:00
bee/version.go
Faissal Elamraoui 059df76c3e Set the output format for bee version
This adds the ability to set the output format (using -o flag) of bee
version command. It supports both JSON and YAML formats.
2016-12-24 14:51:14 +01:00

166 lines
3.6 KiB
Go

package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"os/exec"
path "path/filepath"
"regexp"
"runtime"
"strings"
"gopkg.in/yaml.v2"
)
var cmdVersion = &Command{
UsageLine: "version",
Short: "Prints the current Bee version",
Long: `
Prints the current Bee, Beego and Go version alongside the platform information.
`,
Run: versionCmd,
}
const verboseVersionBanner string = `%s%s______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| 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
`
const shortVersionBanner = `______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}
`
var outputFormat string
func init() {
fs := flag.NewFlagSet("version", flag.ContinueOnError)
fs.StringVar(&outputFormat, "o", "", "Set the output format. Either json or yaml.")
cmdVersion.Flag = *fs
}
func versionCmd(cmd *Command, args []string) int {
cmd.Flag.Parse(args)
stdout := cmd.Out()
if outputFormat != "" {
runtimeInfo := RuntimeInfo{
getGoVersion(),
runtime.GOOS,
runtime.GOARCH,
runtime.NumCPU(),
os.Getenv("GOPATH"),
runtime.GOROOT(),
runtime.Compiler,
version,
getBeegoVersion(),
}
switch outputFormat {
case "json":
{
b, err := json.MarshalIndent(runtimeInfo, "", " ")
MustCheck(err)
fmt.Println(string(b))
return 0
}
case "yaml":
{
b, err := yaml.Marshal(&runtimeInfo)
MustCheck(err)
fmt.Println(string(b))
return 0
}
}
}
coloredBanner := fmt.Sprintf(verboseVersionBanner, "\x1b[35m", "\x1b[1m",
"\x1b[0m", "\x1b[32m", "\x1b[1m", "\x1b[0m")
InitBanner(stdout, bytes.NewBufferString(coloredBanner))
return 0
}
// ShowShortVersionBanner prints the short version banner.
func ShowShortVersionBanner() {
output := NewColorWriter(os.Stdout)
InitBanner(output, bytes.NewBufferString(MagentaBold(shortVersionBanner)))
}
func getBeegoVersion() string {
gopath := os.Getenv("GOPATH")
re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
if err != nil {
return ""
}
if gopath == "" {
err = fmt.Errorf("You need to set GOPATH environment variable")
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'")
}
fd, err := os.Open(filename)
if err != nil {
logger.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/astaxie/beego"
}
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)
}
return strings.Split(string(cmdOut), " ")[2]
}