1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 09:50:55 +00:00
bee/cmd/commands/rs/rs.go

103 lines
2.7 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
"strings"
2020-12-16 05:20:41 +00:00
"github.com/beego/bee/v2/cmd/commands"
"github.com/beego/bee/v2/cmd/commands/version"
"github.com/beego/bee/v2/config"
"github.com/beego/bee/v2/logger"
"github.com/beego/bee/v2/logger/colors"
"github.com/beego/bee/v2/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 ARGS" | bold}}
2017-03-18 19:04:08 +00:00
{{if len .}}
{{"AVAILABLE SCRIPTS"|headline}}{{range $cmdName, $cmd := .}}
{{$cmdName | bold}}
2017-03-18 19:04:08 +00:00
{{$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()
script, args := args[0], args[1:]
if c, exist := config.Conf.Scripts[script]; exist {
command := customCommand{
Name: script,
Command: c,
Args: args,
}
if err := command.run(); err != nil {
beeLogger.Log.Error(err.Error())
2017-03-17 15:10:56 +00:00
}
} else {
beeLogger.Log.Errorf("Command '%s' not found in Beefile/bee.json", script)
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
Args []string
2017-03-18 19:04:08 +00:00
}
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":
args := append([]string{c.Command}, c.Args...)
cmd = exec.Command("sh", "-c", strings.Join(args, " "))
case "windows":
args := append([]string{c.Command}, c.Args...)
cmd = exec.Command("cmd", "/C", strings.Join(args, " "))
2017-03-17 15:10:56 +00:00
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}