Skip to content

Commit

Permalink
Added projects listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Matija Pevec committed Jan 11, 2020
1 parent 9465e4f commit 17b6ec7
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 7 deletions.
8 changes: 4 additions & 4 deletions client/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package client
import "fmt"

type Issue struct {
Id int64 `json:"id"`
Project ProjectBasic `json:"project"`
Subject string `json:"subject"`
Description string `json:"description"`
Id int64 `json:"id"`
Project Entity `json:"project"`
Subject string `json:"subject"`
Description string `json:"description"`
}

type IssueResponse struct {
Expand Down
57 changes: 56 additions & 1 deletion client/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
package client

type ProjectBasic struct {
import (
"fmt"
"time"
)

type Project struct {
Id int64 `json:"id"`
Name string `json:"name"`
Identifier string `json:"identifier"`
Description string `json:"description"`
Status int `json:"status"`
CreatedOn time.Time `json:"created_on"`
Parent *Entity `json:"parent"`
}

type Parent struct{}

type ProjectsResponse struct {
Projects []Project `json:"projects"`
}

type ProjectResponse struct {
Project Project `json:"project"`
}

type Entity struct {
Id int64 `json:"id"`
Name string `json:"name"`
}

func (c *Client) GetProject(id int64) (*Project, error) {
req, err := c.getRequest(fmt.Sprintf("/projects/%v.json", id), "")
if err != nil {
return nil, err
}

var response ProjectResponse
_, err = c.Do(req, &response)
if err != nil {
return nil, err
}

return &response.Project, nil
}

func (c *Client) GetProjects() ([]Project, error) {
req, err := c.getRequest("/projects.json", "")
if err != nil {
return nil, err
}

var response ProjectsResponse
_, err = c.Do(req, &response)
if err != nil {
return nil, err
}

return response.Projects, nil
}
5 changes: 3 additions & 2 deletions cmd/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var issuesCmd = &cobra.Command{

var myIssuesCmd = &cobra.Command{
Use: "my",
Aliases: []string{"assigned"},
Aliases: []string{"assigned", "all"},
Short: "List all issues assigned to the user.",
Run: func(cmd *cobra.Command, args []string) {
issues, err := RClient.GetMyIssues()
Expand Down Expand Up @@ -84,7 +84,8 @@ func IssueFunc(_ *cobra.Command, args []string) {
issueId, _ := strconv.ParseInt(args[0], 10, 64)
issue, err := RClient.GetIssue(issueId)
if err != nil {
log.Fatal("Cannot fetch issue", err)
fmt.Printf("Cannot fetch issue with id %v\n", issueId)
return
}

fmt.Printf("[%v] %v\n", text.FgYellow.Sprint(issue.Id), text.FgYellow.Sprint(issue.Project.Name))
Expand Down
81 changes: 81 additions & 0 deletions cmd/projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cmd

import (
"fmt"
"log"
"strconv"

"github.com/jedib0t/go-pretty/text"

"github.com/mightymatth/arcli/client"

"github.com/spf13/cobra"
)

var projectsCmd = &cobra.Command{
Use: "projects [id]",
Args: ValidProjectArgs(),
Aliases: []string{"tasks", "show"},
Short: "Shows project details.",
Run: ProjectFunc,
}

var myProjectsCmd = &cobra.Command{
Use: "my",
Aliases: []string{"all"},
Short: "List all projects visible to user.",
Run: func(cmd *cobra.Command, args []string) {
projects, err := RClient.GetProjects()
if err != nil {
log.Fatal("Cannot fetch projects", err)
}

drawProjects(projects)
},
}

func init() {
rootCmd.AddCommand(projectsCmd)
projectsCmd.AddCommand(myProjectsCmd)
}

func drawProjects(projects []client.Project) {
for _, project := range projects {
if project.Parent == nil {
fmt.Printf("[%v] %v\n", text.FgYellow.Sprint(project.Id),
text.FgYellow.Sprint(project.Name))
} else {
fmt.Printf(" ‣ [%v] %v\n", text.FgCyan.Sprint(project.Id),
text.FgCyan.Sprint(project.Name))
}

}
}

func ValidProjectArgs() cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
err := cobra.ExactArgs(1)(cmd, args)
if err != nil {
return err
}

_, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
return fmt.Errorf("project id must be integer")
}
return nil
}
}

func ProjectFunc(_ *cobra.Command, args []string) {
projectId, _ := strconv.ParseInt(args[0], 10, 64)
project, err := RClient.GetProject(projectId)
if err != nil {
fmt.Printf("Cannot fetch project with id %v\n", projectId)
return
}

fmt.Printf("[%v] %v\n", text.FgYellow.Sprint(project.Id), text.FgYellow.Sprint(project.Identifier))
fmt.Printf("%v\n", text.FgGreen.Sprint(project.Name))
fmt.Printf("%v\n", project.Description)
}

0 comments on commit 17b6ec7

Please sign in to comment.