diff --git a/devtools/admin/admin_numbers.json b/devtools/admin/admin_numbers.json deleted file mode 100644 index ca58a23..0000000 --- a/devtools/admin/admin_numbers.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "admins": [ - { - "phonenumber" : "" - } - ] -} \ No newline at end of file diff --git a/devtools/admin/commands/seed.go b/devtools/admin/commands/seed.go deleted file mode 100644 index ec639b5..0000000 --- a/devtools/admin/commands/seed.go +++ /dev/null @@ -1,47 +0,0 @@ -package commands - -import ( - "context" - "encoding/json" - "os" - - "git.defalsify.org/vise.git/logging" - "git.grassecon.net/grassrootseconomics/sarafu-vise/store" -) - -var ( - logg = logging.NewVanilla().WithDomain("adminstore") -) - -type Admin struct { - PhoneNumber string `json:"phonenumber"` -} - -type Config struct { - Admins []Admin `json:"admins"` -} - -func Seed(ctx context.Context) error { - var config Config - adminstore, err := store.NewAdminStore(ctx, "../admin_numbers") - store := adminstore.FsStore - if err != nil { - return err - } - defer store.Close() - data, err := os.ReadFile("admin_numbers.json") - if err != nil { - return err - } - if err := json.Unmarshal(data, &config); err != nil { - return err - } - for _, admin := range config.Admins { - err := store.Put(ctx, []byte(admin.PhoneNumber), []byte("1")) - if err != nil { - logg.Printf(logging.LVL_DEBUG, "Failed to insert admin number", admin.PhoneNumber) - return err - } - } - return nil -} diff --git a/devtools/admin/main.go b/devtools/admin/main.go index defbbf6..f65440b 100644 --- a/devtools/admin/main.go +++ b/devtools/admin/main.go @@ -2,16 +2,73 @@ package main import ( "context" - "log" + "flag" + "fmt" + "os" + "path" - "git.grassecon.net/grassrootseconomics/sarafu-vise/devtools/admin/commands" + "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/asm" + + "git.grassecon.net/grassrootseconomics/sarafu-vise/config" + "git.grassecon.net/grassrootseconomics/visedriver/storage" + "git.grassecon.net/grassrootseconomics/sarafu-vise/internal/cmd" +) + +var ( + logg = logging.NewVanilla().WithContextKey("SessionId") + scriptDir = path.Join("services", "registration") ) func main() { - ctx := context.Background() - err := commands.Seed(ctx) + config.LoadConfig() + + var sessionId string + var connStr string + + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") + flag.StringVar(&connStr, "c", "", "connection string") + flag.Parse() + + + if connStr == "" { + connStr = config.DbConn() + } + connData, err := storage.ToConnData(connStr) if err != nil { - log.Fatalf("Failed to initialize a list of admins with error %s", err) + fmt.Fprintf(os.Stderr, "connstr err: %v\n", err) + os.Exit(1) + } + ctx := context.Background() + ctx = context.WithValue(ctx, "SessionId", sessionId) + + pfp := path.Join(scriptDir, "pp.csv") + flagParser := asm.NewFlagParser().WithDebug() + _, err = flagParser.Load(pfp) + if err != nil { + fmt.Fprintf(os.Stderr, "flagparser fail: %v\n", err) + os.Exit(1) + } + + x := cmd.NewCmd(connData, sessionId, flagParser) + err = x.Parse(flag.Args()) + if err != nil { + fmt.Fprintf(os.Stderr, "cmd parse fail: %v\n", err) + os.Exit(1) + } + + logg.Infof("start command", "conn", connData, "subcmd", x) + + menuStorageService := storage.NewMenuStorageService(connData, "") + if err != nil { + fmt.Fprintf(os.Stderr, "menu storage service error: %v\n", err) + os.Exit(1) + } + + err = x.Exec(ctx, menuStorageService) + if err != nil { + fmt.Fprintf(os.Stderr, "cmd exec error: %v\n", err) + os.Exit(1) } } diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 28f6854..abdef28 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -37,6 +37,7 @@ var ( translationDir = path.Join(scriptDir, "locale") ) +// TODO: this is only in use in testing, should be moved to test domain and/or replaced by asm.FlagParser // FlagManager handles centralized flag management type FlagManager struct { parser *asm.FlagParser diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go new file mode 100644 index 0000000..3bc817f --- /dev/null +++ b/internal/cmd/cmd.go @@ -0,0 +1,99 @@ +package cmd + +import ( + "context" + "fmt" + + "git.defalsify.org/vise.git/asm" + "git.defalsify.org/vise.git/logging" + "git.grassecon.net/grassrootseconomics/visedriver/storage" +) + +var ( + logg = logging.NewVanilla().WithDomain("cmd").WithContextKey("SessionId") +) + +type Cmd struct { + sessionId string + conn storage.ConnData + flagParser *asm.FlagParser + cmd int + enable bool + exec func(ctx context.Context, ss storage.StorageService) error +} + +func NewCmd(conn storage.ConnData, sessionId string, flagParser *asm.FlagParser) *Cmd { + return &Cmd{ + conn: conn, + sessionId: sessionId, + flagParser: flagParser, + } +} + +func (c *Cmd) Exec(ctx context.Context, ss storage.StorageService) error { + return c.exec(ctx, ss) +} + +func (c *Cmd) execAdmin(ctx context.Context, ss storage.StorageService) error { + pe, err := ss.GetPersister(ctx) + if err != nil { + return err + } + err = pe.Load(c.sessionId) + if err != nil { + return err + } + defer func() { + err := pe.Save(c.sessionId) + if err != nil { + logg.ErrorCtxf(ctx, "failed persister save: %v", err) + } + }() + + st := pe.GetState() + flag, err := c.flagParser.GetFlag("flag_admin_privilege") + if err != nil { + return err + } + if c.enable { + logg.InfoCtxf(ctx, "setting admin flag", "flag", flag) + st.SetFlag(flag) + } else { + st.ResetFlag(flag) + } + return nil +} + +func (c *Cmd) parseCmdAdmin(cmd string, param string, more []string) (bool, error) { + if cmd == "admin" { + if param == "1" { + c.enable = true + } else if param != "0" { + return false, fmt.Errorf("invalid parameter: %v", param) + } + c.exec = c.execAdmin + return true, nil + } + return false, nil +} + +func (c *Cmd) Parse(args []string) error { + if len(args) < 2 { + return fmt.Errorf("Wrong number of arguments: %v", args) + } + cmd := args[0] + param := args[1] + args = args[2:] + + r, err := c.parseCmdAdmin(cmd, param, args) + if err != nil { + return err + } + if r { + return nil + } + + return fmt.Errorf("unknown subcommand: %s", cmd) +} + +