-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.go
78 lines (63 loc) · 1.5 KB
/
documents.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
// Copyright 2023 The firestore Authors
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"bufio"
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"google.golang.org/api/iterator"
)
// Docss represents a docs subcommand.
type Docs struct {
*cli
}
var _ Command = (*Docs)(nil)
// Register implements Command.
func (ds *Docs) Register(cmd *cobra.Command) {
cmd.AddCommand(ds.NewCommand())
}
// NewCommand implements Command.
func (ds *Docs) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "docs",
Aliases: []string{"ds"},
Short: "List the document",
Long: "List the document",
RunE: func(cmd *cobra.Command, args []string) error {
if err := checkArgs(cmd.Name(), exactArgs, 1, args...); err != nil {
return err
}
return ds.Run(cmd.Context(), args)
},
}
return cmd
}
// Run implements Command.
func (c *cli) Run(ctx context.Context, args []string) error {
collPath := args[0]
if collPath[len(collPath)-1] == '/' {
return fmt.Errorf("invalid collection path: %q", collPath)
}
coll := c.fs.Collection(collPath)
if coll == nil {
return fmt.Errorf("not found %s documents", collPath)
}
iter := coll.DocumentRefs(ctx)
w := bufio.NewWriter(c.Out)
for {
ref, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.Done) {
break
}
return fmt.Errorf("get next iterator result: %w", err)
}
w.WriteString(ref.ID + "\n")
}
if err := w.Flush(); err != nil {
return fmt.Errorf("flush stdout I/O: %w", err)
}
return nil
}