-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdelete.go
40 lines (33 loc) · 879 Bytes
/
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
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func deleteCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength < 1 || argsLength > 2 {
return cli.NewExitError("Wrong number of arguments", 82)
}
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
var documentRef *firestore.DocumentRef
if argsLength == 2 {
collectionPath := c.Args().First()
id := c.Args().Get(1)
collectionRef := client.Collection(collectionPath)
documentRef = collectionRef.Doc(id)
} else {
documentPath := c.Args().First()
documentRef = client.Doc(documentPath)
}
res, err := documentRef.Delete(context.Background())
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to delete data. \n%v", res), 82)
}
defer client.Close()
return nil
}