2017-03-06 23:58:53 +00:00
|
|
|
package version
|
2016-06-01 12:30:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"text/template"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
|
|
|
"time"
|
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
2020-12-22 14:17:05 +00:00
|
|
|
"github.com/beego/bee/v2/utils"
|
2016-06-01 12:30:29 +00:00
|
|
|
)
|
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
// RuntimeInfo holds information about the current runtime.
|
|
|
|
type RuntimeInfo struct {
|
2022-06-10 10:54:40 +00:00
|
|
|
GoVersion string
|
|
|
|
GOOS string
|
|
|
|
GOARCH string
|
|
|
|
NumCPU int
|
|
|
|
GOPATH string
|
|
|
|
GOROOT string
|
|
|
|
Compiler string
|
|
|
|
BeeVersion string
|
|
|
|
Published string
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 21:23:07 +00:00
|
|
|
// InitBanner loads the banner and prints it to output
|
2016-06-01 12:30:29 +00:00
|
|
|
// 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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatal("The input is nil")
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
banner, err := ioutil.ReadAll(in)
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Error while trying to read the banner: %s", err)
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Cannot parse the banner template: %s", err)
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 13:51:14 +00:00
|
|
|
err = t.Execute(out, RuntimeInfo{
|
2022-06-10 10:54:40 +00:00
|
|
|
runtime.Version(),
|
2016-06-01 12:30:29 +00:00
|
|
|
runtime.GOOS,
|
|
|
|
runtime.GOARCH,
|
|
|
|
runtime.NumCPU(),
|
|
|
|
os.Getenv("GOPATH"),
|
|
|
|
runtime.GOROOT(),
|
|
|
|
runtime.Compiler,
|
|
|
|
version,
|
2020-09-14 16:05:38 +00:00
|
|
|
utils.GetLastPublishedTime(),
|
2016-06-01 12:30:29 +00:00
|
|
|
})
|
2017-03-06 23:58:53 +00:00
|
|
|
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)
|
2016-06-01 12:30:29 +00:00
|
|
|
}
|