Reconfigure to seperate "to" and "from" commands
This commit is contained in:
parent
861964ea99
commit
3d77d98d50
|
@ -2,12 +2,21 @@
|
|||
listen = ":5051"
|
||||
|
||||
[[targets]]
|
||||
id = "host"
|
||||
commands = [
|
||||
"sudo chvt 1",
|
||||
id = "win11"
|
||||
commands_to = [
|
||||
"sudo virsh attach-device win11 --live --file ~/.config/kvmswitcher/keyboard.xml",
|
||||
"sudo virsh attach-device win11 --live --file ~/.config/kvmswitcher/mouse.xml",
|
||||
"sudo virsh attach-device win11 --live --file ~/.config/kvmswitcher/binepad.xml",
|
||||
"sudo virsh attach-device win11 --live --file ~/.config/kvmswitcher/webcam.xml",
|
||||
"sleep 0.5",
|
||||
"sudo virsh detach-device win11 --file ~/.config/kvmswitcher/keyboard.xml --current",
|
||||
"sudo virsh detach-device win11 --file ~/.config/kvmswitcher/mouse.xml --current",
|
||||
"sudo ddcutil setvcp 0x60 --display 1 0x12",
|
||||
"sudo chvt 1",
|
||||
]
|
||||
commands_from = [
|
||||
"sudo virsh detach-device win11 --live --file ~/.config/kvmswitcher/keyboard.xml",
|
||||
"sudo virsh detach-device win11 --live --file ~/.config/kvmswitcher/mouse.xml",
|
||||
"sudo virsh detach-device win11 --live --file ~/.config/kvmswitcher/binepad.xml",
|
||||
"sudo virsh detach-device win11 --live --file ~/.config/kvmswitcher/webcam.xml",
|
||||
"sleep 0.5",
|
||||
"sudo ddcutil setvcp 0x60 --display 1 0x0f",
|
||||
"sleep 0.5",
|
||||
|
@ -15,10 +24,27 @@ commands = [
|
|||
]
|
||||
|
||||
[[targets]]
|
||||
id = "guest"
|
||||
commands = [
|
||||
"sudo virsh attach-device win11 --file ~/.config/kvmswitcher/keyboard.xml --current",
|
||||
"sudo virsh attach-device win11 --file ~/.config/kvmswitcher/mouse.xml --current",
|
||||
id = "hoke"
|
||||
commands_to = [
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/keyboard.xml",
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/mouse.xml",
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/binepad.xml",
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/fiio.xml",
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/tonor.xml",
|
||||
"sudo virsh attach-device hoke --live --file ~/.config/kvmswitcher/webcam.xml",
|
||||
"sleep 0.5",
|
||||
"sudo ddcutil setvcp 0x60 --display 1 0x12",
|
||||
"sudo chvt 1",
|
||||
]
|
||||
commands_from = [
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/keyboard.xml",
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/mouse.xml",
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/binepad.xml",
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/fiio.xml",
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/tonor.xml",
|
||||
"sudo virsh detach-device hoke --live --file ~/.config/kvmswitcher/webcam.xml",
|
||||
"sleep 0.5",
|
||||
"sudo ddcutil setvcp 0x60 --display 1 0x0f",
|
||||
"sleep 0.5",
|
||||
"sudo chvt 5",
|
||||
]
|
||||
|
|
|
@ -7,11 +7,14 @@ import (
|
|||
type TargetId string
|
||||
type Command string
|
||||
type Target struct {
|
||||
Id TargetId
|
||||
Commands []Command
|
||||
Id TargetId `toml:"id"`
|
||||
CommandsTo []Command `toml:"commands_to"`
|
||||
CommandsFrom []Command `toml:"commands_from"`
|
||||
}
|
||||
type Targets []Target
|
||||
|
||||
const HostTarget TargetId = "host"
|
||||
|
||||
type T struct {
|
||||
Globals struct {
|
||||
Listen *string
|
||||
|
|
66
server.go
66
server.go
|
@ -20,7 +20,7 @@ var (
|
|||
)
|
||||
listen = flag.String("listen", ":5001", "Address and port to listen on")
|
||||
targets config.Targets
|
||||
curTargetId config.TargetId
|
||||
curTargetId config.TargetId // currently unused, but keeps track of what is activated
|
||||
)
|
||||
|
||||
func targetIds(t config.Targets) []config.TargetId {
|
||||
|
@ -45,21 +45,29 @@ func main() {
|
|||
log.Fatal("No targets configured")
|
||||
}
|
||||
|
||||
curTargetId = targetIds(targets)[0]
|
||||
curTargetId = config.HostTarget
|
||||
|
||||
if c.Globals.Listen != nil {
|
||||
listen = c.Globals.Listen
|
||||
}
|
||||
|
||||
log.Printf("configured hosts: %v\n", targetIds(targets))
|
||||
log.Printf("configured targets: %v\n", targetIds(targets))
|
||||
log.Printf("listening on %s\n", *listen)
|
||||
http.HandleFunc("/to/", doSwitch)
|
||||
http.HandleFunc("/next", doNext)
|
||||
http.HandleFunc("/to/", doSwitchTo)
|
||||
http.HandleFunc("/from/", doSwitchFrom)
|
||||
http.ListenAndServe(*listen, nil)
|
||||
}
|
||||
|
||||
func doSwitch(w http.ResponseWriter, r *http.Request) {
|
||||
ct, _ := regexp.Compile("^/to/")
|
||||
func doSwitchFrom(w http.ResponseWriter, r *http.Request) {
|
||||
doSwitch(false, w, r)
|
||||
}
|
||||
|
||||
func doSwitchTo(w http.ResponseWriter, r *http.Request) {
|
||||
doSwitch(true, w, r)
|
||||
}
|
||||
|
||||
func doSwitch(to bool, w http.ResponseWriter, r *http.Request) {
|
||||
ct, _ := regexp.Compile("^/(from|to)/")
|
||||
u := config.TargetId(ct.ReplaceAllString(r.URL.String(), ""))
|
||||
if u == config.TargetId(r.URL.String()) {
|
||||
fmt.Fprintf(w, "Malformed request: %s\n", u)
|
||||
|
@ -69,7 +77,6 @@ func doSwitch(w http.ResponseWriter, r *http.Request) {
|
|||
for i := range targets {
|
||||
if targets[i].Id == config.TargetId(u) {
|
||||
t = &targets[i]
|
||||
curTargetId = t.Id
|
||||
}
|
||||
}
|
||||
if t == nil {
|
||||
|
@ -77,39 +84,20 @@ func doSwitch(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
log.Printf("Switching to %s\n", u)
|
||||
out, err := runCommands(t.Commands)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "ERROR:\n%s", err)
|
||||
}
|
||||
fmt.Fprintf(w, "OUTPUT:\n%s", out)
|
||||
}
|
||||
|
||||
func doNext(w http.ResponseWriter, r *http.Request) {
|
||||
if len(targets) < 2 {
|
||||
fmt.Fprintln(w, "No other target found")
|
||||
return
|
||||
}
|
||||
|
||||
useNext := false
|
||||
var newTarg *config.Target
|
||||
for newTarg == nil {
|
||||
for i := range targets {
|
||||
if useNext {
|
||||
newTarg = &targets[i]
|
||||
curTargetId = newTarg.Id
|
||||
break
|
||||
}
|
||||
if targets[i].Id == curTargetId {
|
||||
useNext = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Switching to %s\n", newTarg.Id)
|
||||
out, err := runCommands(newTarg.Commands)
|
||||
cmds := t.CommandsFrom
|
||||
toFrom := "from"
|
||||
curTargetId = config.HostTarget
|
||||
if to {
|
||||
cmds = t.CommandsTo
|
||||
toFrom = "to"
|
||||
curTargetId = t.Id
|
||||
}
|
||||
|
||||
log.Printf("Switching %s %s\n", toFrom, u)
|
||||
out, err := runCommands(cmds)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "ERROR:\n%s", err)
|
||||
log.Printf("Error on command execution (switching %s %s): %s\n", toFrom, u, err)
|
||||
}
|
||||
fmt.Fprintf(w, "OUTPUT:\n%s", out)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue