-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
62 lines (55 loc) · 1.33 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"github.com/spf13/cobra"
)
var deleteCommand = cobra.Command{
Use: "delete",
Short: "Delete users or books",
}
var deleteBooksCommand = cobra.Command{
Use: "books IDS...",
Short: "Delete a books, pages or lines",
Args: cobra.MinimumNArgs(1),
RunE: deleteBooks,
}
func deleteBooks(_ *cobra.Command, args []string) error {
c := authenticate()
for _, id := range args {
var bid, pid, lid int
var url string
switch n := parseIDs(id, &bid, &pid, &lid); n {
case 3:
url = c.URL("books/%d/pages/%d/lines/%d", bid, pid, lid)
case 2:
url = c.URL("books/%d/pages/%d", bid, pid)
case 1:
url = c.URL("books/%d", bid)
default:
return fmt.Errorf("delete book: invalid id: %q", id)
}
if err := delete(c, url, nil); err != nil {
return fmt.Errorf("delete book %s: %v", id, err)
}
}
return nil
}
var deleteUsersCommand = cobra.Command{
Use: "users IDS...",
Short: "delete users",
Args: cobra.MinimumNArgs(1),
RunE: deleteUsers,
}
func deleteUsers(_ *cobra.Command, args []string) error {
c := authenticate()
for _, id := range args {
var uid int
if n := parseIDs(id, &uid); n != 1 {
return fmt.Errorf("delete user: invalid user id: %s", id)
}
if err := delete(c, c.URL("users/%d", uid), nil); err != nil {
return fmt.Errorf("delete user: %v", err)
}
}
return nil
}