1
0
mirror of https://github.com/beego/bee.git synced 2024-11-15 03:20:54 +00:00
bee/cmd/commands/rs/rs.go

97 lines
2.5 KiB
Go
Raw Normal View History

2017-03-17 15:10:56 +00:00
// Copyright 2017 bee authors
//
// 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.
2017-03-18 19:04:08 +00:00
// Package rs ...
2017-03-17 15:10:56 +00:00
package rs
import (
2017-03-18 19:04:08 +00:00
"fmt"
2017-03-17 15:10:56 +00:00
"os"
2017-03-18 19:04:08 +00:00
"os/exec"
2017-03-17 15:10:56 +00:00
"runtime"
2017-03-18 19:04:08 +00:00
"time"
2017-03-17 15:10:56 +00:00
"github.com/beego/bee/cmd/commands"
"github.com/beego/bee/cmd/commands/version"
"github.com/beego/bee/config"
"github.com/beego/bee/logger"
"github.com/beego/bee/logger/colors"
2017-03-18 19:04:08 +00:00
"github.com/beego/bee/utils"
2017-03-17 15:10:56 +00:00
)
2017-03-18 19:04:08 +00:00
var cmdRs = &commands.Command{
UsageLine: "rs",
Short: "Run customized scripts",
Long: `Run script allows you to run arbitrary commands using Bee.
2017-03-17 15:10:56 +00:00
Custom commands are provided from the "scripts" object inside bee.json or Beefile.
2017-03-18 19:04:08 +00:00
To run a custom command, use: {{"$ bee rs mycmd" | bold}}
{{if len .}}
{{"AVAILABLE SCRIPTS"|headline}}{{range $cmdName, $cmd := .}}
{{$cmdName | printf "-%s" | bold}}
{{$cmd}}{{end}}{{end}}
`,
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
Run: runScript,
}
2017-03-17 15:10:56 +00:00
func init() {
config.LoadConfig()
2017-03-18 19:04:08 +00:00
cmdRs.Long = utils.TmplToString(cmdRs.Long, config.Conf.Scripts)
commands.AvailableCommands = append(commands.AvailableCommands, cmdRs)
2017-03-17 15:10:56 +00:00
}
2017-03-18 19:04:08 +00:00
func runScript(cmd *commands.Command, args []string) int {
2017-03-17 15:10:56 +00:00
if len(args) == 0 {
cmd.Usage()
}
2017-03-18 19:04:08 +00:00
start := time.Now()
2017-03-17 15:10:56 +00:00
for _, arg := range args {
if c, exist := config.Conf.Scripts[arg]; exist {
command := customCommand{
Name: arg,
Command: c,
}
if err := command.run(); err != nil {
beeLogger.Log.Error(err.Error())
}
} else {
2017-03-18 19:04:08 +00:00
beeLogger.Log.Errorf("Command '%s' not found in Beefile/bee.json", arg)
2017-03-17 15:10:56 +00:00
}
}
2017-03-18 19:04:08 +00:00
elapsed := time.Since(start)
fmt.Println(colors.GreenBold(fmt.Sprintf("Finished in %s.", elapsed)))
2017-03-17 15:10:56 +00:00
return 0
}
2017-03-18 19:04:08 +00:00
type customCommand struct {
Name string
Command string
}
2017-03-17 15:10:56 +00:00
func (c *customCommand) run() error {
2017-03-18 19:04:08 +00:00
beeLogger.Log.Info(colors.GreenBold(fmt.Sprintf("Running '%s'...", c.Name)))
2017-03-17 15:10:56 +00:00
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin", "linux":
cmd = exec.Command("sh", "-c", c.Command)
case "windows":
cmd = exec.Command("cmd", "/C", c.Command)
2017-03-17 15:10:56 +00:00
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}