-
Notifications
You must be signed in to change notification settings - Fork 28
/
set.go
85 lines (69 loc) · 1.59 KB
/
set.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func setData(
client *firestore.Client,
collectionPath string,
documentPath string,
id string,
data string,
merge bool) error {
object, err := unmarshallData(data)
if err != nil {
return err
}
transformExtendedJsonMapToFirestoreMap(object, client)
var options []firestore.SetOption
if merge {
options = append(options, firestore.MergeAll)
}
if collectionPath != "" {
_, err = client.
Collection(collectionPath).
Doc(id).
Set(context.Background(), object, options...)
} else {
_, err = client.
Doc(documentPath).
Set(context.Background(), object, options...)
}
if err != nil {
return err
}
return nil
}
func setCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength < 2 || argsLength > 3 {
return cli.NewExitError("Wrong number of arguments", 85)
}
merge := c.Bool("merge")
var collectionPath, id, data, documentPath string
if argsLength == 3 {
collectionPath = c.Args().First()
id = c.Args().Get(1)
data = c.Args().Get(2)
} else {
documentPath = c.Args().First()
data = c.Args().Get(1)
}
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
err = setData(client, collectionPath, documentPath, id, data, merge)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to write data. \n%v", err), 85)
}
if collectionPath != "" {
fmt.Fprintf(c.App.Writer, "%v\n", id)
} else {
fmt.Fprintf(c.App.Writer, "%v\n", documentPath)
}
defer client.Close()
return nil
}