From 1e2cb13e7c8764500c67274672539bfaa30f8626 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 12 Aug 2014 22:14:25 -0700 Subject: [PATCH] added `drone status` command to query latest build for a repo from cli --- client/commits.go | 8 ++++---- cmd/main.go | 1 + cmd/status.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/util.go | 5 +---- 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 cmd/status.go diff --git a/client/commits.go b/client/commits.go index 96b9fa9c..5928987c 100644 --- a/client/commits.go +++ b/client/commits.go @@ -13,7 +13,7 @@ type CommitService struct { // GET /v1/repos/{host}/{owner}/{name}/branch/{branch}/commit/{commit} func (s *CommitService) Get(host, owner, name, branch, sha string) (*model.Commit, error) { - var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branch/%s/commit/%s", host, owner, name, branch, sha) + var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branches/%s/commits/%s", host, owner, name, branch, sha) var commit = model.Commit{} var err = s.run("GET", path, nil, &commit) return &commit, err @@ -21,7 +21,7 @@ func (s *CommitService) Get(host, owner, name, branch, sha string) (*model.Commi // GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}/console func (s *CommitService) GetOutput(host, owner, name, branch, sha string) (io.ReadCloser, error) { - var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branch/%s/commit/%s/console", host, owner, name, branch, sha) + var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branches/%s/commits/%s/console", host, owner, name, branch, sha) resp, err := s.do("GET", path) if err != nil { return nil, nil @@ -31,7 +31,7 @@ func (s *CommitService) GetOutput(host, owner, name, branch, sha string) (io.Rea // POST /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}?action=rebuild func (s *CommitService) Rebuild(host, owner, name, branch, sha string) error { - var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branch/%s/commit/%s", host, owner, name, branch, sha) + var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branches/%s/commits/%s", host, owner, name, branch, sha) return s.run("POST", path, nil, nil) } @@ -45,7 +45,7 @@ func (s *CommitService) List(host, owner, name string) ([]*model.Commit, error) // GET /v1/repos/{host}/{owner}/{name}/branch/{branch} func (s *CommitService) ListBranch(host, owner, name, branch string) ([]*model.Commit, error) { - var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branch/%s", host, owner, name, branch) + var path = fmt.Sprintf("/v1/repos/%s/%s/%s/branches/%s/commits", host, owner, name, branch) var list []*model.Commit var err = s.run("GET", path, nil, &list) return list, err diff --git a/cmd/main.go b/cmd/main.go index 1902436f..d051834c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -34,6 +34,7 @@ func main() { app.Commands = []cli.Command{ NewBuildCommand(), NewReposCommand(), + NewStatusCommand(), NewEnableCommand(), NewDisableCommand(), NewRestartCommand(), diff --git a/cmd/status.go b/cmd/status.go new file mode 100644 index 00000000..7f3412f9 --- /dev/null +++ b/cmd/status.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + + "github.com/codegangsta/cli" + "github.com/drone/drone/client" +) + +// NewStatusCommand returns the CLI command for "status". +func NewStatusCommand() cli.Command { + return cli.Command{ + Name: "status", + Usage: "display a repository build status", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "b, branch", + Usage: "branch to display", + }, + }, + Action: func(c *cli.Context) { + handle(c, statusCommandFunc) + }, + } +} + +// statusCommandFunc executes the "status" command. +func statusCommandFunc(c *cli.Context, client *client.Client) error { + var host, owner, repo, branch string + var args = c.Args() + + if len(args) != 0 { + host, owner, repo = parseRepo(args[0]) + } + + if c.IsSet("branch") { + branch = c.String("branch") + } else { + branch = "master" + } + + builds, err := client.Commits.ListBranch(host, owner, repo, branch) + if err != nil { + return err + } else if len(builds) == 0 { + return nil + } + + var build = builds[len(builds)-1] + fmt.Printf("%s\t%s\t%s\t%s\t%v", build.Status, build.ShaShort(), build.Timestamp, build.Author, build.Message) + return nil +} diff --git a/cmd/util.go b/cmd/util.go index 1208d288..f98a88c3 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -11,10 +11,7 @@ import ( func parseRepo(str string) (host, owner, repo string) { var parts = strings.Split(str, "/") - if len(repo) != 3 { - host = "undefined" - owner = "undefined" - repo = "undefined" + if len(parts) != 3 { return } host = parts[0]