1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 20:20:55 +00:00
bee/cmd/commands/version/version.go

128 lines
2.8 KiB
Go
Raw Normal View History

package version
2014-05-16 17:37:45 +00:00
import (
2016-07-31 21:23:07 +00:00
"bytes"
"encoding/json"
"flag"
2014-05-16 17:37:45 +00:00
"fmt"
2014-05-22 02:35:37 +00:00
"os"
2016-10-22 13:44:22 +00:00
"os/exec"
"runtime"
2016-10-22 13:44:22 +00:00
"strings"
"gopkg.in/yaml.v2"
2020-12-16 05:20:41 +00:00
"github.com/beego/bee/v2/cmd/commands"
"github.com/beego/bee/v2/config"
beeLogger "github.com/beego/bee/v2/logger"
"github.com/beego/bee/v2/logger/colors"
"github.com/beego/bee/v2/utils"
2014-05-16 17:37:45 +00:00
)
2016-07-31 21:23:07 +00:00
const verboseVersionBanner string = `%s%s______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
2016-07-31 21:23:07 +00:00
\____/ \___| \___| v{{ .BeeVersion }}%s
%s%s
GoVersion : {{ .GoVersion }}
GOOS : {{ .GOOS }}
GOARCH : {{ .GOARCH }}
NumCPU : {{ .NumCPU }}
GOPATH : {{ .GOPATH }}
GOROOT : {{ .GOROOT }}
Compiler : {{ .Compiler }}
2020-09-14 16:05:38 +00:00
Published : {{ .Published }}%s
`
const shortVersionBanner = `______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v{{ .BeeVersion }}
`
var CmdVersion = &commands.Command{
UsageLine: "version",
Short: "Prints the current Bee version",
Long: `
Prints the current Bee, Beego and Go version alongside the platform information.
`,
Run: versionCmd,
}
var outputFormat string
const version = config.Version
2014-05-16 17:37:45 +00:00
func init() {
fs := flag.NewFlagSet("version", flag.ContinueOnError)
fs.StringVar(&outputFormat, "o", "", "Set the output format. Either json or yaml.")
CmdVersion.Flag = *fs
commands.AvailableCommands = append(commands.AvailableCommands, CmdVersion)
2014-05-16 17:37:45 +00:00
}
func versionCmd(cmd *commands.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,
2020-09-14 16:05:38 +00:00
utils.GetLastPublishedTime(),
}
switch outputFormat {
case "json":
{
b, err := json.MarshalIndent(runtimeInfo, "", " ")
if err != nil {
beeLogger.Log.Error(err.Error())
}
fmt.Println(string(b))
return 0
}
case "yaml":
{
b, err := yaml.Marshal(&runtimeInfo)
if err != nil {
beeLogger.Log.Error(err.Error())
}
fmt.Println(string(b))
return 0
}
}
}
2014-05-22 02:35:37 +00:00
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 := colors.NewColorWriter(os.Stdout)
InitBanner(output, bytes.NewBufferString(colors.MagentaBold(shortVersionBanner)))
}
func GetGoVersion() string {
2016-10-22 13:44:22 +00:00
var (
cmdOut []byte
err error
)
if cmdOut, err = exec.Command("go", "version").Output(); err != nil {
beeLogger.Log.Fatalf("There was an error running 'go version' command: %s", err)
2016-10-22 13:44:22 +00:00
}
return strings.Split(string(cmdOut), " ")[2]
}