1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 05:00:54 +00:00

Merge pull request #343 from amrfaissal/set-version-output-format

Set the output format for bee version
This commit is contained in:
astaxie 2016-12-25 13:51:22 +08:00 committed by GitHub
commit d801cc2469
3 changed files with 56 additions and 19 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
_obj
_test
.idea
.vscode
# Architecture specific extensions/prefixes
*.[568vq]

View File

@ -8,7 +8,8 @@ import (
"text/template"
)
type vars struct {
// RuntimeInfo holds information about the current runtime.
type RuntimeInfo struct {
GoVersion string
GOOS string
GOARCH string
@ -45,7 +46,7 @@ func show(out io.Writer, content string) {
logger.Fatalf("Cannot parse the banner template: %s", err)
}
err = t.Execute(out, vars{
err = t.Execute(out, RuntimeInfo{
getGoVersion(),
runtime.GOOS,
runtime.GOARCH,

View File

@ -3,13 +3,18 @@ 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{
@ -17,8 +22,8 @@ var cmdVersion = &Command{
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______
@ -39,36 +44,66 @@ const verboseVersionBanner string = `%s%s______
Date : {{ Now "Monday, 2 Jan 2006" }}%s
`
const shortVersionBanner = `%s%s______
const shortVersionBanner = `______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}%s
\____/ \___| \___| v{{ .BeeVersion }}
`
var outputFormat string
func init() {
cmdVersion.Run = versionCmd
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 {
ShowVerboseVersionBanner()
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
}
// ShowVerboseVersionBanner prints the verbose version banner
func ShowVerboseVersionBanner() {
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))
}
// ShowShortVersionBanner prints the short version banner
// ShowShortVersionBanner prints the short version banner.
func ShowShortVersionBanner() {
w := NewColorWriter(os.Stdout)
coloredBanner := fmt.Sprintf(shortVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m")
InitBanner(w, bytes.NewBufferString(coloredBanner))
output := NewColorWriter(os.Stdout)
InitBanner(output, bytes.NewBufferString(MagentaBold(shortVersionBanner)))
}
func getBeegoVersion() string {