factor out getflag

This commit is contained in:
Carlosokumu 2024-09-19 21:04:09 +03:00
parent 96358959b4
commit e581a1ad2f
Signed by untrusted user: carlos
GPG Key ID: 7BD6BC8160A5C953
5 changed files with 29 additions and 83 deletions

View File

@ -12,7 +12,6 @@ import (
"strings" "strings"
"syscall" "syscall"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
@ -65,15 +64,6 @@ func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
return []byte(parts[len(parts)-1]), nil return []byte(parts[len(parts)-1]), nil
} }
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
flagParser := asm.NewFlagParser().WithDebug()
_, err := flagParser.Load(fp)
if err != nil {
return nil, err
}
return flagParser, nil
}
func main() { func main() {
var dbDir string var dbDir string
var resourceDir string var resourceDir string
@ -95,11 +85,6 @@ func main() {
ctx := context.Background() ctx := context.Background()
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
flagParser, err := getFlags(pfp, true)
if err != nil {
os.Exit(1)
}
cfg := engine.Config{ cfg := engine.Config{
Root: "root", Root: "root",
@ -138,13 +123,9 @@ func main() {
if !ok { if !ok {
os.Exit(1) os.Exit(1)
} }
lhs := handlers.LocalHandlerService{
Parser: flagParser, lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
DbRs: dbResource, lhs.WithDataStore(&userdataStore)
UserdataStore: userdataStore,
Cfg: cfg,
Rs: rs,
}
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())

View File

@ -9,7 +9,6 @@ import (
"path" "path"
"syscall" "syscall"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
@ -36,15 +35,6 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) {
return p.input, nil return p.input, nil
} }
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
flagParser := asm.NewFlagParser().WithDebug()
_, err := flagParser.Load(fp)
if err != nil {
return nil, err
}
return flagParser, nil
}
func main() { func main() {
var sessionId string var sessionId string
var dbDir string var dbDir string
@ -68,11 +58,6 @@ func main() {
ctx := context.Background() ctx := context.Background()
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
flagParser, err := getFlags(pfp, true)
if err != nil {
os.Exit(1)
}
cfg := engine.Config{ cfg := engine.Config{
Root: "root", Root: "root",
@ -111,13 +96,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
lhs := handlers.LocalHandlerService{ lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
Parser: flagParser, lhs.WithDataStore(&userdataStore)
DbRs: dbResource,
UserdataStore: userdataStore,
Cfg: cfg,
Rs: rs,
}
hl, err := lhs.GetHandler() hl, err := lhs.GetHandler()
if err != nil { if err != nil {

View File

@ -11,7 +11,6 @@ import (
"strconv" "strconv"
"syscall" "syscall"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
@ -26,23 +25,6 @@ var (
scriptDir = path.Join("services", "registration") scriptDir = path.Join("services", "registration")
) )
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
flagParser := asm.NewFlagParser().WithDebug()
_, err := flagParser.Load(fp)
if err != nil {
return nil, err
}
return flagParser, nil
}
func ensureDbDir(dbDir string) error {
err := os.MkdirAll(dbDir, 0700)
if err != nil {
return fmt.Errorf("state dir create exited with error: %v\n", err)
}
return nil
}
func main() { func main() {
var dbDir string var dbDir string
var resourceDir string var resourceDir string
@ -64,11 +46,6 @@ func main() {
ctx := context.Background() ctx := context.Background()
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
flagParser, err := getFlags(pfp, true)
if err != nil {
os.Exit(1)
}
cfg := engine.Config{ cfg := engine.Config{
Root: "root", Root: "root",
@ -107,12 +84,12 @@ func main() {
os.Exit(1) os.Exit(1)
} }
lhs := handlers.LocalHandlerService{ lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
Parser: flagParser, lhs.WithDataStore(&userdataStore)
DbRs: dbResource,
UserdataStore: userdataStore, if err != nil {
Cfg: cfg, fmt.Fprintf(os.Stderr, err.Error())
Rs: rs, os.Exit(1)
} }
hl, err := lhs.GetHandler() hl, err := lhs.GetHandler()

View File

@ -75,7 +75,9 @@ func main() {
os.Exit(1) os.Exit(1)
} }
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, pe, userdatastore, cfg, rs) lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
lhs.WithDataStore(&userdatastore)
lhs.WithPersister(pe)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())

View File

@ -26,28 +26,34 @@ type LocalHandlerService struct {
Parser *asm.FlagParser Parser *asm.FlagParser
DbRs *resource.DbResource DbRs *resource.DbResource
Pe *persist.Persister Pe *persist.Persister
UserdataStore db.Db UserdataStore *db.Db
Cfg engine.Config Cfg engine.Config
Rs resource.Resource Rs resource.Resource
} }
func NewLocalHandlerService(fp string, debug bool, dbResource *resource.DbResource, Pe *persist.Persister, userDataStore db.Db, cfg engine.Config, rs resource.Resource) (*LocalHandlerService, error) { func NewLocalHandlerService(fp string, debug bool, dbResource *resource.DbResource, cfg engine.Config, rs resource.Resource) (*LocalHandlerService, error) {
parser, err := getParser(fp, debug) parser, err := getParser(fp, debug)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &LocalHandlerService{ return &LocalHandlerService{
Parser: parser, Parser: parser,
DbRs: dbResource, DbRs: dbResource,
Pe: Pe, Cfg: cfg,
UserdataStore: userDataStore, Rs: rs,
Cfg: cfg,
Rs: rs,
}, nil }, nil
} }
func (localHandlerService *LocalHandlerService) WithPersister(Pe *persist.Persister) {
localHandlerService.Pe = Pe
}
func (localHandlerService *LocalHandlerService) WithDataStore(db *db.Db) {
localHandlerService.UserdataStore = db
}
func (localHandlerService *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { func (localHandlerService *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, localHandlerService.UserdataStore) ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, *localHandlerService.UserdataStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }