package main import ( "context" "fmt" "git.aiterp.net/lucifer/new-server/app/client" "git.aiterp.net/lucifer/new-server/models" "log" "os" "strings" ) func main() { cfg, err := loadConfig() if err != nil { log.Println("Config at $HOME/.config/lucy.yaml failed:", err) cfg = &Config{Remote: "http://localhost:8000"} } c := client.Client{APIRoot: cfg.Remote} cmd := parseCommand(os.Args[1:]) switch cmd.Name { // DEVICE case "list": { fetchStr := cmd.Params.Get(0).String() if fetchStr == nil { s := "all" fetchStr = &s } devices, err := c.GetDevices(context.Background(), *fetchStr) if err != nil { log.Fatalln(err) } WriteDeviceStateTable(os.Stdout, devices) } case "set": { devices, err := c.PutDeviceState(context.Background(), cmd.Params.Get(0).StringOr("all"), models.NewDeviceState{ Power: cmd.Params.Get("power").Bool(), Color: cmd.Params.Get("color").String(), Intensity: cmd.Params.Get("intensity").Float(), Temperature: cmd.Params.Get("temperature").Int(), }) if err != nil { log.Fatalln(err) } WriteDeviceStateTable(os.Stdout, devices) } case "tag": { var addTags []string var removeTags []string for _, tag := range cmd.Params.Strings(1) { if strings.HasPrefix(tag, "+") { addTags = append(addTags, tag[1:]) } else if strings.HasPrefix(tag, "-") { removeTags = append(removeTags, tag[1:]) } else { addTags = append(addTags, tag) } } devices, err := c.PutDeviceTags( context.Background(), cmd.Params.Get(0).StringOr("all"), addTags, removeTags, ) if err != nil { log.Fatalln(err) } WriteDeviceInfoTable(os.Stdout, devices) } case "update": { devices, err := c.PutDevice(context.Background(), cmd.Params.Get(0).StringOr("all"), models.DeviceUpdate{ Name: cmd.Params.Get("name").String(), Icon: cmd.Params.Get("icon").String(), UserProperties: cmd.Params.Subset("prop.").StringPtrMap(), }) if err != nil { log.Fatalln(err) } WriteDeviceInfoTable(os.Stdout, devices) } // EVENT case "run": { name := cmd.Params.Get(0).String() if name == nil { return } event := models.Event{ Name: *name, Payload: cmd.Params.StringMap(), } err := c.FireEvent(context.Background(), event) if err != nil { log.Fatalln(err) } } default: if cmd.Name != "help" { log.Println("Unknown command:", cmd.Name) } _, _ = fmt.Fprintln(os.Stderr, helpString[1:]) } }