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"
"syscall"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
"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
}
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() {
var dbDir string
var resourceDir string
@ -95,11 +85,6 @@ func main() {
ctx := context.Background()
pfp := path.Join(scriptDir, "pp.csv")
flagParser, err := getFlags(pfp, true)
if err != nil {
os.Exit(1)
}
cfg := engine.Config{
Root: "root",
@ -138,13 +123,9 @@ func main() {
if !ok {
os.Exit(1)
}
lhs := handlers.LocalHandlerService{
Parser: flagParser,
DbRs: dbResource,
UserdataStore: userdataStore,
Cfg: cfg,
Rs: rs,
}
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
lhs.WithDataStore(&userdataStore)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())

View File

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

View File

@ -11,7 +11,6 @@ import (
"strconv"
"syscall"
"git.defalsify.org/vise.git/asm"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource"
@ -26,23 +25,6 @@ var (
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() {
var dbDir string
var resourceDir string
@ -64,11 +46,6 @@ func main() {
ctx := context.Background()
pfp := path.Join(scriptDir, "pp.csv")
flagParser, err := getFlags(pfp, true)
if err != nil {
os.Exit(1)
}
cfg := engine.Config{
Root: "root",
@ -107,12 +84,12 @@ func main() {
os.Exit(1)
}
lhs := handlers.LocalHandlerService{
Parser: flagParser,
DbRs: dbResource,
UserdataStore: userdataStore,
Cfg: cfg,
Rs: rs,
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
lhs.WithDataStore(&userdataStore)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
hl, err := lhs.GetHandler()

View File

@ -75,7 +75,9 @@ func main() {
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 {
fmt.Fprintf(os.Stderr, err.Error())

View File

@ -26,28 +26,34 @@ type LocalHandlerService struct {
Parser *asm.FlagParser
DbRs *resource.DbResource
Pe *persist.Persister
UserdataStore db.Db
UserdataStore *db.Db
Cfg engine.Config
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)
if err != nil {
return nil, err
}
return &LocalHandlerService{
Parser: parser,
DbRs: dbResource,
Pe: Pe,
UserdataStore: userDataStore,
Cfg: cfg,
Rs: rs,
Parser: parser,
DbRs: dbResource,
Cfg: cfg,
Rs: rs,
}, 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) {
ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, localHandlerService.UserdataStore)
ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, *localHandlerService.UserdataStore)
if err != nil {
return nil, err
}