| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// gocdk-docstore demonstrates the use of the Go CDK docstore package in a |
| 16 |
// simple command-line application. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"os" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/google/subcommands" |
| 28 |
"github.com/google/uuid" |
| 29 |
"gocloud.dev/docstore" |
| 30 |
|
| 31 |
// Import the docstore driver packages we want to be able to open. |
| 32 |
_ "gocloud.dev/docstore/awsdynamodb/v2" |
| 33 |
_ "gocloud.dev/docstore/gcpfirestore" |
| 34 |
_ "gocloud.dev/docstore/memdocstore" |
| 35 |
_ "gocloud.dev/docstore/mongodocstore" |
| 36 |
) |
| 37 |
|
| 38 |
const helpSuffix = ` |
| 39 |
|
| 40 |
See https://gocloud.dev/concepts/urls/ for more background on |
| 41 |
Go CDK URLs, and sub-packages under gocloud.dev/docstore |
| 42 |
(https://godoc.org/gocloud.dev/docstore#pkg-subdirectories) |
| 43 |
for details on the docstore.Collection URL format. |
| 44 |
` |
| 45 |
|
| 46 |
func main() { |
| 47 |
os.Exit(run()) |
| 48 |
} |
| 49 |
|
| 50 |
func run() int { |
| 51 |
subcommands.Register(subcommands.HelpCommand(), "") |
| 52 |
subcommands.Register(&listCmd{}, "") |
| 53 |
subcommands.Register(&putCmd{}, "") |
| 54 |
subcommands.Register(&updateCmd{}, "") |
| 55 |
subcommands.Register(&deleteCmd{}, "") |
| 56 |
flag.Parse() |
| 57 |
return int(subcommands.Execute(context.Background())) |
| 58 |
} |
| 59 |
|
| 60 |
// A Message is a document entry stored in a collection. |
| 61 |
type Message struct { |
| 62 |
ID string // unique ID of each document |
| 63 |
Date string |
| 64 |
Content string |
| 65 |
DocstoreRevision any |
| 66 |
} |
| 67 |
|
| 68 |
func (m Message) String() string { |
| 69 |
return fmt.Sprintf("%s %s: %s", m.ID, m.Date, m.Content) |
| 70 |
} |
| 71 |
|
| 72 |
type listCmd struct { |
| 73 |
date string |
| 74 |
} |
| 75 |
|
| 76 |
func (*listCmd) Name() string { return "ls" } |
| 77 |
func (*listCmd) Synopsis() string { return "List items in a collection" } |
| 78 |
func (*listCmd) Usage() string { |
| 79 |
return `ls [-d <date>] <collection URL> |
| 80 |
|
| 81 |
List the documents in <collection URL>. |
| 82 |
|
| 83 |
Example: |
| 84 |
gocdk-docstore ls -d "2006-01-02" "mongo://myDB/myCollection?id_field=ID"` + helpSuffix |
| 85 |
} |
| 86 |
|
| 87 |
func (cmd *listCmd) SetFlags(f *flag.FlagSet) { |
| 88 |
f.StringVar(&cmd.date, "d", "", "get the messages from this date, in the format YYYY-MM-DD") |
| 89 |
} |
| 90 |
|
| 91 |
func (cmd *listCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 92 |
if f.NArg() != 1 { |
| 93 |
f.Usage() |
| 94 |
return subcommands.ExitUsageError |
| 95 |
} |
| 96 |
collectionURL := f.Arg(0) |
| 97 |
|
| 98 |
// Open a *docstore.Collection using the collectionURL. |
| 99 |
collection, err := docstore.OpenCollection(ctx, collectionURL) |
| 100 |
if err != nil { |
| 101 |
fmt.Fprintf(os.Stderr, "Failed to open collection: %v\n", err) |
| 102 |
return subcommands.ExitFailure |
| 103 |
} |
| 104 |
defer collection.Close() |
| 105 |
|
| 106 |
q := collection.Query() |
| 107 |
if cmd.date != "" { |
| 108 |
q = q.Where("Date", "=", cmd.date) |
| 109 |
} |
| 110 |
iter := q.Get(ctx) |
| 111 |
defer iter.Stop() |
| 112 |
for { |
| 113 |
var msg Message |
| 114 |
err := iter.Next(ctx, &msg) |
| 115 |
if err == io.EOF { |
| 116 |
break |
| 117 |
} |
| 118 |
if err != nil { |
| 119 |
fmt.Fprintf(os.Stderr, "Failed to list: %v\n", err) |
| 120 |
return subcommands.ExitFailure |
| 121 |
} |
| 122 |
fmt.Println(msg) |
| 123 |
} |
| 124 |
return subcommands.ExitSuccess |
| 125 |
} |
| 126 |
|
| 127 |
type putCmd struct { |
| 128 |
id string // user-chosen ID |
| 129 |
date string // user-chosen date |
| 130 |
} |
| 131 |
|
| 132 |
func (*putCmd) Name() string { return "put" } |
| 133 |
func (*putCmd) Synopsis() string { return "Put an item from stdin" } |
| 134 |
func (*putCmd) Usage() string { |
| 135 |
return `put [-id <ID>] [-d <date>] <collection URL> <message> |
| 136 |
|
| 137 |
Read from stdin and put an message with the current timestamp in <collection URL>. |
| 138 |
|
| 139 |
Example: |
| 140 |
gocdk-docstore put "mongo://myDB/myCollection?id_field=ID" "hello docstore"` + helpSuffix |
| 141 |
} |
| 142 |
|
| 143 |
func (p *putCmd) SetFlags(f *flag.FlagSet) { |
| 144 |
f.StringVar(&p.id, "id", "", "ID of document") |
| 145 |
f.StringVar(&p.date, "d", "", "date of document") |
| 146 |
} |
| 147 |
|
| 148 |
func (p *putCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 149 |
if f.NArg() != 2 { |
| 150 |
f.Usage() |
| 151 |
return subcommands.ExitUsageError |
| 152 |
} |
| 153 |
collectionURL := f.Arg(0) |
| 154 |
content := f.Arg(1) |
| 155 |
|
| 156 |
// Open a *docstore.Collection using the collectionURL. |
| 157 |
collection, err := docstore.OpenCollection(ctx, collectionURL) |
| 158 |
if err != nil { |
| 159 |
fmt.Fprintf(os.Stderr, "Failed to open collection: %v\n", err) |
| 160 |
return subcommands.ExitFailure |
| 161 |
} |
| 162 |
defer collection.Close() |
| 163 |
|
| 164 |
if p.id == "" { |
| 165 |
p.id = uuid.New().String() |
| 166 |
} |
| 167 |
if p.date == "" { |
| 168 |
p.date = time.Now().Format("2006-01-02") |
| 169 |
} |
| 170 |
msg := &Message{ |
| 171 |
ID: p.id, |
| 172 |
Date: p.date, |
| 173 |
Content: content, |
| 174 |
} |
| 175 |
if err := collection.Put(ctx, msg); err != nil { |
| 176 |
fmt.Fprintf(os.Stderr, "Failed to put message: %v\n", err) |
| 177 |
return subcommands.ExitFailure |
| 178 |
} |
| 179 |
fmt.Printf("Put message: %s\n", msg) |
| 180 |
return subcommands.ExitSuccess |
| 181 |
} |
| 182 |
|
| 183 |
type updateCmd struct{} |
| 184 |
|
| 185 |
func (*updateCmd) Name() string { return "update" } |
| 186 |
func (*updateCmd) Synopsis() string { return "Update an item in a collection" } |
| 187 |
func (*updateCmd) Usage() string { |
| 188 |
return `update <ID> <collection URL> <updated message> |
| 189 |
|
| 190 |
Update the document with ID <ID> in <collection URL>. |
| 191 |
|
| 192 |
Example: |
| 193 |
gocdk-docstore update <ID> "mongo://myDB/myCollection?id_field=ID" "hello again"` + helpSuffix |
| 194 |
} |
| 195 |
|
| 196 |
func (*updateCmd) SetFlags(_ *flag.FlagSet) {} |
| 197 |
|
| 198 |
func (cmd *updateCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 199 |
if f.NArg() != 3 { |
| 200 |
f.Usage() |
| 201 |
return subcommands.ExitUsageError |
| 202 |
} |
| 203 |
id := f.Arg(0) |
| 204 |
collectionURL := f.Arg(1) |
| 205 |
updated := f.Arg(2) |
| 206 |
|
| 207 |
// Open a *docstore.Collection using the collectionURL. |
| 208 |
collection, err := docstore.OpenCollection(ctx, collectionURL) |
| 209 |
if err != nil { |
| 210 |
fmt.Fprintf(os.Stderr, "Failed to open collection: %v\n", err) |
| 211 |
return subcommands.ExitFailure |
| 212 |
} |
| 213 |
defer collection.Close() |
| 214 |
|
| 215 |
msg := &Message{ID: id} |
| 216 |
mods := docstore.Mods{"Content": updated} |
| 217 |
if errs := collection.Actions().Update(msg, mods).Get(msg).Do(ctx); errs != nil { |
| 218 |
fmt.Fprintf(os.Stderr, "Failed to update message: %v\n", errs) |
| 219 |
return subcommands.ExitFailure |
| 220 |
} |
| 221 |
fmt.Printf("updated: %s\n", msg) |
| 222 |
return subcommands.ExitSuccess |
| 223 |
} |
| 224 |
|
| 225 |
type deleteCmd struct { |
| 226 |
date string |
| 227 |
} |
| 228 |
|
| 229 |
func (*deleteCmd) Name() string { return "delete" } |
| 230 |
func (*deleteCmd) Synopsis() string { return "Delete items in a collection" } |
| 231 |
func (*deleteCmd) Usage() string { |
| 232 |
return `delete [-d <date>] <collection URL> |
| 233 |
|
| 234 |
Delete the documents in <collection URL>. |
| 235 |
|
| 236 |
Example: |
| 237 |
gocdk-docstore delete -d 2006-01-02 mongo://myDB/myCollection?id_field=ID` + helpSuffix |
| 238 |
} |
| 239 |
|
| 240 |
func (cmd *deleteCmd) SetFlags(f *flag.FlagSet) { |
| 241 |
f.StringVar(&cmd.date, "d", "", "delete the messages from this date, in the format YYYY-MM-DD") |
| 242 |
} |
| 243 |
|
| 244 |
func (cmd *deleteCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 245 |
if f.NArg() != 1 { |
| 246 |
f.Usage() |
| 247 |
return subcommands.ExitUsageError |
| 248 |
} |
| 249 |
collectionURL := f.Arg(0) |
| 250 |
|
| 251 |
// Open a *docstore.Collection using the collectionURL. |
| 252 |
collection, err := docstore.OpenCollection(ctx, collectionURL) |
| 253 |
if err != nil { |
| 254 |
fmt.Fprintf(os.Stderr, "Failed to open collection: %v\n", err) |
| 255 |
return subcommands.ExitFailure |
| 256 |
} |
| 257 |
defer collection.Close() |
| 258 |
|
| 259 |
q := collection.Query() |
| 260 |
if cmd.date != "" { |
| 261 |
q = q.Where("Date", "=", cmd.date) |
| 262 |
} |
| 263 |
iter := q.Get(ctx, "ID") |
| 264 |
dels := collection.Actions() |
| 265 |
for { |
| 266 |
var msg Message |
| 267 |
err := iter.Next(ctx, &msg) |
| 268 |
if err == io.EOF { |
| 269 |
break |
| 270 |
} |
| 271 |
if err != nil { |
| 272 |
return subcommands.ExitFailure |
| 273 |
} |
| 274 |
dels.Delete(&msg) |
| 275 |
} |
| 276 |
if err := dels.Do(ctx); err != nil { |
| 277 |
fmt.Fprintf(os.Stderr, "Failed to delete: %v\n", err) |
| 278 |
return subcommands.ExitFailure |
| 279 |
} |
| 280 |
return subcommands.ExitSuccess |
| 281 |
} |