1
0
mirror of https://github.com/beego/bee.git synced 2024-11-24 18:30:53 +00:00

add arguments support to rs functionality (#386)

This commit is contained in:
Boris Borshevsky 2017-03-23 12:15:53 +02:00 committed by Faissal Elamraoui
parent 8c0742c140
commit 48c931d635
2 changed files with 53 additions and 15 deletions

View File

@ -169,6 +169,38 @@ ______
For more information on the usage, run `bee help pack`. For more information on the usage, run `bee help pack`.
### bee rs
Inspired by makefile / npm scripts.
Run script allows you to run arbitrary commands using Bee.
Custom commands are provided from the "scripts" object inside bee.json or Beefile.
To run a custom command, use: $ bee rs mycmd ARGS
```bash
$ bee help rs
USAGE
bee rs
DESCRIPTION
Run script allows you to run arbitrary commands using Bee.
Custom commands are provided from the "scripts" object inside bee.json or Beefile.
To run a custom command, use: $ bee rs mycmd ARGS
AVAILABLE SCRIPTS
gtest
APP_ENV=test APP_CONF_PATH=$(pwd)/conf go test -v -cover
gtestall
APP_ENV=test APP_CONF_PATH=$(pwd)/conf go test -v -cover $(go list ./... | grep -v /vendor/)
```
*Run your scripts with:*
```$ bee rs gtest tests/*.go```
```$ bee rs gtestall```
### bee api ### bee api
To create a Beego API application: To create a Beego API application:

View File

@ -22,6 +22,8 @@ import (
"runtime" "runtime"
"time" "time"
"strings"
"github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands"
"github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/cmd/commands/version"
"github.com/beego/bee/config" "github.com/beego/bee/config"
@ -36,10 +38,10 @@ var cmdRs = &commands.Command{
Long: `Run script allows you to run arbitrary commands using Bee. Long: `Run script allows you to run arbitrary commands using Bee.
Custom commands are provided from the "scripts" object inside bee.json or Beefile. Custom commands are provided from the "scripts" object inside bee.json or Beefile.
To run a custom command, use: {{"$ bee rs mycmd" | bold}} To run a custom command, use: {{"$ bee rs mycmd ARGS" | bold}}
{{if len .}} {{if len .}}
{{"AVAILABLE SCRIPTS"|headline}}{{range $cmdName, $cmd := .}} {{"AVAILABLE SCRIPTS"|headline}}{{range $cmdName, $cmd := .}}
{{$cmdName | printf "-%s" | bold}} {{$cmdName | bold}}
{{$cmd}}{{end}}{{end}} {{$cmd}}{{end}}{{end}}
`, `,
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
@ -58,18 +60,19 @@ func runScript(cmd *commands.Command, args []string) int {
} }
start := time.Now() start := time.Now()
for _, arg := range args { script, args := args[0], args[1:]
if c, exist := config.Conf.Scripts[arg]; exist {
command := customCommand{ if c, exist := config.Conf.Scripts[script]; exist {
Name: arg, command := customCommand{
Command: c, Name: script,
} Command: c,
if err := command.run(); err != nil { Args: args,
beeLogger.Log.Error(err.Error())
}
} else {
beeLogger.Log.Errorf("Command '%s' not found in Beefile/bee.json", arg)
} }
if err := command.run(); err != nil {
beeLogger.Log.Error(err.Error())
}
} else {
beeLogger.Log.Errorf("Command '%s' not found in Beefile/bee.json", script)
} }
elapsed := time.Since(start) elapsed := time.Since(start)
fmt.Println(colors.GreenBold(fmt.Sprintf("Finished in %s.", elapsed))) fmt.Println(colors.GreenBold(fmt.Sprintf("Finished in %s.", elapsed)))
@ -79,6 +82,7 @@ func runScript(cmd *commands.Command, args []string) int {
type customCommand struct { type customCommand struct {
Name string Name string
Command string Command string
Args []string
} }
func (c *customCommand) run() error { func (c *customCommand) run() error {
@ -86,9 +90,11 @@ func (c *customCommand) run() error {
var cmd *exec.Cmd var cmd *exec.Cmd
switch runtime.GOOS { switch runtime.GOOS {
case "darwin", "linux": case "darwin", "linux":
cmd = exec.Command("sh", "-c", c.Command) args := append([]string{c.Command}, c.Args...)
cmd = exec.Command("sh", "-c", strings.Join(args, " "))
case "windows": case "windows":
cmd = exec.Command("cmd", "/C", c.Command) args := append([]string{c.Command}, c.Args...)
cmd = exec.Command("cmd", "/C", strings.Join(args, " "))
} }
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr