Add CLI as example usage

This commit is contained in:
Josh Deprez 2023-04-23 10:39:35 +00:00
parent ca6e93abf4
commit 69a1770760

35
cmd/plugctl/main.go Normal file
View file

@ -0,0 +1,35 @@
// The plugctl command provides manual CLI control over a Kasa KP105 smart plug.
package main
import (
"flag"
"fmt"
"log"
"gitea.drjosh.dev/josh/plugctl"
)
var kasaPlugAddr = flag.String("kasa-addr", "KP105:9999", "Address of Kasa smart plug")
func main() {
flag.Parse()
plug := plugctl.Plug{Addr: *kasaPlugAddr}
switch flag.Arg(0) {
case "on":
if err := plug.TurnOn(); err != nil {
log.Printf("Couldn't turn on smart plug: %v", err)
}
case "off":
if err := plug.TurnOff(); err != nil {
log.Printf("Couldn't turn off smart plug: %v", err)
}
case "state":
state, err := plug.RelayState()
if err != nil {
log.Printf("Couldn't get current relay state: %v", err)
}
fmt.Println(state)
}
}