mirror of
https://github.com/hanzokms/cli.git
synced 2026-10-30 20:30:00 +00:00
The gateway test started redis:8.4.0 through the testcontainers redis module and connected with go-redis. It now starts ghcr.io/hanzoai/kv and connects with github.com/hanzokv/go, so the e2e suite exercises the server we ship. The client is built from the container's host and mapped port rather than from its connection string. Our ParseURL accepts kv:// only, and a mapped port is host:port anyway — the URL was a detour. reset_redis.go is reset_kv.go; the PAM resource type is left as the generated client names it, because it names the external system being connected to, alongside kubernetes. Verified: go build ./... and go vet ./relay/... ./packages/... both clean.
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
kv "github.com/hanzokv/go/v9"
|
|
)
|
|
|
|
// KVConfig holds KV connection configuration
|
|
type KVConfig struct {
|
|
Host string
|
|
Port int
|
|
Password string
|
|
}
|
|
|
|
// DefaultKVConfig returns the default KV configuration
|
|
func DefaultKVConfig() KVConfig {
|
|
return KVConfig{
|
|
Host: "localhost",
|
|
Port: 6379,
|
|
Password: "",
|
|
}
|
|
}
|
|
|
|
// ResetKVOptions holds options for resetting KV
|
|
type ResetKVOptions struct {
|
|
KVConfig KVConfig
|
|
}
|
|
|
|
// DefaultResetKVOptions returns default options for resetting KV
|
|
func DefaultResetKVOptions() ResetKVOptions {
|
|
return ResetKVOptions{
|
|
KVConfig: DefaultKVConfig(),
|
|
}
|
|
}
|
|
|
|
// ResetKV resets the KV database by flushing all keys.
|
|
// It accepts a port provider to get service ports, and options to configure the reset behavior.
|
|
func ResetKV(ctx context.Context, opts ...func(*ResetKVOptions)) error {
|
|
options := DefaultResetKVOptions()
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
return resetKVDB(ctx, options)
|
|
}
|
|
|
|
// resetKVDB resets the KV database by flushing all keys.
|
|
func resetKVDB(ctx context.Context, opts ResetKVOptions) error {
|
|
slog.Info("Resetting Postgres database")
|
|
addr := fmt.Sprintf("%s:%d", opts.KVConfig.Host, opts.KVConfig.Port)
|
|
rdb := kv.NewClient(&kv.Options{
|
|
Addr: addr,
|
|
Password: opts.KVConfig.Password,
|
|
})
|
|
defer func() {
|
|
_ = rdb.Close()
|
|
}()
|
|
|
|
// Test the connection
|
|
pong, err := rdb.Ping(ctx).Result()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to KV: %w", err)
|
|
}
|
|
slog.Info("Connected to KV", "pong", pong)
|
|
|
|
// Clear all keys in the current database
|
|
err = rdb.FlushAll(ctx).Err()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to flush KV database: %w", err)
|
|
}
|
|
slog.Info("All keys cleared successfully from KV database")
|
|
|
|
return nil
|
|
}
|
|
|
|
// WithKVConfig sets the KV configuration
|
|
func WithKVConfig(config KVConfig) func(*ResetKVOptions) {
|
|
return func(opts *ResetKVOptions) {
|
|
opts.KVConfig = config
|
|
}
|
|
}
|