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

3 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "git.aiterp.net/lucifer/new-server/app/client"
  6. "git.aiterp.net/lucifer/new-server/app/services/handlerfactory"
  7. "log"
  8. "os"
  9. )
  10. func factoryCmd(
  11. ctx context.Context,
  12. c client.Client,
  13. ) {
  14. cmd := parseCommand(os.Args[2:])
  15. switch cmd.Name {
  16. case "run":
  17. fileName := cmd.Params.Get(0).String()
  18. if fileName == nil {
  19. log.Fatalln("Missing filename")
  20. }
  21. scenes, err := c.GetScenes(ctx)
  22. if err != nil {
  23. log.Fatalln(err.Error())
  24. }
  25. handlers, err := c.GetHandlers(ctx)
  26. if err != nil {
  27. log.Fatalln(err.Error())
  28. }
  29. file, err := os.Open(*fileName)
  30. if err != nil {
  31. log.Fatalln("Failed to open file:", err)
  32. }
  33. defer file.Close()
  34. configs, err := handlerfactory.ParseConfigs(file)
  35. for _, config := range configs {
  36. toAdd, toRemove := handlerfactory.CreateHandlers(config, scenes, handlers)
  37. for _, removing := range toRemove {
  38. _, err := c.DeleteHandler(ctx, removing.ID)
  39. if err != nil {
  40. log.Fatalln(err.Error())
  41. }
  42. log.Printf("Removed handler %d", removing.ID)
  43. }
  44. for _, adding := range toAdd {
  45. res, err := c.AddHandler(ctx, adding)
  46. if err != nil {
  47. log.Fatalln(err.Error())
  48. }
  49. log.Printf("Added handler %d", res.ID)
  50. }
  51. }
  52. default:
  53. if cmd.Name != "help" {
  54. log.Println("Unknown command:", cmd.Name)
  55. }
  56. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  57. }
  58. }