ussd/devtools/gen/main.go

92 lines
2.0 KiB
Go
Raw Normal View History

2024-12-08 00:06:03 +01:00
package main
import (
"context"
"crypto/sha1"
2024-12-08 00:06:03 +01:00
"flag"
"fmt"
"os"
"path"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/internal/storage"
2024-12-08 23:48:39 +01:00
"git.grassecon.net/urdt/ussd/initializers"
2024-12-08 00:06:03 +01:00
"git.grassecon.net/urdt/ussd/common"
)
var (
logg = logging.NewVanilla()
scriptDir = path.Join("services", "registration")
)
2024-12-08 23:48:39 +01:00
func init() {
initializers.LoadEnvVariables()
}
2024-12-08 00:06:03 +01:00
func main() {
config.LoadConfig()
2025-01-04 10:37:12 +01:00
var connStr string
2024-12-08 00:06:03 +01:00
var sessionId string
var database string
var engineDebug bool
2025-01-04 10:37:12 +01:00
var err error
2024-12-08 00:06:03 +01:00
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&connStr, "c", "", "connection string")
2024-12-08 00:06:03 +01:00
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse()
if connStr != "" {
connStr = config.DbConn
}
connData, err := storage.ToConnData(config.DbConn)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
2025-01-04 10:37:12 +01:00
}
logg.Infof("start command", "conn", connData)
2025-01-04 10:37:12 +01:00
2024-12-08 00:06:03 +01:00
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
ctx = context.WithValue(ctx, "Database", database)
resourceDir := scriptDir
2025-01-04 10:37:12 +01:00
menuStorageService := storage.NewMenuStorageService(resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
2024-12-08 00:06:03 +01:00
store, err := menuStorageService.GetUserdataDb(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
userStore := common.UserDataStore{store}
h := sha1.New()
h.Write([]byte(sessionId))
address := h.Sum(nil)
addressString := fmt.Sprintf("%x", address)
err = userStore.WriteEntry(ctx, sessionId, common.DATA_PUBLIC_KEY, []byte(addressString))
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
err = userStore.WriteEntry(ctx, addressString, common.DATA_PUBLIC_KEY_REVERSE, []byte(sessionId))
2024-12-08 00:06:03 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
err = store.Close()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}