/
command_Execute_SSH.go
67 lines (56 loc) · 1.88 KB
/
command_Execute_SSH.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
package ssh
import (
"context"
"fmt"
"github.com/weAutomateEverything/go2hal/telegram"
"gopkg.in/telegram-bot-api.v4"
"log"
)
type sshExecute struct {
service Service
store Store
telegramStore telegram.Store
telegramService telegram.Service
}
func NewSSHExecute(service Service,
store Store,
telegramStore telegram.Store,
telegramService telegram.Service) telegram.Commandlet {
return &sshExecute{
telegramStore: telegramStore,
store: store,
service: service,
telegramService: telegramService,
}
}
func (sshExecute) CanExecute(update tgbotapi.Update, state telegram.State) bool {
return state.State == "SSH_SERVER"
}
func (s sshExecute) Execute(ctx context.Context, update tgbotapi.Update, state telegram.State) {
id, err := s.telegramStore.GetUUID(update.Message.Chat.ID, update.Message.Chat.Description)
if err != nil {
log.Println(err)
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, fmt.Sprintf("Technical Error getting group id. - please let the devs know. %v", err), update.Message.MessageID)
return
}
servers, err := s.store.getServers(id)
if err != nil {
log.Println(err)
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, fmt.Sprintf("Technical Error fetching servers - please let the devs know. %v", err), update.Message.MessageID)
return
}
for _, server := range servers {
if server.Description == update.Message.Text {
err = s.service.ExecuteRemoteCommand(ctx, id, state.Field[0], server.Address)
if err != nil {
s.telegramService.SendMessage(ctx, update.Message.Chat.ID, fmt.Sprintf("Technical Error executing commands - please let the devs know. %v", err), update.Message.MessageID)
}
}
}
}
func (sshExecute) NextState(update tgbotapi.Update, state telegram.State) string {
return ""
}
func (sshExecute) Fields(update tgbotapi.Update, state telegram.State) []string {
return nil
}