Created
December 22, 2025 08:21
-
-
Save nikolaydubina/08b43d344920c6cf8a2cbaa316875af4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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