Skip to content

Instantly share code, notes, and snippets.

@nikolaydubina
Created December 22, 2025 08:21
Show Gist options
  • Select an option

  • Save nikolaydubina/08b43d344920c6cf8a2cbaa316875af4 to your computer and use it in GitHub Desktop.

Select an option

Save nikolaydubina/08b43d344920c6cf8a2cbaa316875af4 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"cloud.google.com/go/firestore"
)
func main() {
var project, collection, docID, field string
flag.StringVar(&project, "project", os.Getenv("PROJECT_ID"), "GCP project ID")
flag.StringVar(&collection, "collection", "", "Firestore collection")
flag.StringVar(&docID, "doc", "", "document id")
flag.StringVar(&field, "field", "", "field path to extract")
flag.Parse()
if project == "" || collection == "" {
flag.Usage()
os.Exit(1)
}
ctx := context.Background()
db, err := firestore.NewClient(ctx, project)
if err != nil {
log.Fatal(err)
}
defer db.Close()
doc, err := db.Collection(collection).Doc(docID).Get(ctx)
if err != nil {
log.Fatal(err)
}
val, err := doc.DataAt(field)
if err != nil {
log.Fatal(err)
}
switch v := val.(type) {
case []byte:
os.Stdout.Write(v)
default:
if err := json.NewEncoder(os.Stdout).Encode(val); err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment