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.

129 lines
2.4 KiB

  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/models"
  7. "log"
  8. "os"
  9. "strings"
  10. )
  11. func main() {
  12. cfg, err := loadConfig()
  13. if err != nil {
  14. log.Println("Config at $HOME/.config/lucy.yaml failed:", err)
  15. cfg = &Config{Remote: "http://localhost:8000"}
  16. }
  17. ctx := context.Background()
  18. c := client.Client{APIRoot: cfg.Remote}
  19. cmd := parseCommand(os.Args[1:])
  20. switch cmd.Name {
  21. // DEVICE
  22. case "list":
  23. {
  24. fetchStr := cmd.Params.Get(0).String()
  25. if fetchStr == nil {
  26. s := "all"
  27. fetchStr = &s
  28. }
  29. devices, err := c.GetDevices(ctx, *fetchStr)
  30. if err != nil {
  31. log.Fatalln(err)
  32. }
  33. if cmd.Params.Get("info").StringOr("false") == "true" {
  34. WriteDeviceInfoTable(os.Stdout, devices)
  35. } else {
  36. WriteDeviceStateTable(os.Stdout, devices)
  37. }
  38. }
  39. case "set":
  40. {
  41. devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.Params.DeviceState(""))
  42. if err != nil {
  43. log.Fatalln(err)
  44. }
  45. WriteDeviceStateTable(os.Stdout, devices)
  46. }
  47. case "tag":
  48. {
  49. var addTags []string
  50. var removeTags []string
  51. for _, tag := range cmd.Params.Strings(1) {
  52. if strings.HasPrefix(tag, "+") {
  53. addTags = append(addTags, tag[1:])
  54. } else if strings.HasPrefix(tag, "-") {
  55. removeTags = append(removeTags, tag[1:])
  56. } else {
  57. addTags = append(addTags, tag)
  58. }
  59. }
  60. devices, err := c.PutDeviceTags(
  61. ctx,
  62. cmd.Params.Get(0).StringOr("all"),
  63. addTags,
  64. removeTags,
  65. )
  66. if err != nil {
  67. log.Fatalln(err)
  68. }
  69. WriteDeviceInfoTable(os.Stdout, devices)
  70. }
  71. case "update":
  72. {
  73. devices, err := c.PutDevice(ctx, cmd.Params.Get(0).StringOr("all"), models.DeviceUpdate{
  74. Name: cmd.Params.Get("name").String(),
  75. Icon: cmd.Params.Get("icon").String(),
  76. UserProperties: cmd.Params.Subset("prop.").StringPtrMap(),
  77. })
  78. if err != nil {
  79. log.Fatalln(err)
  80. }
  81. WriteDeviceInfoTable(os.Stdout, devices)
  82. }
  83. case "handler":
  84. handlerCmd(ctx, c)
  85. case "scene":
  86. sceneCmd(ctx, c)
  87. // EVENT
  88. case "run":
  89. {
  90. name := cmd.Params.Get(0).String()
  91. if name == nil {
  92. return
  93. }
  94. event := models.Event{
  95. Name: *name,
  96. Payload: cmd.Params.StringMap(),
  97. }
  98. err := c.FireEvent(ctx, event)
  99. if err != nil {
  100. log.Fatalln(err)
  101. }
  102. }
  103. default:
  104. if cmd.Name != "help" {
  105. log.Println("Unknown command:", cmd.Name)
  106. }
  107. _, _ = fmt.Fprintln(os.Stderr, helpString[1:])
  108. }
  109. }