Skip to content

Commit

Permalink
fix project
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 10, 2019
1 parent 168e5aa commit 15e414d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
99 changes: 58 additions & 41 deletions cmd/todoist/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var projectAddCmd = &cobra.Command{
}
project.Color = color
}
/* FIXME
indentStr, err := cmd.Flags().GetString("indent")
if err != nil {
return errors.New("Invalid project indent")
Expand All @@ -67,6 +68,7 @@ var projectAddCmd = &cobra.Command{
}
project.Indent = i
}
*/
if _, err = client.Project.Add(project); err != nil {
return err
}
Expand Down Expand Up @@ -122,17 +124,19 @@ var projectUpdateCmd = &cobra.Command{
}
project.Color = color
}
indentStr, err := cmd.Flags().GetString("indent")
if err != nil {
return errors.New("Invalid project indent")
}
if len(indentStr) > 0 {
i, err := strconv.Atoi(indentStr)
/*
indentStr, err := cmd.Flags().GetString("indent")
if err != nil {
return fmt.Errorf("Invalid project indent: %s", indentStr)
return errors.New("Invalid project indent")
}
project.Indent = i
}
if len(indentStr) > 0 {
i, err := strconv.Atoi(indentStr)
if err != nil {
return fmt.Errorf("Invalid project indent: %s", indentStr)
}
project.Indent = i
}
*/
if _, err = client.Project.Update(*project); err != nil {
return err
}
Expand All @@ -154,32 +158,31 @@ var projectUpdateCmd = &cobra.Command{
}

var projectDeleteCmd = &cobra.Command{
Use: "delete id [...]",
Short: "delete projects",
Use: "delete id",
Short: "delete project",
RunE: func(cmd *cobra.Command, args []string) error {
if err := util.AutoCommit(func(client todoist.Client, ctx context.Context) error {
return util.ProcessIDs(
args,
func(ids []todoist.ID) error {
var projects []todoist.Project
for _, id := range ids {
project := client.Project.Resolve(id)
if project == nil {
return fmt.Errorf("invalid id: %s", id)
}
projects = append(projects, *project)
}
fmt.Println(util.ProjectTableString(projects))
if len(args) != 1 {
return fmt.Errorf("require one project id")
}
id, err := todoist.NewID(args[0])
if err != nil {
return err
}
project := client.Project.Resolve(id)
if project == nil {
return fmt.Errorf("invalid id: %s", id)
}
fmt.Println(util.ProjectTableString([]todoist.Project{*project}))

reader := bufio.NewReader(os.Stdin)
fmt.Print("are you sure to delete above project(s)? (y/[n]): ")
ans, err := reader.ReadString('\n')
if ans != "y\n" || err != nil {
fmt.Println("abort")
return errors.New("abort")
}
return client.Project.Delete(ids)
})
reader := bufio.NewReader(os.Stdin)
fmt.Print("are you sure to delete above project(s)? (y/[n]): ")
ans, err := reader.ReadString('\n')
if ans != "y\n" || err != nil {
fmt.Println("abort")
return errors.New("abort")
}
return client.Project.Delete(id)
}); err != nil {
if err.Error() == "abort" {
return nil
Expand All @@ -192,11 +195,18 @@ var projectDeleteCmd = &cobra.Command{
}

var projectArchiveCmd = &cobra.Command{
Use: "archive id [...]",
Short: "archive projects",
Use: "archive id",
Short: "archive project",
RunE: func(cmd *cobra.Command, args []string) error {
if err := util.AutoCommit(func(client todoist.Client, ctx context.Context) error {
return util.ProcessIDs(args, client.Project.Archive)
if len(args) != 1 {
return fmt.Errorf("require one project id")
}
id, err := todoist.NewID(args[0])
if err != nil {
return err
}
return client.Project.Archive(id)
}); err != nil {
return err
}
Expand All @@ -206,11 +216,18 @@ var projectArchiveCmd = &cobra.Command{
}

var projectUnarchiveCmd = &cobra.Command{
Use: "unarchive id [...]",
Short: "unarchive projects",
Use: "unarchive id",
Short: "unarchive project",
RunE: func(cmd *cobra.Command, args []string) error {
if err := util.AutoCommit(func(client todoist.Client, ctx context.Context) error {
return util.ProcessIDs(args, client.Project.Unarchive)
if len(args) != 1 {
return fmt.Errorf("require one project id")
}
id, err := todoist.NewID(args[0])
if err != nil {
return err
}
return client.Project.Unarchive(id)
}); err != nil {
return err
}
Expand All @@ -222,11 +239,11 @@ var projectUnarchiveCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(projectCmd)
projectCmd.AddCommand(projectListCmd)
projectAddCmd.Flags().StringP("color", "c", "7", "color")
projectAddCmd.Flags().StringP("indent", "i", "1", "indent")
projectAddCmd.Flags().StringP("color", "c", "47", "color")
// FIXME: projectAddCmd.Flags().StringP("indent", "i", "1", "indent")
projectCmd.AddCommand(projectAddCmd)
projectUpdateCmd.Flags().StringP("color", "c", "", "color")
projectUpdateCmd.Flags().StringP("indent", "i", "", "indent")
// FIXME: projectUpdateCmd.Flags().StringP("indent", "i", "", "indent")
projectCmd.AddCommand(projectUpdateCmd)
projectCmd.AddCommand(projectDeleteCmd)
projectCmd.AddCommand(projectArchiveCmd)
Expand Down
2 changes: 1 addition & 1 deletion cmd/util/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ItemTableString(items []todoist.Item, relations todoist.ItemRelations, f fu

func ProjectTableString(projects []todoist.Project) string {
sort.Slice(projects, func(i, j int) bool {
return projects[i].ItemOrder < projects[j].ItemOrder
return projects[i].ChildOrder < projects[j].ChildOrder
})
var rows [][]todoist.ColorStringer
for _, p := range projects {
Expand Down
24 changes: 12 additions & 12 deletions todoist/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Project struct {
Entity
Name string `json:"name"`
Color int `json:"color"`
Indent int `json:"indent"`
ItemOrder int `json:"item_order"`
ChildOrder int `json:"child_order"`
ParentID ID `json:"parent_id"` // FIXME: nullable
Collapsed int `json:"collapsed"`
Shared bool `json:"shared"`
IsArchived int `json:"is_archived"`
Expand All @@ -24,7 +24,7 @@ type Project struct {
}

func (p Project) String() string {
return strings.Repeat(" ", p.Indent-1) + "#" + p.Name
return /* FIXME: strings.Repeat(" ", p.Indent-1) + */ "#" + p.Name
}

func (p Project) ColorString() string {
Expand Down Expand Up @@ -84,36 +84,36 @@ func (c *ProjectClient) Update(project Project) (*Project, error) {
return &project, nil
}

func (c *ProjectClient) Delete(ids []ID) error {
func (c *ProjectClient) Delete(id ID) error {
command := Command{
Type: "project_delete",
UUID: GenerateUUID(),
Args: map[string][]ID{
"ids": ids,
Args: map[string]ID{
"id": id,
},
}
c.queue = append(c.queue, command)
return nil
}

func (c *ProjectClient) Archive(ids []ID) error {
func (c *ProjectClient) Archive(id ID) error {
command := Command{
Type: "project_archive",
UUID: GenerateUUID(),
Args: map[string][]ID{
"ids": ids,
Args: map[string]ID{
"id": id,
},
}
c.queue = append(c.queue, command)
return nil
}

func (c *ProjectClient) Unarchive(ids []ID) error {
func (c *ProjectClient) Unarchive(id ID) error {
command := Command{
Type: "project_unarchive",
UUID: GenerateUUID(),
Args: map[string][]ID{
"ids": ids,
Args: map[string]ID{
"id": id,
},
}
c.queue = append(c.queue, command)
Expand Down

0 comments on commit 15e414d

Please sign in to comment.