From fbc89c97ec147a2efec8e440a2110d7f9be49e5d Mon Sep 17 00:00:00 2001 From: BRO3886 Date: Thu, 12 Nov 2020 15:53:09 +0530 Subject: [PATCH] :fire: delete tasks added --- cmd/tasks.go | 72 +++++++++++++++++++++++++++++++++++++++++++++- utils/tasklists.go | 5 +--- utils/tasks.go | 6 ++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/cmd/tasks.go b/cmd/tasks.go index c5c5efb..c6e7bf1 100644 --- a/cmd/tasks.go +++ b/cmd/tasks.go @@ -234,11 +234,81 @@ var markCompletedCmd = &cobra.Command{ }, } +var deleteTaskCmd = &cobra.Command{ + Use: "rm", + Short: "Delete a task in a tasklist", + Long: ` + Use this command to delete a task in a tasklist + for the currently signed in account + `, + Run: func(cmd *cobra.Command, args []string) { + config := utils.ReadCredentials() + client := getClient(config) + + srv, err := tasks.New(client) + if err != nil { + log.Fatalf("Unable to retrieve tasks Client %v", err) + } + + list, err := utils.GetTaskLists(srv) + if err != nil { + log.Fatalf("Error %v", err) + } + + fmt.Println("Choose a Tasklist:") + var l []string + for _, i := range list { + l = append(l, i.Title) + } + + prompt := promptui.Select{ + Label: "Select Tasklist", + Items: l, + } + option, result, err := prompt.Run() + if err != nil { + color.Red("Error: " + err.Error()) + return + } + fmt.Printf("Tasks in %s:\n", result) + tID := list[option].Id + + tasks, err := utils.GetTasks(srv, tID, false) + if err != nil { + color.Red(err.Error()) + return + } + + tString := []string{} + for _, i := range tasks { + tString = append(tString, i.Title) + } + + prompt = promptui.Select{ + Label: "Select Task", + Items: tString, + } + option, result, err = prompt.Run() + if err != nil { + color.Red("Error: " + err.Error()) + return + } + t := tasks[option] + t.Status = "completed" + err = utils.DeleteTask(srv, t.Id, tID) + if err != nil { + color.Red("Unable to delete task: %v", err) + return + } + fmt.Printf("%s: %s\n", color.GreenString("Deleted"), t.Title) + }, +} + var showCompletedFlag bool func init() { viewTasksCmd.Flags().BoolVarP(&showCompletedFlag, "completed", "c", false, "use this flag to include completed tasks") - tasksCmd.AddCommand(viewTasksCmd, createTaskCmd, markCompletedCmd) + tasksCmd.AddCommand(viewTasksCmd, createTaskCmd, markCompletedCmd, deleteTaskCmd) rootCmd.AddCommand(tasksCmd) } diff --git a/utils/tasklists.go b/utils/tasklists.go index 9a9f5bd..451e08e 100644 --- a/utils/tasklists.go +++ b/utils/tasklists.go @@ -29,8 +29,5 @@ func UpdateTaskList(srv *tasks.Service, tl *tasks.TaskList) (*tasks.TaskList, er func DeleteTaskList(srv *tasks.Service, tID string) error { err := srv.Tasklists.Delete(tID).Do() - if err != nil { - return err - } - return nil + return err } diff --git a/utils/tasks.go b/utils/tasks.go index 87fe831..25afc95 100644 --- a/utils/tasks.go +++ b/utils/tasks.go @@ -45,3 +45,9 @@ func UpdateTask(srv *tasks.Service, t *tasks.Task, tListID string) (*tasks.Task, } return r, nil } + +//DeleteTask used to delete a task +func DeleteTask(srv *tasks.Service, id string, tid string) error { + err := srv.Tasks.Delete(tid, id).Do() + return err +}