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