-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matija Pevec
committed
Jan 11, 2020
1 parent
9465e4f
commit 17b6ec7
Showing
4 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |