|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"git.aiterp.net/lucifer/new-server/models" |
|
|
|
"log" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
|
func handlerCmd( |
|
|
@ -38,6 +39,98 @@ func handlerCmd( |
|
|
|
log.Fatalln("Search filter needed!") |
|
|
|
} |
|
|
|
model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr) |
|
|
|
model = applyCmdToHandler(model, cmd) |
|
|
|
|
|
|
|
newHandler, err := c.AddHandler(ctx, model) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err) |
|
|
|
} |
|
|
|
|
|
|
|
WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*newHandler}) |
|
|
|
case "edit": |
|
|
|
id := cmd.Params.Get(0).IntOr(0) |
|
|
|
if id == 0 { |
|
|
|
log.Fatalln("ID missing") |
|
|
|
} |
|
|
|
|
|
|
|
model, err := c.GetHandler(ctx, id) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
eventStr := cmd.Params.Get("event").String() |
|
|
|
if eventStr != nil { |
|
|
|
model.EventName = *eventStr |
|
|
|
} |
|
|
|
|
|
|
|
fetchStr := cmd.Params.Get("search").String() |
|
|
|
if fetchStr != nil { |
|
|
|
model.TargetKind, model.TargetValue = models.ParseFetchString(*fetchStr) |
|
|
|
} |
|
|
|
|
|
|
|
modified := applyCmdToHandler(*model, cmd) |
|
|
|
returned, err := c.PutHandler(ctx, &modified) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*returned}) |
|
|
|
case "delete": |
|
|
|
id := cmd.Params.Get(0).Int() |
|
|
|
if id == nil { |
|
|
|
log.Fatalln("ID missing") |
|
|
|
} |
|
|
|
|
|
|
|
handler, err := c.DeleteHandler(ctx, *id) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err) |
|
|
|
} |
|
|
|
|
|
|
|
WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*handler}) |
|
|
|
default: |
|
|
|
if cmd.Name != "help" { |
|
|
|
log.Println("Unknown command:", cmd.Name) |
|
|
|
} |
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, helpString[1:]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandler { |
|
|
|
// Remove keys
|
|
|
|
for _, elem := range cmd.Params.Strings(1) { |
|
|
|
if elem[0] != '-' { |
|
|
|
continue |
|
|
|
} |
|
|
|
keyToRemove := elem[1:] |
|
|
|
|
|
|
|
if strings.HasPrefix(keyToRemove, "conditions.") { |
|
|
|
condKeyToRemove := keyToRemove[11:] |
|
|
|
delete(model.Conditions, condKeyToRemove) |
|
|
|
} else if strings.HasPrefix(keyToRemove, "fire.payload.") && model.Actions.FireEvent != nil { |
|
|
|
firePayloadKeyToRemove := keyToRemove[13:] |
|
|
|
delete(model.Actions.FireEvent.Payload, firePayloadKeyToRemove) |
|
|
|
} else { |
|
|
|
switch keyToRemove { |
|
|
|
case "set-power": |
|
|
|
model.Actions.SetPower = nil |
|
|
|
case "set-color": |
|
|
|
model.Actions.SetColor = nil |
|
|
|
case "set-intensity": |
|
|
|
model.Actions.SetIntensity = nil |
|
|
|
case "add-intensity": |
|
|
|
model.Actions.AddIntensity = nil |
|
|
|
case "fire": |
|
|
|
fallthrough |
|
|
|
case "fire.name": |
|
|
|
model.Actions.FireEvent = nil |
|
|
|
case "from": |
|
|
|
model.From = models.Never |
|
|
|
case "to": |
|
|
|
model.To = models.Never |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// From / To
|
|
|
|
model.From = cmd.Params.Get("from").TimeOfDayOr(models.Never) |
|
|
@ -80,29 +173,5 @@ func handlerCmd( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
newHandler, err := c.AddHandler(ctx, model) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err) |
|
|
|
} |
|
|
|
|
|
|
|
WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*newHandler}) |
|
|
|
case "delete": |
|
|
|
id := cmd.Params.Get(0).Int() |
|
|
|
if id == nil { |
|
|
|
log.Fatalln("ID missing") |
|
|
|
} |
|
|
|
|
|
|
|
handler, err := c.DeleteHandler(ctx, *id) |
|
|
|
if err != nil { |
|
|
|
log.Fatalln(err) |
|
|
|
} |
|
|
|
|
|
|
|
WriteHandlerInfoTable(os.Stdout, []models.EventHandler{*handler}) |
|
|
|
default: |
|
|
|
if cmd.Name != "help" { |
|
|
|
log.Println("Unknown command:", cmd.Name) |
|
|
|
} |
|
|
|
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, helpString[1:]) |
|
|
|
} |
|
|
|
return model |
|
|
|
} |