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.

75 lines
1.4 KiB

3 years ago
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. if err != nil {
  36. log.Fatalln("Failed to parse file:", err)
  37. }
  38. for _, config := range configs {
  39. toAdd, toRemove := handlerfactory.CreateHandlers(config, scenes, handlers)
  40. for _, removing := range toRemove {
  41. _, err := c.DeleteHandler(ctx, removing.ID)
  42. if err != nil {
  43. log.Fatalln(err.Error())
  44. }
  45. log.Printf("Removed handler %d", removing.ID)
  46. }
  47. for _, adding := range toAdd {
  48. res, err := c.AddHandler(ctx, adding)
  49. if err != nil {
  50. log.Fatalln(err.Error())
  51. }
  52. log.Printf("Added handler %d", res.ID)
  53. }
  54. }
  55. default:
  56. if cmd.Name != "help" {
  57. log.Println("Unknown command:", cmd.Name)
  58. }
  59. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  60. }
  61. }