lash/admin-tool #8

Merged
lash merged 7 commits from lash/admin-tool into master 2025-01-17 10:02:04 +01:00
5 changed files with 162 additions and 59 deletions
Showing only changes of commit 6b404f56c9 - Show all commits

View File

@ -1,7 +0,0 @@
{
"admins": [
{
"phonenumber" : "<replace with any admin number to test with >"
}
]
}

View File

@ -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
}

View File

@ -2,16 +2,73 @@ package main
import ( import (
"context" "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() { func main() {
ctx := context.Background() config.LoadConfig()
err := commands.Seed(ctx)
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 { 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)
} }
} }

View File

@ -37,6 +37,7 @@ var (
translationDir = path.Join(scriptDir, "locale") 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 // FlagManager handles centralized flag management
type FlagManager struct { type FlagManager struct {
parser *asm.FlagParser parser *asm.FlagParser

99
internal/cmd/cmd.go Normal file
View File

@ -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)
}