mirror of
https://github.com/beego/bee.git
synced 2024-12-22 10:00:50 +00:00
Improves rs command using Go templates
This commit is contained in:
parent
01703caefa
commit
e543958fe3
@ -11,53 +11,53 @@
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
// Package rs ...
|
||||
package rs
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"os"
|
||||
|
||||
"runtime"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
"github.com/beego/bee/utils"
|
||||
)
|
||||
|
||||
type customCommand struct {
|
||||
Name string
|
||||
Command string
|
||||
}
|
||||
|
||||
var description = `Run script allows you to run arbitrary commands using Bee.
|
||||
var cmdRs = &commands.Command{
|
||||
UsageLine: "rs",
|
||||
Short: "Run customized scripts",
|
||||
Long: `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" | 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,
|
||||
}
|
||||
|
||||
func init() {
|
||||
CmdNew := &commands.Command{
|
||||
UsageLine: "rs",
|
||||
Short: "Run customized scripts",
|
||||
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
||||
Run: RunScript,
|
||||
}
|
||||
config.LoadConfig()
|
||||
CmdNew.Long = description
|
||||
for commandName, command := range config.Conf.Scripts {
|
||||
CmdNew.Long += fmt.Sprintf(" ▶ $ bee %s %s ==> %s\n", CmdNew.UsageLine, commandName, command)
|
||||
}
|
||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdNew)
|
||||
cmdRs.Long = utils.TmplToString(cmdRs.Long, config.Conf.Scripts)
|
||||
commands.AvailableCommands = append(commands.AvailableCommands, cmdRs)
|
||||
}
|
||||
|
||||
func RunScript(cmd *commands.Command, args []string) int {
|
||||
func runScript(cmd *commands.Command, args []string) int {
|
||||
if len(args) == 0 {
|
||||
cmd.Usage()
|
||||
return 0
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
for _, arg := range args {
|
||||
if c, exist := config.Conf.Scripts[arg]; exist {
|
||||
command := customCommand{
|
||||
@ -68,14 +68,21 @@ func RunScript(cmd *commands.Command, args []string) int {
|
||||
beeLogger.Log.Error(err.Error())
|
||||
}
|
||||
} else {
|
||||
beeLogger.Log.Errorf("Command %s not found in Beefile", arg)
|
||||
beeLogger.Log.Errorf("Command '%s' not found in Beefile/bee.json", arg)
|
||||
}
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
fmt.Println(colors.GreenBold(fmt.Sprintf("Finished in %s.", elapsed)))
|
||||
return 0
|
||||
}
|
||||
|
||||
type customCommand struct {
|
||||
Name string
|
||||
Command string
|
||||
}
|
||||
|
||||
func (c *customCommand) run() error {
|
||||
beeLogger.Log.Info(colors.GreenBold(fmt.Sprintf("Running '%s':", c.Name)))
|
||||
beeLogger.Log.Info(colors.GreenBold(fmt.Sprintf("Running '%s'...", c.Name)))
|
||||
var cmd *exec.Cmd
|
||||
switch runtime.GOOS {
|
||||
case "darwin", "linux":
|
||||
|
Loading…
Reference in New Issue
Block a user