1
0
mirror of https://github.com/beego/bee.git synced 2024-11-24 03:20:55 +00:00

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.
This commit is contained in:
Faissal Elamraoui 2016-12-24 14:51:14 +01:00
parent d8b9d96d84
commit 059df76c3e
3 changed files with 56 additions and 19 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -3,13 +3,18 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json"
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
path "path/filepath" path "path/filepath"
"regexp" "regexp"
"runtime"
"strings" "strings"
"gopkg.in/yaml.v2"
) )
var cmdVersion = &Command{ var cmdVersion = &Command{
@ -17,8 +22,8 @@ var cmdVersion = &Command{
Short: "Prints the current Bee version", Short: "Prints the current Bee version",
Long: ` Long: `
Prints the current Bee, Beego and Go version alongside the platform information. Prints the current Bee, Beego and Go version alongside the platform information.
`, `,
Run: versionCmd,
} }
const verboseVersionBanner string = `%s%s______ const verboseVersionBanner string = `%s%s______
@ -39,36 +44,66 @@ const verboseVersionBanner string = `%s%s______
Date : {{ Now "Monday, 2 Jan 2006" }}%s Date : {{ Now "Monday, 2 Jan 2006" }}%s
` `
const shortVersionBanner = `%s%s______ const shortVersionBanner = `______
| ___ \ | ___ \
| |_/ / ___ ___ | |_/ / ___ ___
| ___ \ / _ \ / _ \ | ___ \ / _ \ / _ \
| |_/ /| __/| __/ | |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}%s \____/ \___| \___| v{{ .BeeVersion }}
` `
var outputFormat string
func init() { 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 { 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 return 0
} }
// ShowVerboseVersionBanner prints the verbose version banner // ShowShortVersionBanner prints the short 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
func ShowShortVersionBanner() { func ShowShortVersionBanner() {
w := NewColorWriter(os.Stdout) output := NewColorWriter(os.Stdout)
coloredBanner := fmt.Sprintf(shortVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m") InitBanner(output, bytes.NewBufferString(MagentaBold(shortVersionBanner)))
InitBanner(w, bytes.NewBufferString(coloredBanner))
} }
func getBeegoVersion() string { func getBeegoVersion() string {