cli: add 'switch' command to change page on running daemon
This commit is contained in:
parent
799ef45e1a
commit
993756117c
39
main.go
39
main.go
@ -1,12 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
@ -30,6 +34,7 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, "Usage: streamdeck-lets-go [flags] [command]\n\n")
|
fmt.Fprintf(os.Stderr, "Usage: streamdeck-lets-go [flags] [command]\n\n")
|
||||||
fmt.Fprintf(os.Stderr, "Commands:\n")
|
fmt.Fprintf(os.Stderr, "Commands:\n")
|
||||||
fmt.Fprintf(os.Stderr, " daemon run the Stream Deck daemon with web UI (default)\n")
|
fmt.Fprintf(os.Stderr, " daemon run the Stream Deck daemon with web UI (default)\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " switch switch running daemon to a page (e.g. 'switch games')\n")
|
||||||
fmt.Fprintf(os.Stderr, " discover list connected Stream Decks\n")
|
fmt.Fprintf(os.Stderr, " discover list connected Stream Decks\n")
|
||||||
fmt.Fprintf(os.Stderr, "\nFlags:\n")
|
fmt.Fprintf(os.Stderr, "\nFlags:\n")
|
||||||
fs.PrintDefaults()
|
fs.PrintDefaults()
|
||||||
@ -42,6 +47,18 @@ func main() {
|
|||||||
switch cmd {
|
switch cmd {
|
||||||
case "daemon":
|
case "daemon":
|
||||||
fs.Parse(args)
|
fs.Parse(args)
|
||||||
|
case "switch":
|
||||||
|
fs.Parse(args)
|
||||||
|
if fs.NArg() == 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage: streamdeck-lets-go switch <page>\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if err := switchToPage(*httpAddr, fs.Arg(0)); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Printf("switched to page %q\n", fs.Arg(0))
|
||||||
|
return
|
||||||
case "discover":
|
case "discover":
|
||||||
discover()
|
discover()
|
||||||
return
|
return
|
||||||
@ -94,3 +111,25 @@ func discover() {
|
|||||||
fmt.Printf(" Serial: %s Model: %s PID: 0x%04x\n", d.Serial, d.Model, d.PID)
|
fmt.Printf(" Serial: %s Model: %s PID: 0x%04x\n", d.Serial, d.Model, d.PID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// switchToPage tells the running daemon (at httpAddr) to activate the given page
|
||||||
|
// by POSTing to its /api/activate-page endpoint.
|
||||||
|
func switchToPage(httpAddr, page string) error {
|
||||||
|
host := httpAddr
|
||||||
|
if strings.HasPrefix(host, ":") {
|
||||||
|
host = "127.0.0.1" + host
|
||||||
|
}
|
||||||
|
url := "http://" + host + "/api/activate-page"
|
||||||
|
|
||||||
|
body, _ := json.Marshal(map[string]string{"page": page})
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("daemon not running? %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("switch failed (HTTP %d)", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user