From 059df76c3edf5cb93049d962350a55c9b0d82fc5 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sat, 24 Dec 2016 14:51:14 +0100 Subject: [PATCH] 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. --- .gitignore | 1 + banner.go | 5 ++-- version.go | 69 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index a4c4e18..ab1571f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ _obj _test .idea +.vscode # Architecture specific extensions/prefixes *.[568vq] diff --git a/banner.go b/banner.go index b9a4789..fb06316 100644 --- a/banner.go +++ b/banner.go @@ -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, diff --git a/version.go b/version.go index 721a885..6da9d1d 100644 --- a/version.go +++ b/version.go @@ -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 {