-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
122 lines (99 loc) · 2.54 KB
/
commands.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
120
121
122
package main
import (
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/gorcon/rcon"
)
// idk what is this :D
func updateModel(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
selectedData := m.table.SelectedRow()
if selectedData[0] == "Exec command" {
if !m.showInput {
m.showInput = true
m.selectedRow = m.table.Cursor()
m.textInput.SetValue("")
m.textInput.Focus()
return m, nil
} else {
m.showInput = false
command := m.textInput.Value()
log.Info("EXECCOMMAND:" + command)
output := execMinecraftRCON(command)
m.response = fmt.Sprintf("Command: %s -> %s", command, output)
}
} else if selectedData[0] == "Give admin permission" {
adminGive := os.Getenv("ADMINCOMMAND")
if !m.showInput {
m.showInput = true
m.selectedRow = m.table.Cursor()
m.textInput.SetValue("")
m.textInput.Focus()
return m, nil
} else {
m.showInput = false
user := m.textInput.Value()
log.Info("adminGive:" + adminGive)
log.Info("PERMSCOMMAND:" + strings.Replace(adminGive, "%user%", user, -1))
output := execMinecraftRCON(strings.Replace(adminGive, "%user%", user, -1))
m.response = fmt.Sprintf("permission for %s given %s", user, output)
}
} else {
response := executeAction(selectedData[0])
m.response = response
}
case "esc":
m.showInput = false
}
case tea.WindowSizeMsg:
m.table.SetWidth(msg.Width)
m.table.SetHeight(msg.Height - 4)
}
var cmd tea.Cmd
if m.showInput {
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
// detector list select
func executeAction(action string) string {
switch action {
/*TODO
case "LuckPerms editor open":
return execMinecraftRCON("lp editor")
*/
case "TPS":
return execMinecraftRCON("tps")
case "Players":
return execMinecraftRCON("list")
case "Stop server":
return execMinecraftRCON("stop")
}
return "Unkown action"
}
func execMinecraftRCON(command string) string {
hostRcon := os.Getenv("HOSTRCON")
passwordRcon := os.Getenv("PASSWORDRCON")
conn, err := rcon.Dial(hostRcon, passwordRcon)
if err != nil {
log.Error(err)
return fmt.Sprintf("ERROR %s", err)
}
defer conn.Close()
response, err := conn.Execute(command)
if err != nil {
log.Error(err)
return fmt.Sprintf("ERROR %s", err)
}
return response
}