mirror of
https://github.com/beego/bee.git
synced 2025-06-20 08:20:39 +00:00
Refactor!
create sub packages delete unused code delete code from not use command cmdRouter,cmdTest, cmdRundocs make command plugins check with gosimple,staticcheck,go vet,unused,unconvert
This commit is contained in:
autorouter.goautorouter_test.gobee.go
cmd
code.goconfig
g.gog_scaffold.gogenerate
g.gog_appcode.gog_controllers.gog_hproseappcode.gog_migration.gog_model.gog_scaffold.gog_views.go
hproseapp.goswaggergen
logger
main.gorundocs.gotest.gotestdata/router
utils
vendor/github.com/smartystreets/assertions/internal/oglematchers
72
cmd/commands/version/banner.go
Normal file
72
cmd/commands/version/banner.go
Normal file
@ -0,0 +1,72 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"text/template"
|
||||
|
||||
"time"
|
||||
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
)
|
||||
|
||||
// RuntimeInfo holds information about the current runtime.
|
||||
type RuntimeInfo struct {
|
||||
GoVersion string
|
||||
GOOS string
|
||||
GOARCH string
|
||||
NumCPU int
|
||||
GOPATH string
|
||||
GOROOT string
|
||||
Compiler string
|
||||
BeeVersion string
|
||||
BeegoVersion string
|
||||
}
|
||||
|
||||
// InitBanner loads the banner and prints it to output
|
||||
// All errors are ignored, the application will not
|
||||
// print the banner in case of error.
|
||||
func InitBanner(out io.Writer, in io.Reader) {
|
||||
if in == nil {
|
||||
beeLogger.Log.Fatal("The input is nil")
|
||||
}
|
||||
|
||||
banner, err := ioutil.ReadAll(in)
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Error while trying to read the banner: %s", err)
|
||||
}
|
||||
|
||||
show(out, string(banner))
|
||||
}
|
||||
|
||||
func show(out io.Writer, content string) {
|
||||
t, err := template.New("banner").
|
||||
Funcs(template.FuncMap{"Now": Now}).
|
||||
Parse(content)
|
||||
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Cannot parse the banner template: %s", err)
|
||||
}
|
||||
|
||||
err = t.Execute(out, RuntimeInfo{
|
||||
GetGoVersion(),
|
||||
runtime.GOOS,
|
||||
runtime.GOARCH,
|
||||
runtime.NumCPU(),
|
||||
os.Getenv("GOPATH"),
|
||||
runtime.GOROOT(),
|
||||
runtime.Compiler,
|
||||
version,
|
||||
GetBeegoVersion(),
|
||||
})
|
||||
if err != nil {
|
||||
beeLogger.Log.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Now returns the current local time in the specified layout
|
||||
func Now(layout string) string {
|
||||
return time.Now().Format(layout)
|
||||
}
|
175
cmd/commands/version/version.go
Normal file
175
cmd/commands/version/version.go
Normal file
@ -0,0 +1,175 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
path "path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/beego/bee/cmd/commands"
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"github.com/beego/bee/logger/colors"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const verboseVersionBanner string = `%s%s______
|
||||
| ___ \
|
||||
| |_/ / ___ ___
|
||||
| ___ \ / _ \ / _ \
|
||||
| |_/ /| __/| __/
|
||||
\____/ \___| \___| v{{ .BeeVersion }}%s
|
||||
%s%s
|
||||
├── Beego : {{ .BeegoVersion }}
|
||||
├── GoVersion : {{ .GoVersion }}
|
||||
├── GOOS : {{ .GOOS }}
|
||||
├── GOARCH : {{ .GOARCH }}
|
||||
├── NumCPU : {{ .NumCPU }}
|
||||
├── GOPATH : {{ .GOPATH }}
|
||||
├── GOROOT : {{ .GOROOT }}
|
||||
├── Compiler : {{ .Compiler }}
|
||||
└── Date : {{ Now "Monday, 2 Jan 2006" }}%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 = "1.8.0"
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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,
|
||||
GetBeegoVersion(),
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 GetBeegoVersion() string {
|
||||
gopath := os.Getenv("GOPATH")
|
||||
re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if gopath == "" {
|
||||
err = fmt.Errorf("You need to set GOPATH environment variable")
|
||||
return ""
|
||||
}
|
||||
wgopath := path.SplitList(gopath)
|
||||
for _, wg := range wgopath {
|
||||
wg, _ = path.EvalSymlinks(path.Join(wg, "src", "github.com", "astaxie", "beego"))
|
||||
filename := path.Join(wg, "beego.go")
|
||||
_, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
beeLogger.Log.Error("Error while getting stats of 'beego.go'")
|
||||
}
|
||||
fd, err := os.Open(filename)
|
||||
if err != nil {
|
||||
beeLogger.Log.Error("Error while reading 'beego.go'")
|
||||
continue
|
||||
}
|
||||
reader := bufio.NewReader(fd)
|
||||
for {
|
||||
byteLine, _, er := reader.ReadLine()
|
||||
if er != nil && er != io.EOF {
|
||||
return ""
|
||||
}
|
||||
if er == io.EOF {
|
||||
break
|
||||
}
|
||||
line := string(byteLine)
|
||||
s := re.FindStringSubmatch(line)
|
||||
if len(s) >= 2 {
|
||||
return s[1]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return "Beego is not installed. Please do consider installing it first: https://github.com/astaxie/beego"
|
||||
}
|
||||
|
||||
func GetGoVersion() string {
|
||||
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)
|
||||
}
|
||||
return strings.Split(string(cmdOut), " ")[2]
|
||||
}
|
Reference in New Issue
Block a user