Skip to content

Commit

Permalink
🔥 delete tasks added
Browse files Browse the repository at this point in the history
  • Loading branch information
BRO3886 committed Nov 12, 2020
1 parent 156278d commit fbc89c9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
72 changes: 71 additions & 1 deletion cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 1 addition & 4 deletions utils/tasklists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions utils/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fbc89c9

Please sign in to comment.