sarafu-vise/devtools/store/dump/main.go

102 lines
2.4 KiB
Go
Raw Normal View History

2025-01-12 10:24:07 +01:00
package main
import (
"context"
"flag"
"fmt"
"os"
"path"
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
2025-01-12 10:24:07 +01:00
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise/debug"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/logging"
)
var (
logg = logging.NewVanilla()
scriptDir = path.Join("services", "registration")
)
2025-01-17 11:36:02 +01:00
func formatItem(k []byte, v []byte, sessionId string) (string, error) {
o, err := debug.ToKeyInfo(k, sessionId)
2025-01-12 10:24:07 +01:00
if err != nil {
return "", err
}
s := fmt.Sprintf("%v\t%v\n", o.Label, string(v))
2025-01-12 10:24:07 +01:00
return s, nil
}
func main() {
config.LoadConfig()
2025-01-19 17:32:55 +01:00
var override config.Override
2025-01-12 10:24:07 +01:00
var sessionId string
var database string
var engineDebug bool
var err error
var first bool
2025-01-12 10:24:07 +01:00
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
2025-01-19 17:32:55 +01:00
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string")
2025-01-12 10:24:07 +01:00
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse()
2025-01-19 17:32:55 +01:00
config.Apply(&override)
conns, err := config.GetConns()
2025-01-12 10:24:07 +01:00
if err != nil {
2025-01-19 17:32:55 +01:00
fmt.Fprintf(os.Stderr, "conn specification error: %v\n", err)
2025-01-12 10:24:07 +01:00
os.Exit(1)
}
2025-01-19 17:32:55 +01:00
logg.Infof("start command", "conn", conns)
2025-01-12 10:24:07 +01:00
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
ctx = context.WithValue(ctx, "Database", database)
2025-01-19 17:32:55 +01:00
menuStorageService := storage.NewMenuStorageService(conns)
2025-01-12 10:24:07 +01:00
store, err := menuStorageService.GetUserdataDb(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "get userdata db: %v\n", err.Error())
os.Exit(1)
}
2025-01-17 11:36:02 +01:00
store.SetSession(sessionId)
2025-01-12 10:24:07 +01:00
store.SetPrefix(db.DATATYPE_USERDATA)
2025-01-17 11:36:02 +01:00
d, err := store.Dump(ctx, []byte(""))
2025-01-12 10:24:07 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, "store dump fail: %v\n", err.Error())
os.Exit(1)
}
for true {
k, v := d.Next(ctx)
if k == nil {
break
}
if !first {
fmt.Printf("Session ID: %s\n---\n", sessionId)
first = true
}
2025-01-17 11:36:02 +01:00
r, err := formatItem(append([]byte{db.DATATYPE_USERDATA}, k...), v, sessionId)
2025-01-12 10:24:07 +01:00
if err != nil {
2025-01-17 11:36:02 +01:00
fmt.Fprintf(os.Stderr, "format db item error: %v\n", err)
2025-01-12 10:24:07 +01:00
os.Exit(1)
}
fmt.Printf(r)
}
2025-01-19 10:46:37 +01:00
err = store.Close(ctx)
2025-01-12 10:24:07 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}