This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpubsubDir.go
68 lines (59 loc) · 1.5 KB
/
pubsubDir.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
package gcp
import (
"context"
"cloud.google.com/go/pubsub"
"github.com/puppetlabs/wash/plugin"
"google.golang.org/api/iterator"
)
type pubsubDir struct {
plugin.EntryBase
client *pubsub.Client
}
func newPubsubDir(ctx context.Context, projID string) (*pubsubDir, error) {
cli, err := pubsub.NewClient(context.Background(), projID)
if err != nil {
return nil, err
}
p := &pubsubDir{
EntryBase: plugin.NewEntry("pubsub"),
client: cli,
}
if _, err := plugin.List(ctx, p); err != nil {
p.MarkInaccessible(ctx, err)
}
return p, nil
}
// List all topics as dirs
func (p *pubsubDir) List(ctx context.Context) ([]plugin.Entry, error) {
topics := make([]plugin.Entry, 0)
it := p.client.Topics(ctx)
for {
t, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
topics = append(topics, newPubsubTopic(p.client, t))
}
return topics, nil
}
func (p *pubsubDir) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(p, "pubsub").
IsSingleton().
SetDescription(pubsubDirDescription)
}
func (p *pubsubDir) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{
(&pubsubTopic{}).Schema(),
}
}
const pubsubDirDescription = `
This directory represents Cloud Pub/Sub. Its entries consist of Pub/Sub topics.
You can publish a message to a topic by appending text to the topic file. For example
wash gcp/project/pubsub > tail -f topic &
wash gcp/project/pubsub > echo hello >> topic
===> my-topic <===
Nov 21 00:25:14.633 | hello
`