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

97 lines
2.1 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.FromKey(k)
o, err := debug.ToKeyInfo(k, sessionId)
2025-01-12 10:24:07 +01:00
if err != nil {
return "", err
}
s := fmt.Sprintf("%vValue: %v\n\n", o, string(v))
return s, nil
}
func main() {
config.LoadConfig()
var connStr string
var sessionId string
var database string
var engineDebug bool
var err error
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
2025-01-17 11:36:02 +01:00
flag.StringVar(&connStr, "c", "", "connection string")
2025-01-12 10:24:07 +01:00
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse()
2025-01-17 11:36:02 +01:00
if connStr == "" {
connStr = config.DbConn()
2025-01-12 10:24:07 +01:00
}
connData, err := storage.ToConnData(connStr)
2025-01-12 10:24:07 +01:00
if err != nil {
2025-01-17 11:36:02 +01:00
fmt.Fprintf(os.Stderr, "connstr err: %v\n", err)
2025-01-12 10:24:07 +01:00
os.Exit(1)
}
logg.Infof("start command", "conn", connData)
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
ctx = context.WithValue(ctx, "Database", database)
resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
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
}
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)
}
err = store.Close()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}