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
|
|
|
|
2017-03-23 10:15:53 +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
|
|
|
|
2017-03-23 10:15:53 +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 := .}}
|
2017-03-23 10:15:53 +00:00
|
|
|
{{$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()
|
2017-03-23 10:15:53 +00:00
|
|
|
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
|
|
|
}
|
2017-03-23 10:15:53 +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
|
2017-03-23 10:15:53 +00:00
|
|
|
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":
|
2017-03-23 10:15:53 +00:00
|
|
|
args := append([]string{c.Command}, c.Args...)
|
|
|
|
cmd = exec.Command("sh", "-c", strings.Join(args, " "))
|
2017-03-18 19:10:43 +00:00
|
|
|
case "windows":
|
2017-03-23 10:15:53 +00:00
|
|
|
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()
|
|
|
|
}
|