You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
1.4 KiB

package main
import (
"context"
"fmt"
"git.aiterp.net/lucifer/new-server/app/client"
"git.aiterp.net/lucifer/new-server/app/services/handlerfactory"
"log"
"os"
)
func factoryCmd(
ctx context.Context,
c client.Client,
) {
cmd := parseCommand(os.Args[2:])
switch cmd.Name {
case "run":
fileName := cmd.Params.Get(0).String()
if fileName == nil {
log.Fatalln("Missing filename")
}
scenes, err := c.GetScenes(ctx)
if err != nil {
log.Fatalln(err.Error())
}
handlers, err := c.GetHandlers(ctx)
if err != nil {
log.Fatalln(err.Error())
}
file, err := os.Open(*fileName)
if err != nil {
log.Fatalln("Failed to open file:", err)
}
defer file.Close()
configs, err := handlerfactory.ParseConfigs(file)
for _, config := range configs {
toAdd, toRemove := handlerfactory.CreateHandlers(config, scenes, handlers)
for _, removing := range toRemove {
_, err := c.DeleteHandler(ctx, removing.ID)
if err != nil {
log.Fatalln(err.Error())
}
log.Printf("Removed handler %d", removing.ID)
}
for _, adding := range toAdd {
res, err := c.AddHandler(ctx, adding)
if err != nil {
log.Fatalln(err.Error())
}
log.Printf("Added handler %d", res.ID)
}
}
default:
if cmd.Name != "help" {
log.Println("Unknown command:", cmd.Name)
}
_, _ = fmt.Fprintln(os.Stderr, helpString[1:])
}
}