/
autoload.go
74 lines (67 loc) · 1.55 KB
/
autoload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package banner
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"runtime"
"text/template"
"time"
colorable "github.com/mattn/go-colorable"
)
type vars struct {
GoVersion string
GOOS string
GOARCH string
Cli cli
}
func (v vars) Now(layout string) string {
return time.Now().Format(layout)
}
// DefaultBannerFile is the default file to render
const DefaultBannerFile = "banner.tpl"
func Autoload(title, version, statement string) {
out := colorable.NewColorableStdout()
c := cli{
Title: title,
Version: version,
Statement: statement,
}
// restore default color
defer func() {
out.Write([]byte(c.DefaultColor()))
}()
// panic recover
defer func() {
if err := recover(); err != nil {
out.Write([]byte(c.Error("\n[error] Banner-autoload:" + err.(error).Error())))
}
}()
// get this file path
_, file, _, ok := runtime.Caller(0)
if !ok {
panic(fmt.Errorf("Unable to access the current file path"))
}
path := filepath.Dir(file)
bytes, err := ioutil.ReadFile(path + "/" + DefaultBannerFile)
if err != nil {
panic(fmt.Errorf("Unable to access the render file \"" + DefaultBannerFile + "\""))
}
// render template
if err := ColorableRender(out, string(bytes), c); err != nil {
panic(err)
}
}
// ColorableRender help user to render the colorable-template file
func ColorableRender(out io.Writer, content string, cmdline cli) error {
t, err := template.New("banner").Parse(content)
if err != nil {
return err
}
return t.Execute(out, vars{
runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
cmdline,
})
}