1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 20:10:55 +00:00
bee/bee.go

259 lines
5.6 KiB
Go
Raw Normal View History

2013-09-03 17:23:58 +00:00
// Copyright 2013 bee authors
2013-07-27 01:44:44 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// Bee is a tool for developing applications based on beego framework.
2012-12-15 16:56:28 +00:00
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
2012-12-15 16:56:28 +00:00
)
2016-12-20 08:54:03 +00:00
const version = "1.6.2"
2014-05-16 17:37:45 +00:00
// Command is the unit of execution
2012-12-15 16:56:28 +00:00
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
2014-08-15 09:38:51 +00:00
Run func(cmd *Command, args []string) int
2012-12-15 16:56:28 +00:00
// PreRun performs an operation before running the command
PreRun func(cmd *Command, args []string)
2012-12-15 16:56:28 +00:00
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description shown in the 'go help' output.
Short string
2012-12-15 16:56:28 +00:00
// Long is the long message shown in the 'go help <this-command>' output.
Long string
2012-12-15 16:56:28 +00:00
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
// CustomFlags indicates that the command will do its own
// flag parsing.
CustomFlags bool
// output out writer if set in SetOutput(w)
output *io.Writer
2012-12-15 16:56:28 +00:00
}
// Name returns the command's name: the first word in the usage line.
func (c *Command) Name() string {
name := c.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
// SetOutput sets the destination for usage and error messages.
// If output is nil, os.Stderr is used.
func (c *Command) SetOutput(output io.Writer) {
c.output = &output
}
// Out returns the out writer of the current command.
// If cmd.output is nil, os.Stderr is used.
func (c *Command) Out() io.Writer {
if c.output != nil {
return *c.output
}
return NewColorWriter(os.Stderr)
}
// Usage puts out the usage for the command.
2012-12-15 16:56:28 +00:00
func (c *Command) Usage() {
tmpl(cmdUsage, c)
2012-12-15 16:56:28 +00:00
os.Exit(2)
}
// Runnable reports whether the command can be run; otherwise
// it is a documentation pseudo-command such as import path.
2012-12-15 16:56:28 +00:00
func (c *Command) Runnable() bool {
return c.Run != nil
}
func (c *Command) Options() map[string]string {
options := make(map[string]string)
c.Flag.VisitAll(func(f *flag.Flag) {
defaultVal := f.DefValue
if len(defaultVal) > 0 {
options[f.Name+"="+defaultVal] = f.Usage
} else {
options[f.Name] = f.Usage
}
})
return options
}
var availableCommands = []*Command{
cmdNew,
2013-07-24 12:01:14 +00:00
cmdRun,
cmdPack,
2013-07-08 07:34:58 +00:00
cmdApiapp,
cmdHproseapp,
2014-08-18 15:12:27 +00:00
//cmdRouter,
//cmdTest,
2013-09-03 17:23:58 +00:00
cmdBale,
2014-05-16 17:37:45 +00:00
cmdVersion,
cmdGenerate,
2014-08-18 15:12:27 +00:00
//cmdRundocs,
2014-08-11 03:33:53 +00:00
cmdMigrate,
2016-01-06 03:55:56 +00:00
cmdFix,
cmdDockerize,
2012-12-15 16:56:28 +00:00
}
var logger = GetBeeLogger(os.Stdout)
2012-12-15 16:56:28 +00:00
func main() {
currentpath, _ := os.Getwd()
2012-12-15 16:56:28 +00:00
flag.Usage = usage
flag.Parse()
log.SetFlags(0)
args := flag.Args()
2012-12-15 16:56:28 +00:00
if len(args) < 1 {
usage()
}
if args[0] == "help" {
help(args[1:])
return
}
for _, cmd := range availableCommands {
2012-12-15 16:56:28 +00:00
if cmd.Name() == args[0] && cmd.Run != nil {
cmd.Flag.Usage = func() { cmd.Usage() }
if cmd.CustomFlags {
args = args[1:]
} else {
2013-06-27 07:29:27 +00:00
cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args()
2012-12-15 16:56:28 +00:00
}
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") && isGenerateDocs(cmd.Name(), args) {
parsePackagesFromDir(currentpath)
}
2014-08-15 09:38:51 +00:00
os.Exit(cmd.Run(cmd, args))
2012-12-15 16:56:28 +00:00
return
}
}
printErrorAndExit("Unknown subcommand")
2012-12-15 16:56:28 +00:00
}
func isGenerateDocs(name string, args []string) bool {
if name != "generate" {
return false
}
for _, a := range args {
if a == "docs" {
return true
}
}
return false
}
var usageTemplate = `Bee is a Fast and Flexible tool for managing your Beego Web Application.
2012-12-15 16:56:28 +00:00
{{"USAGE" | headline}}
{{"bee command [arguments]" | bold}}
2012-12-15 16:56:28 +00:00
{{"AVAILABLE COMMANDS" | headline}}
2012-12-15 16:56:28 +00:00
{{range .}}{{if .Runnable}}
{{.Name | printf "%-11s" | bold}} {{.Short}}{{end}}{{end}}
2012-12-15 16:56:28 +00:00
Use {{"bee help [command]" | bold}} for more information about a command.
2012-12-15 16:56:28 +00:00
{{"ADDITIONAL HELP TOPICS" | headline}}
2012-12-15 16:56:28 +00:00
{{range .}}{{if not .Runnable}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
Use {{"bee help [topic]" | bold}} for more information about that topic.
2012-12-15 16:56:28 +00:00
`
var helpTemplate = `{{"USAGE" | headline}}
{{.UsageLine | printf "bee %s" | bold}}
{{if .Options}}{{endline}}{{"OPTIONS" | headline}}{{range $k,$v := .Options}}
{{$k | printf "-%s" | bold}}
{{$v}}
{{end}}{{end}}
{{"DESCRIPTION" | headline}}
{{tmpltostr .Long . | trim}}
`
2012-12-15 16:56:28 +00:00
var errorTemplate = `bee: %s.
Use {{"bee help" | bold}} for more information.
2012-12-15 16:56:28 +00:00
`
var cmdUsage = `Use {{printf "bee help %s" .Name | bold}} for more information.{{endline}}`
2012-12-15 16:56:28 +00:00
func usage() {
tmpl(usageTemplate, availableCommands)
2012-12-15 16:56:28 +00:00
os.Exit(2)
}
func tmpl(text string, data interface{}) {
output := NewColorWriter(os.Stderr)
t := template.New("usage").Funcs(BeeFuncMap())
2012-12-15 16:56:28 +00:00
template.Must(t.Parse(text))
err := t.Execute(output, data)
MustCheck(err)
2012-12-15 16:56:28 +00:00
}
func help(args []string) {
if len(args) == 0 {
usage()
}
if len(args) != 1 {
printErrorAndExit("Too many arguments")
2012-12-15 16:56:28 +00:00
}
arg := args[0]
for _, cmd := range availableCommands {
2012-12-15 16:56:28 +00:00
if cmd.Name() == arg {
tmpl(helpTemplate, cmd)
2012-12-15 16:56:28 +00:00
return
}
}
printErrorAndExit("Unknown help topic")
}
2012-12-15 16:56:28 +00:00
func printErrorAndExit(message string) {
tmpl(fmt.Sprintf(errorTemplate, message), nil)
os.Exit(2)
2012-12-15 16:56:28 +00:00
}