-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandle.go
119 lines (114 loc) · 3.19 KB
/
Handle.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"log"
"strconv"
"strings"
"github.com/firefox-boxes/boxes"
"github.com/go-vgo/robotgo"
)
func getParts(str string) []string {
partsRaw := strings.Split(str, "|")
parts := make([]string, 0, 1)
for _, p := range partsRaw {
parts = append(parts, strings.TrimSpace(p))
}
return parts
}
const HELP = `boxes-ipc help
i:ls - List Installations of Firefox
box:ls - List Boxes
box:new <icon path>|<name>|<firefox executable> - Create a new Box
box:del <id> - Delete a Box
box:attrs set <id>|<icon path>|<name>|<firefox executable> - Set the attributes for a box
exec <id> - Start a Box
help - This thing you are looking at`
func Handle(cmd string, attrDB AttrDB, p boxes.ProbeResult) string {
cmdSplit := strings.SplitN(cmd, " ", 2)
switch cmdSplit[0] {
case "i:ls":
r := ""
for _, I := range boxes.GetInstallations() {
r += I.Exec
r += "\n"
}
return strings.TrimSuffix(r, "\n")
case "box:ls":
r := ""
for _, attrs := range attrDB.GetAllBoxes() {
r += attrs.Id + "|" + attrs.Icon + "|" + attrs.Name + "|" + attrs.Exec + "\n"
}
r += "<done>"
return r
case "default:get":
return attrDB.GetDefault()
case "default:set":
attrDB.SetDefault(strings.TrimSpace(cmdSplit[1]))
return "<done>"
case "box:new":
parts := getParts(cmdSplit[1])
profileID := boxes.NewProfileSetId(p)
attrDB.AddBox(string(profileID), parts[0], parts[1], parts[2])
return string(profileID)
case "box:del":
if len(attrDB.GetAllBoxes()) > 1 {
profileID := strings.TrimSpace(cmdSplit[1])
boxes.DeleteProfile(p, boxes.ProfileID(profileID))
attrDB.DeleteBox(profileID)
if attrDB.GetDefault() == profileID {
attrDB.SetDefault(attrDB.GetAllBoxes()[0].Id)
}
return "<done>"
}
return "<only one box left>"
case "box:attrs":
cmdSplit = strings.SplitN(cmdSplit[1], " ", 2)
parts := getParts(cmdSplit[1])
switch cmdSplit[0] {
case "set":
attrDB.SetBoxAttrs(parts[0], parts[1], parts[2], parts[3])
return "<done>"
case "get":
attrs, err := attrDB.GetBoxAttrs(parts[0])
if err != nil {
panic(err)
}
return attrs.Icon + "|" + attrs.Name + "|" + attrs.Exec
default:
return "boxes-shell: command not found"
}
case "whoami":
pid, err := strconv.Atoi(strings.TrimSpace(cmdSplit[1]))
if err != nil {
panic(err)
}
targetProcessID := boxes.ProcessID(pid)
for profileID, processID := range boxes.Exec {
if targetProcessID == processID {
return string(profileID)
}
}
return "<nobody>"
case "exec":
profileID := boxes.ProfileID(strings.TrimSpace(cmdSplit[1]))
running, processID := boxes.Exec.IsRunning(profileID)
attrs, err := attrDB.GetBoxAttrs(string(profileID))
if err != nil {
log.Fatal(err)
}
if !running {
installation := boxes.Installation{Exec:attrs.Exec}
//TODO: Check if installation is legit
if !installation.IExists() {
return "<installation doesn't exist>"
}
processID = installation.ExecProfileAndDetach(profileID, p)
}
for int32(processID) != robotgo.GetPID() {
robotgo.ActivePID(int32(processID))
}
return strconv.Itoa(int(processID))
case "help":
return HELP
}
return "boxes-shell: command not found"
}