From a4d34379f7bc32adda04be021d52eb27ba5ef2c9 Mon Sep 17 00:00:00 2001 From: at15 Date: Fri, 15 Jan 2016 17:33:02 +0800 Subject: [PATCH] Add git status command #13 - FIXME: cant get the std error when git command is not executed correctly --- Ayi.go | 14 ++++++++------ app/command.go | 28 ++++++++++++++++++++++++++-- app/git/wrapper.go | 29 ++++++++++++++++++++++++++++- sys/command.go | 4 ++-- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/Ayi.go b/Ayi.go index 46267c5..7c5d1c9 100644 --- a/Ayi.go +++ b/Ayi.go @@ -4,15 +4,16 @@ import ( "os" "github.com/codegangsta/cli" + "github.com/dyweb/Ayi/app" "github.com/dyweb/Ayi/sys" "github.com/dyweb/Ayi/util" ) func main() { - app := cli.NewApp() - app.Name = "Ayi" - app.Usage = "Let Ayi do it for you" - app.Commands = []cli.Command{ + application := cli.NewApp() + application.Name = "Ayi" + application.Usage = "Let Ayi do it for you" + application.Commands = []cli.Command{ { // Ayi like roast mie Name: "mie", @@ -24,7 +25,8 @@ func main() { }, util.DummyCommand, util.ServeStaticCommand, - sys.HostCommand, + sys.HostCommands, + app.GitCommands, } - app.Run(os.Args) + application.Run(os.Args) } diff --git a/app/command.go b/app/command.go index 07617de..1cb6b6e 100644 --- a/app/command.go +++ b/app/command.go @@ -1,5 +1,29 @@ package app import ( -// "github.com/dyweb/Ayi/app/git" -) \ No newline at end of file + "github.com/codegangsta/cli" + "github.com/dyweb/Ayi/app/git" + "log" +) + +// GitCommands wrap common git operations with config defined in Ayi +var GitCommands = cli.Command{ + Name: "git", + Aliases: []string{"g"}, + Usage: "git command wrapper", + Subcommands: []cli.Command{ + { + Name: "status", + Aliases: []string{"s"}, + Usage: "git status", + Action: func(c *cli.Context) { + out, err, e := git.Status{}.Execute() + if e != nil { + println(err) + log.Fatal(e) + } + println(out) + }, + }, + }, +} diff --git a/app/git/wrapper.go b/app/git/wrapper.go index 48d706b..f485492 100644 --- a/app/git/wrapper.go +++ b/app/git/wrapper.go @@ -1,3 +1,30 @@ package git -// wrap git command for clone +import ( + "os/exec" +) + +var cmdName = "git" + +// simpleCommand is a interface execute and return output as string +type simpleCommand interface { + Execute() (stdOut string, stdErr string, err error) +} + +// Status execute git status command and return output +type Status struct { +} + +// Execute execute git status and return output as string +func (s Status) Execute() (stdOut string, stdErr string, err error) { + var cmdOut []byte + err = nil + cmdArgs := []string{"status"} + cmdOut, err = exec.Command(cmdName, cmdArgs...).Output() + // FIXME: cant get correct error output + if err != nil{ + stdErr = err.Error() + } + // TODO: get return code? + return string(cmdOut), stdErr, err +} diff --git a/sys/command.go b/sys/command.go index 7a68e53..edf0668 100644 --- a/sys/command.go +++ b/sys/command.go @@ -6,8 +6,8 @@ import ( "github.com/go-errors/errors" ) -// HostCommand for list and modify host file -var HostCommand = cli.Command{ +// HostCommands for list and modify host file +var HostCommands = cli.Command{ Name: "hosts", Aliases: []string{"host"}, Usage: "config/show host",