forked from MrMarble/teledock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
151 lines (134 loc) · 4.26 KB
/
utils.go
File metadata and controls
151 lines (134 loc) · 4.26 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/rs/zerolog/log"
tb "gopkg.in/tucnak/telebot.v2"
)
// parseInt64 parses a string and converts it to int64
func parseInt64(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, err
}
return int64(i), nil
}
func isNumber(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
func parseList(options types.ContainerListOptions) []string {
containers := docker.list(options)
if len(containers) == 0 {
return []string{"No containers running"}
}
resultMsg := make([]string, len(containers))
for _, container := range containers {
message := []string{
fmt.Sprintf("%v <b>%v</b>", state[container.State], container.Names[0][1:]),
fmt.Sprintf("<code> %-8v</code><code>%v</code>", "ID:", container.ID[:12]),
fmt.Sprintf("<code> %-8v</code><code>%v</code>", "STATUS:", container.Status),
fmt.Sprintf("<code> %-8v</code><code>%v</code>", "IMAGE:", container.Image),
}
if stack, ok := container.Labels["com.docker.compose.project"]; ok {
message = append(message, fmt.Sprintf("<code> %-8v</code><code>%v</code>", "STACK:", stack))
}
resultMsg = append(resultMsg, strings.Join(message, "\n"))
}
return resultMsg
}
func getStacks() map[string][]types.Container {
var (
filters = filters.NewArgs()
stacks = map[string][]types.Container{}
)
filters.Add("label", "com.docker.compose.project")
containers := docker.list(types.ContainerListOptions{All: true, Filters: filters})
for _, container := range containers {
stacks[container.Labels["com.docker.compose.project"]] = append(stacks[container.Labels["com.docker.compose.project"]], container)
}
return stacks
}
func parseStacks() []string {
stacks := getStacks()
if len(stacks) == 0 {
return []string{"No stacks running"}
}
resultMsg := make([]string, len(stacks))
for stackName, stack := range stacks {
resultMsg = append(resultMsg, strings.Join([]string{
fmt.Sprintf("<b>%v</b>", stackName),
fmt.Sprintf("<code> %-8v</code><code>%v</code>", "SERVICES:", len(stack)),
}, "\n"))
}
return resultMsg
}
func makeContainerMenu(t *Telegram, options types.ContainerListOptions, callback string) *tb.ReplyMarkup {
buttonsPerRow := 3
containers := docker.list(options)
menu := t.bot.NewMarkup()
rowNumber := int(math.Ceil(float64(len(containers)) / float64(buttonsPerRow)))
buttons := []tb.InlineButton{}
rows := make([][]tb.InlineButton, rowNumber)
for index, container := range containers {
if index != 0 && index%buttonsPerRow == 0 {
rows = append(rows, buttons)
buttons = nil
}
btn := menu.Data(container.Names[0][1:], fmt.Sprintf("%v:%v", index, container.ID[:10]), fmt.Sprintf("%v:%v", callback, container.ID[:10])).Inline()
buttons = append(buttons, *btn)
}
if len(buttons) > 0 {
rows = append(rows, buttons)
}
menu.InlineKeyboard = rows
return menu
}
func formatStruct(data interface{}) (string, error) {
result, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}
return string(result), nil
}
func callbackResponse(t *Telegram, c *tb.Callback, err error, payload interface{}, response string) {
if err != nil {
err = t.bot.Respond(c, &tb.CallbackResponse{Text: err.Error(), ShowAlert: false})
if err != nil {
log.Fatal().Str("module", "utils").Err(err).Msg("error replying to message")
}
_, err = t.bot.Edit(c.Message, fmt.Sprintf("Container %v errored: %v", payload, err.Error()))
if err != nil {
log.Fatal().Str("module", "utils").Err(err).Msg("error editing message")
}
} else {
err := t.bot.Respond(c, &tb.CallbackResponse{Text: "", ShowAlert: false})
if err != nil {
log.Fatal().Str("module", "utils").Err(err).Msg("error replying to message")
}
_, err = t.bot.Edit(c.Message, response, tb.ModeHTML)
if err != nil {
log.Fatal().Str("module", "utils").Err(err).Msg("error editing message")
}
}
}
func chunkString(s string, chunkSize int) []string {
var chunks []string
runes := []rune(s)
if len(runes) == 0 {
return []string{s}
}
for i := 0; i < len(runes); i += chunkSize {
nn := i + chunkSize
if nn > len(runes) {
nn = len(runes)
}
chunks = append(chunks, string(runes[i:nn]))
}
return chunks
}