Browse Source

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.
tags/v1.8.3
Faissal Elamraoui 2 years ago
parent
commit
059df76c3e
3 changed files with 56 additions and 19 deletions
  1. 1
    0
      .gitignore
  2. 3
    2
      banner.go
  3. 52
    17
      version.go

+ 1
- 0
.gitignore View File

@@ -7,6 +7,7 @@
7 7
 _obj
8 8
 _test
9 9
 .idea
10
+.vscode
10 11
 
11 12
 # Architecture specific extensions/prefixes
12 13
 *.[568vq]

+ 3
- 2
banner.go View File

@@ -8,7 +8,8 @@ import (
8 8
 	"text/template"
9 9
 )
10 10
 
11
-type vars struct {
11
+// RuntimeInfo holds information about the current runtime.
12
+type RuntimeInfo struct {
12 13
 	GoVersion    string
13 14
 	GOOS         string
14 15
 	GOARCH       string
@@ -45,7 +46,7 @@ func show(out io.Writer, content string) {
45 46
 		logger.Fatalf("Cannot parse the banner template: %s", err)
46 47
 	}
47 48
 
48
-	err = t.Execute(out, vars{
49
+	err = t.Execute(out, RuntimeInfo{
49 50
 		getGoVersion(),
50 51
 		runtime.GOOS,
51 52
 		runtime.GOARCH,

+ 52
- 17
version.go View File

@@ -3,13 +3,18 @@ package main
3 3
 import (
4 4
 	"bufio"
5 5
 	"bytes"
6
+	"encoding/json"
7
+	"flag"
6 8
 	"fmt"
7 9
 	"io"
8 10
 	"os"
9 11
 	"os/exec"
10 12
 	path "path/filepath"
11 13
 	"regexp"
14
+	"runtime"
12 15
 	"strings"
16
+
17
+	"gopkg.in/yaml.v2"
13 18
 )
14 19
 
15 20
 var cmdVersion = &Command{
@@ -17,8 +22,8 @@ var cmdVersion = &Command{
17 22
 	Short:     "Prints the current Bee version",
18 23
 	Long: `
19 24
 Prints the current Bee, Beego and Go version alongside the platform information.
20
-
21 25
 `,
26
+	Run: versionCmd,
22 27
 }
23 28
 
24 29
 const verboseVersionBanner string = `%s%s______
@@ -39,36 +44,66 @@ const verboseVersionBanner string = `%s%s______
39 44
 └── Date      : {{ Now "Monday, 2 Jan 2006" }}%s
40 45
 `
41 46
 
42
-const shortVersionBanner = `%s%s______
47
+const shortVersionBanner = `______
43 48
 | ___ \
44 49
 | |_/ /  ___   ___
45 50
 | ___ \ / _ \ / _ \
46 51
 | |_/ /|  __/|  __/
47
-\____/  \___| \___| v{{ .BeeVersion }}%s
52
+\____/  \___| \___| v{{ .BeeVersion }}
48 53
 `
49 54
 
55
+var outputFormat string
56
+
50 57
 func init() {
51
-	cmdVersion.Run = versionCmd
58
+	fs := flag.NewFlagSet("version", flag.ContinueOnError)
59
+	fs.StringVar(&outputFormat, "o", "", "Set the output format. Either json or yaml.")
60
+	cmdVersion.Flag = *fs
52 61
 }
53 62
 
54 63
 func versionCmd(cmd *Command, args []string) int {
55
-	ShowVerboseVersionBanner()
56
-	return 0
57
-}
64
+	cmd.Flag.Parse(args)
65
+	stdout := cmd.Out()
58 66
 
59
-// ShowVerboseVersionBanner prints the verbose version banner
60
-func ShowVerboseVersionBanner() {
61
-	w := NewColorWriter(os.Stdout)
62
-	coloredBanner := fmt.Sprintf(verboseVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m",
63
-		"\x1b[32m", "\x1b[1m", "\x1b[0m")
64
-	InitBanner(w, bytes.NewBufferString(coloredBanner))
67
+	if outputFormat != "" {
68
+		runtimeInfo := RuntimeInfo{
69
+			getGoVersion(),
70
+			runtime.GOOS,
71
+			runtime.GOARCH,
72
+			runtime.NumCPU(),
73
+			os.Getenv("GOPATH"),
74
+			runtime.GOROOT(),
75
+			runtime.Compiler,
76
+			version,
77
+			getBeegoVersion(),
78
+		}
79
+		switch outputFormat {
80
+		case "json":
81
+			{
82
+				b, err := json.MarshalIndent(runtimeInfo, "", "    ")
83
+				MustCheck(err)
84
+				fmt.Println(string(b))
85
+				return 0
86
+			}
87
+		case "yaml":
88
+			{
89
+				b, err := yaml.Marshal(&runtimeInfo)
90
+				MustCheck(err)
91
+				fmt.Println(string(b))
92
+				return 0
93
+			}
94
+		}
95
+	}
96
+
97
+	coloredBanner := fmt.Sprintf(verboseVersionBanner, "\x1b[35m", "\x1b[1m",
98
+		"\x1b[0m", "\x1b[32m", "\x1b[1m", "\x1b[0m")
99
+	InitBanner(stdout, bytes.NewBufferString(coloredBanner))
100
+	return 0
65 101
 }
66 102
 
67
-// ShowShortVersionBanner prints the short version banner
103
+// ShowShortVersionBanner prints the short version banner.
68 104
 func ShowShortVersionBanner() {
69
-	w := NewColorWriter(os.Stdout)
70
-	coloredBanner := fmt.Sprintf(shortVersionBanner, "\x1b[35m", "\x1b[1m", "\x1b[0m")
71
-	InitBanner(w, bytes.NewBufferString(coloredBanner))
105
+	output := NewColorWriter(os.Stdout)
106
+	InitBanner(output, bytes.NewBufferString(MagentaBold(shortVersionBanner)))
72 107
 }
73 108
 
74 109
 func getBeegoVersion() string {

Loading…
Cancel
Save