-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.go
71 lines (55 loc) · 1.41 KB
/
mian.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"errors"
"fmt"
"log"
"net/http"
"os/exec"
)
const Port = 8090
const ShellToUse = "bash"
func runCommand(command string) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(ShellToUse, "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return stdout.String(), stderr.String(), errors.New(fmt.Sprintf("Error running command: %s", err))
}
return stdout.String(), stderr.String(), nil
}
func runShortcut(shortcutName string) error {
command := fmt.Sprintf("open 'shortcuts://run-shortcut?name=%s'", shortcutName)
log.Printf("Command: %s\n", command)
out, errOut, err := runCommand(command)
if err != nil {
log.Printf("error: %v\n", err)
}
log.Println("--- stdout ---")
log.Println(out)
log.Println("--- stderr ---")
log.Printf("%v\n\n", errOut)
return err
}
func runShortcutHandler(w http.ResponseWriter, req *http.Request) {
shortcutName := req.URL.Query().Get("name")
err := runShortcut(shortcutName)
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte("OK"))
}
func main() {
http.HandleFunc("/execute", runShortcutHandler)
log.Printf("Server started on port %d", Port)
err := http.ListenAndServe(fmt.Sprintf(":%d", Port), nil)
if err != nil {
log.Fatalf("Error starting server: %s", err)
}
}