Skip to content

Commit

Permalink
confirm when deleting projects
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 17, 2018
1 parent 942eddb commit 86d315d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion cmd/todoist/cmd/project.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"bufio"
"context"
"errors"
"fmt"
"github.com/kobtea/go-todoist/cmd/util"
"github.com/kobtea/go-todoist/todoist"
"github.com/spf13/cobra"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -156,8 +158,32 @@ var projectDeleteCmd = &cobra.Command{
Short: "delete projects",
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.Delete)
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))

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)
})
}); err != nil {
if err.Error() == "abort" {
return nil
}
return err
}
fmt.Println("Successful deleting of project(s).")
Expand Down

0 comments on commit 86d315d

Please sign in to comment.