Use FlagParser on main.go and delete the flags.go file

This commit is contained in:
Alfred Kamanda 2024-09-02 21:06:42 +03:00
parent b6e4ba7ede
commit 3a3ddc9922
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
2 changed files with 42 additions and 40 deletions

View File

@ -7,13 +7,13 @@ import (
"os" "os"
"path" "path"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/state"
"git.grassecon.net/urdt/ussd/internal/handlers/ussd" "git.grassecon.net/urdt/ussd/internal/handlers/ussd"
"git.grassecon.net/urdt/ussd/internal/models"
) )
var ( var (
@ -35,22 +35,46 @@ func main() {
ctx := context.Background() ctx := context.Background()
st := state.NewState(16) st := state.NewState(16)
st.UseDebug() st.UseDebug()
state.FlagDebugger.Register(models.USERFLAG_LANGUAGE_SET, "LANGUAGE_CHANGE")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_CREATED, "ACCOUNT_CREATED") // Initialize the FlagParser
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_SUCCESS, "ACCOUNT_SUCCESS") pfp := path.Join(scriptDir, "pp.csv")
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_PENDING, "ACCOUNT_PENDING") parser := asm.NewFlagParser()
state.FlagDebugger.Register(models.USERFLAG_INCORRECTPIN, "INCORRECTPIN")
state.FlagDebugger.Register(models.USERFLAG_INCORRECTDATEFORMAT, "INVALIDDATEFORMAT") // Load flags from the pp.csv file
state.FlagDebugger.Register(models.USERFLAG_INVALID_RECIPIENT, "INVALIDRECIPIENT") _, err := parser.Load(pfp)
state.FlagDebugger.Register(models.USERFLAG_PINMISMATCH, "PINMISMATCH") if err != nil {
state.FlagDebugger.Register(models.USERFLAG_PIN_SET, "PIN_SET") fmt.Fprintf(os.Stderr, "Failed to load flags: %v\n", err)
state.FlagDebugger.Register(models.USERFLAG_INVALID_RECIPIENT_WITH_INVITE, "INVALIDRECIPIENT_WITH_INVITE") os.Exit(1)
state.FlagDebugger.Register(models.USERFLAG_INVALID_AMOUNT, "INVALIDAMOUNT") }
state.FlagDebugger.Register(models.USERFLAG_ALLOW_UPDATE, "UNLOCKFORUPDATE")
state.FlagDebugger.Register(models.USERFLAG_VALIDPIN, "VALIDPIN") // Register all flags loaded from pp.csv
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_AUTHORIZED, "ACCOUNT_AUTHORIZED") flagKeys := []string{
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_CREATION_FAILED, "ACCOUNT_CREATION_FAILED") "flag_language_set",
state.FlagDebugger.Register(models.USERFLAG_SINGLE_EDIT, "SINGLEEDIT") "flag_account_created",
"flag_account_creation_failed",
"flag_account_pending",
"flag_account_success",
"flag_pin_mismatch",
"flag_pin_set",
"flag_account_authorized",
"flag_invalid_recipient",
"flag_invalid_recipient_with_invite",
"flag_invalid_amount",
"flag_incorrect_pin",
"flag_valid_pin",
"flag_allow_update",
"flag_single_edit",
"flag_incorrect_date_format",
}
for _, key := range flagKeys {
id, err := parser.GetFlag(key)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get flag %s: %v\n", key, err)
continue
}
state.FlagDebugger.Register(id, key)
}
rfs := resource.NewFsResource(scriptDir) rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache() ca := cache.NewCache()
@ -60,7 +84,7 @@ func main() {
} }
dp := path.Join(scriptDir, ".state") dp := path.Join(scriptDir, ".state")
err := os.MkdirAll(dp, 0700) err = os.MkdirAll(dp, 0700)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err) fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -1,22 +0,0 @@
package models
import "git.defalsify.org/vise.git/state"
const (
USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART
USERFLAG_ACCOUNT_CREATED
USERFLAG_ACCOUNT_CREATION_FAILED
USERFLAG_ACCOUNT_PENDING
USERFLAG_ACCOUNT_SUCCESS
USERFLAG_PINMISMATCH
USERFLAG_PIN_SET
USERFLAG_ACCOUNT_AUTHORIZED
USERFLAG_INVALID_RECIPIENT
USERFLAG_INVALID_RECIPIENT_WITH_INVITE
USERFLAG_INVALID_AMOUNT
USERFLAG_INCORRECTPIN
USERFLAG_VALIDPIN
USERFLAG_ALLOW_UPDATE
USERFLAG_SINGLE_EDIT
USERFLAG_INCORRECTDATEFORMAT
)