Add Windows platform integration scaffolding
This commit is contained in:
committed by
Brandon McGinty
parent
fab4fd50d2
commit
9c487c2586
@@ -3,18 +3,14 @@ package main
|
||||
import _ "net/http/pprof"
|
||||
import (
|
||||
"al.essio.dev/pkg/shellescape"
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
barnlog "git.stormux.org/storm/barnard/log"
|
||||
|
||||
@@ -50,26 +46,16 @@ func do_list_devices() {
|
||||
|
||||
const notificationQueueSize = 32
|
||||
|
||||
func setup_notify_runner(notify_command string) chan []string {
|
||||
t := make(chan []string, notificationQueueSize)
|
||||
var do_nothing = false
|
||||
var err error
|
||||
if err != nil {
|
||||
}
|
||||
if notify_command == "" {
|
||||
do_nothing = true
|
||||
}
|
||||
go func(events chan []string, cmd_template string, dummy bool) {
|
||||
func setup_notify_runner(notifyCommand string) chan []string {
|
||||
events := make(chan []string, notificationQueueSize)
|
||||
go func() {
|
||||
for event := range events {
|
||||
if !dummy {
|
||||
cmd := "/bin/sh"
|
||||
args := []string{"-c", expandNotification(cmd_template, event)}
|
||||
x := exec.Command(cmd, args...)
|
||||
x.Run()
|
||||
if notifyCommand != "" {
|
||||
runNotification(expandNotification(notifyCommand, event))
|
||||
}
|
||||
}
|
||||
}(t, notify_command, do_nothing)
|
||||
return t
|
||||
}()
|
||||
return events
|
||||
}
|
||||
|
||||
// expandNotification replaces placeholders in one pass so text supplied for
|
||||
@@ -82,50 +68,6 @@ func expandNotification(template string, event []string) string {
|
||||
).Replace(template)
|
||||
}
|
||||
|
||||
func setup_fifo(fn string) (chan string, error) {
|
||||
t := make(chan string)
|
||||
if fn == "" {
|
||||
return t, nil
|
||||
}
|
||||
if info, err := os.Lstat(fn); err == nil {
|
||||
if info.Mode()&os.ModeNamedPipe == 0 {
|
||||
return t, fmt.Errorf("FIFO path %q already exists and is not a FIFO", fn)
|
||||
}
|
||||
if err := os.Remove(fn); err != nil {
|
||||
return t, err
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return t, err
|
||||
}
|
||||
err := syscall.Mkfifo(fn, 0600)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
file, err := os.OpenFile(fn, os.O_RDWR, os.ModeNamedPipe)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
go readFIFO(file, t)
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// readFIFO forwards complete commands and terminates on EOF or any read
|
||||
// failure. Retrying an unrecoverable FIFO error used to spin a CPU forever.
|
||||
func readFIFO(fh io.ReadCloser, out chan<- string) {
|
||||
defer fh.Close()
|
||||
defer close(out)
|
||||
reader := bufio.NewReader(fh)
|
||||
for {
|
||||
line, err := reader.ReadBytes('\n')
|
||||
if len(line) != 0 {
|
||||
out <- strings.TrimSpace(string(line))
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Command line flags
|
||||
server := flag.String("server", "localhost:64738", "the server to connect to")
|
||||
|
||||
Reference in New Issue
Block a user