mirror of
https://github.com/imjasonh/gcsbuf
synced 2026-07-08 00:55:41 +00:00
add test binary, observability
Signed-off-by: Jason Hall <imjasonh@gmail.com>
This commit is contained in:
parent
eeb0713f5a
commit
f6fd234e2d
6 changed files with 235 additions and 36 deletions
72
cmd/gcscat/main.go
Normal file
72
cmd/gcscat/main.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/storage"
|
||||
buf "github.com/imjasonh/gcsbuf"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "usage: gcscat <bucket> <object>\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
bucket, object := os.Args[1], os.Args[2]
|
||||
|
||||
ctx := context.Background()
|
||||
client, err := storage.NewClient(ctx)
|
||||
if err != nil {
|
||||
slog.Error("creating storage client", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
onOp := buf.OnOp(func(op string, d time.Duration, err error) {
|
||||
if err != nil {
|
||||
slog.Error("gcs", "op", op, "dur", d, "err", err)
|
||||
} else {
|
||||
slog.Info("gcs", "op", op, "dur", d)
|
||||
}
|
||||
})
|
||||
|
||||
w := buf.NewWriter(ctx, buf.WriterOptions{
|
||||
Client: client,
|
||||
Bucket: bucket,
|
||||
Object: object,
|
||||
OnOp: onOp,
|
||||
})
|
||||
|
||||
r := buf.NewReader(ctx, buf.ReaderOptions{
|
||||
Client: client,
|
||||
Bucket: bucket,
|
||||
Object: object,
|
||||
OnOp: onOp,
|
||||
})
|
||||
|
||||
// Read concurrently, printing to stdout.
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
if _, err := io.Copy(os.Stdout, r); err != nil {
|
||||
slog.Error("read", "err", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Write stdin to GCS.
|
||||
if _, err := io.Copy(w, os.Stdin); err != nil {
|
||||
slog.Error("write", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
slog.Error("close writer", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue