1
0
mirror of https://github.com/beego/bee.git synced 2025-07-14 23:31:02 +00:00

Added PreRun phase to Command struct

Now each command has a PreRun function that will execute before calling
the Run() function. This allows to show the banner and do some pre-check
work. Also moved parsePackagesFromDir() to the main function to avoid
getting called each time 'bee' is invoked.
This commit is contained in:
Faissal Elamraoui
2016-11-20 22:28:52 +01:00
parent 35384b463e
commit 03f2057eb0
13 changed files with 39 additions and 29 deletions

17
bee.go
View File

@ -33,6 +33,9 @@ type Command struct {
// The args are the arguments after the command name.
Run func(cmd *Command, args []string) int
// PreRun performs an operation before running the command
PreRun func(cmd *Command, args []string)
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
@ -93,6 +96,8 @@ var commands = []*Command{
var logger = GetBeeLogger(os.Stdout)
func main() {
currentpath, _ := os.Getwd()
flag.Usage = usage
flag.Parse()
log.SetFlags(0)
@ -116,6 +121,17 @@ func main() {
cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args()
}
if cmd.PreRun != nil {
cmd.PreRun(cmd, args)
}
// Check if current directory is inside the GOPATH,
// if so parse the packages inside it.
if strings.Contains(currentpath, GetGOPATHs()[0]+"/src") {
parsePackagesFromDir(currentpath)
}
os.Exit(cmd.Run(cmd, args))
return
}
@ -142,7 +158,6 @@ Additional help topics:
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
Use "bee help [topic]" for more information about that topic.
`
var helpTemplate = `{{if .Runnable}}usage: bee {{.UsageLine}}