From 01703caefaf1c9f5c1e6215113583ee7d7745b2c Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Fri, 17 Mar 2017 17:10:56 +0200 Subject: [PATCH 1/4] delete scripts add rs --- cmd/bee.go | 2 +- cmd/commands/rs/rs.go | 88 +++++++++++++++++++++++++++++++++ cmd/commands/scripts/scripts.go | 44 ----------------- 3 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 cmd/commands/rs/rs.go delete mode 100644 cmd/commands/scripts/scripts.go diff --git a/cmd/bee.go b/cmd/bee.go index fd615bf..702340e 100644 --- a/cmd/bee.go +++ b/cmd/bee.go @@ -26,8 +26,8 @@ import ( _ "github.com/beego/bee/cmd/commands/migrate" _ "github.com/beego/bee/cmd/commands/new" _ "github.com/beego/bee/cmd/commands/pack" + _ "github.com/beego/bee/cmd/commands/rs" _ "github.com/beego/bee/cmd/commands/run" - _ "github.com/beego/bee/cmd/commands/scripts" _ "github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/utils" ) diff --git a/cmd/commands/rs/rs.go b/cmd/commands/rs/rs.go new file mode 100644 index 0000000..ec4d067 --- /dev/null +++ b/cmd/commands/rs/rs.go @@ -0,0 +1,88 @@ +// 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. +package rs + +import ( + "os/exec" + + "os" + + "runtime" + + "fmt" + + "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" +) + +type customCommand struct { + Name string + Command string +} + +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. +` + +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) +} + +func RunScript(cmd *commands.Command, args []string) int { + if len(args) == 0 { + cmd.Usage() + return 0 + } + 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 { + beeLogger.Log.Errorf("Command %s not found in Beefile", arg) + } + } + return 0 +} + +func (c *customCommand) run() error { + beeLogger.Log.Info(colors.GreenBold(fmt.Sprintf("Running '%s':", c.Name))) + var cmd *exec.Cmd + switch runtime.GOOS { + case "darwin", "linux": + cmd = exec.Command("sh", "-c", c.Command) + case "windows": //TODO + } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/cmd/commands/scripts/scripts.go b/cmd/commands/scripts/scripts.go deleted file mode 100644 index b5576c5..0000000 --- a/cmd/commands/scripts/scripts.go +++ /dev/null @@ -1,44 +0,0 @@ -package scripts - -import ( - "os/exec" - - "os" - - "runtime" - - "strings" - - "github.com/beego/bee/cmd/commands" - "github.com/beego/bee/cmd/commands/version" - "github.com/beego/bee/config" - "github.com/beego/bee/logger" -) - -func init() { - for commandName, command := range config.Conf.Scripts { - CmdNew := &commands.Command{ - UsageLine: commandName, - Short: command, - PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, - Run: RunScript, - } - commands.AvailableCommands = append(commands.AvailableCommands, CmdNew) - } -} - -func RunScript(cmd *commands.Command, args []string) int { - var c *exec.Cmd - switch runtime.GOOS { - case "darwin", "linux": - c = exec.Command("sh", "-c", cmd.Short+" "+strings.Join(args, " ")) - case "windows": //TODO - } - c.Stdout = os.Stdout - c.Stderr = os.Stderr - err := c.Run() - if err != nil { - beeLogger.Log.Error(err.Error()) - } - return 0 -} From e543958fe3bff3294bdb3e12a10664e88cd41192 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sat, 18 Mar 2017 20:04:08 +0100 Subject: [PATCH 2/4] Improves rs command using Go templates --- cmd/commands/rs/rs.go | 63 ++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/cmd/commands/rs/rs.go b/cmd/commands/rs/rs.go index ec4d067..970b9d2 100644 --- a/cmd/commands/rs/rs.go +++ b/cmd/commands/rs/rs.go @@ -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": From 855ac34dd3c61a65a7076320c8db452d64ac901a Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sat, 18 Mar 2017 20:05:06 +0100 Subject: [PATCH 3/4] Makes config.LoadConfig() silent --- config/conf.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/conf.go b/config/conf.go index a69f2b8..eebbce2 100644 --- a/config/conf.go +++ b/config/conf.go @@ -99,7 +99,6 @@ func LoadConfig() { switch file.Name() { case "bee.json": { - beeLogger.Log.Info("Loading configuration from 'bee.json'...") err = parseJSON(filepath.Join(currentPath, file.Name()), &Conf) if err != nil { beeLogger.Log.Errorf("Failed to parse JSON file: %s", err) @@ -108,7 +107,6 @@ func LoadConfig() { } case "Beefile": { - beeLogger.Log.Info("Loading configuration from 'Beefile'...") err = parseYAML(filepath.Join(currentPath, file.Name()), &Conf) if err != nil { beeLogger.Log.Errorf("Failed to parse YAML file: %s", err) From 77a36964e5d41364d08f798ddf5060f0e10ea5a9 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sat, 18 Mar 2017 20:10:43 +0100 Subject: [PATCH 4/4] Adds Windows support for command execution --- cmd/commands/rs/rs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/commands/rs/rs.go b/cmd/commands/rs/rs.go index 970b9d2..bf6383e 100644 --- a/cmd/commands/rs/rs.go +++ b/cmd/commands/rs/rs.go @@ -87,7 +87,8 @@ func (c *customCommand) run() error { switch runtime.GOOS { case "darwin", "linux": cmd = exec.Command("sh", "-c", c.Command) - case "windows": //TODO + case "windows": + cmd = exec.Command("cmd", "/C", c.Command) } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr