Skip to content

Commit

Permalink
style: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Dec 29, 2021
1 parent 0b95491 commit ec36602
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
6 changes: 3 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/rs/zerolog/log"
)

// Docker represents a docker client
// Docker represents a docker client.
type Docker struct {
cli *client.Client
ctx context.Context
Expand Down Expand Up @@ -81,8 +81,8 @@ func (d *Docker) inspect(containerID string) (*types.ContainerJSON, error) {

func (d *Docker) logs(containerID string, tail string) ([]string, error) {
var (
bytes []byte = make([]byte, 3000) // Telegram message length limit
logs []string = nil
bytes = make([]byte, 3000) // Telegram message length limit
logs []string
)
if tail != "all" && !isNumber(tail) {
tail = "10"
Expand Down
18 changes: 9 additions & 9 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var state = map[string]emoji.Emoji{
"dead": emoji.Skull,
}

const FORMATED_STR = "<code>%v</code>"
const FormatedStr = "<code>%v</code>"

// handleStart triggers when /start is sent on private
// handleStart triggers when /start is sent on private.
func (t *Telegram) handleStart(m *tb.Message) {
if !m.Private() {
return
Expand All @@ -33,7 +33,7 @@ func (t *Telegram) handleStart(m *tb.Message) {
t.send(m.Chat, "Telegram bot made by <a href='tg://user?id=256671105'>MrMarble</a>")
}

// handleList triggers when the ps command is sent
// handleList triggers when the ps command is sent.
func (t *Telegram) handleList(m *tb.Message) {
if !t.isSuperAdmin(m.Sender) {
return
Expand All @@ -42,7 +42,7 @@ func (t *Telegram) handleList(m *tb.Message) {
t.send(m.Chat, strings.Join(resultMsg, "\n\n"))
}

// handleList triggers when the psa command is sent
// handleList triggers when the psa command is sent.
func (t *Telegram) handleListAll(m *tb.Message) {
if !t.isSuperAdmin(m.Sender) {
return
Expand Down Expand Up @@ -117,9 +117,9 @@ func (t *Telegram) handleInspect(m *tb.Message) {
}
for index, chunk := range chunkString(response, 3000) {
if index == 0 {
t.reply(m, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.reply(m, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
} else {
t.send(m.Chat, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.send(m.Chat, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
}
time.Sleep(100 * time.Millisecond)
}
Expand All @@ -143,7 +143,7 @@ func (t *Telegram) handleLogs(m *tb.Message) {
if containerID == "" || !docker.isValidID(containerID) {
t.askForContainer(m, types.ContainerListOptions{All: true}, "logs")
} else {
var tail string = "10"
tail := "10"
if len(payload) > 1 {
tail = payload[1]
}
Expand All @@ -155,9 +155,9 @@ func (t *Telegram) handleLogs(m *tb.Message) {
}
for index, chunk := range logs {
if index == 0 {
t.reply(m, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.reply(m, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
} else {
t.send(m.Chat, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.send(m.Chat, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
}
time.Sleep(100 * time.Millisecond)
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

var (
bot *Telegram = nil
superAdmins []int64 = []int64{}
docker *Docker = nil
bot *Telegram
superAdmins = []int64{}
docker *Docker
)

func main() {
Expand Down
14 changes: 7 additions & 7 deletions telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"github.com/rs/zerolog/log"
)

// Telegram represents the telegram bot
// Telegram represents the telegram bot.
type Telegram struct {
bot *tb.Bot
handlersRegistered bool
}

// Command represent a telegram command
// Command represent a telegram command.
type Command struct {
Cmd string
Aliases []string
Description string
Handler interface{}
}

// NewBot returns a Telegram bot
// NewBot returns a Telegram bot.
func NewBot(token string) (*Telegram, error) {
bot, err := tb.NewBot(tb.Settings{
Token: token,
Expand All @@ -42,15 +42,15 @@ func NewBot(token string) (*Telegram, error) {
return &Telegram{bot: bot}, nil
}

// Start starts polling for telegram updates
// Start starts polling for telegram updates.
func (t *Telegram) Start() {
t.registerHandlers()

log.Info().Str("module", "telegram").Msg("start polling")
t.bot.Start()
}

// RegisterHandlers registers all the handlers
// RegisterHandlers registers all the handlers.
func (t *Telegram) registerHandlers() {
if t.handlersRegistered {
return
Expand Down Expand Up @@ -142,7 +142,7 @@ func (t *Telegram) isSuperAdmin(user *tb.User) bool {
return false
}

// send sends a message with error logging and retries
// send sends a message with error logging and retries.
func (t *Telegram) send(to tb.Recipient, what interface{}, options ...interface{}) *tb.Message {
hasParseMode := false
for _, opt := range options {
Expand Down Expand Up @@ -175,7 +175,7 @@ func (t *Telegram) send(to tb.Recipient, what interface{}, options ...interface{
}
}

// reply replies a message with error logging and retries
// reply replies a message with error logging and retries.
func (t *Telegram) reply(to *tb.Message, what interface{}, options ...interface{}) *tb.Message {
hasParseMode := false
for _, opt := range options {
Expand Down
30 changes: 15 additions & 15 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)

const COMPOSE_LABEL = "com.docker.compose.project"
const FORMATED_STR_PADDED = "<code> %-8v</code><code>%v</code>"
const ComposeLabel = "com.docker.compose.project"
const FormatedStrPadded = "<code> %-8v</code><code>%v</code>"

// parseInt64 parses a string and converts it to int64
// parseInt64 parses a string and converts it to int64.
func parseInt64(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 64)

Expand All @@ -43,12 +43,12 @@ func parseList(options types.ContainerListOptions) []string {
for _, container := range containers {
message := []string{
fmt.Sprintf("%v <b>%v</b>", state[container.State], container.Names[0][1:]),
fmt.Sprintf(FORMATED_STR_PADDED, "ID:", container.ID[:12]),
fmt.Sprintf(FORMATED_STR_PADDED, "STATUS:", container.Status),
fmt.Sprintf(FORMATED_STR_PADDED, "IMAGE:", container.Image),
fmt.Sprintf(FormatedStrPadded, "ID:", container.ID[:12]),
fmt.Sprintf(FormatedStrPadded, "STATUS:", container.Status),
fmt.Sprintf(FormatedStrPadded, "IMAGE:", container.Image),
}
if stack, ok := container.Labels[COMPOSE_LABEL]; ok {
message = append(message, fmt.Sprintf(FORMATED_STR_PADDED, "STACK:", stack))
if stack, ok := container.Labels[ComposeLabel]; ok {
message = append(message, fmt.Sprintf(FormatedStrPadded, "STACK:", stack))
}
resultMsg = append(resultMsg, strings.Join(message, "\n"))
}
Expand All @@ -72,10 +72,10 @@ func getStacks() map[string][]types.Container {
filters = filters.NewArgs()
stacks = map[string][]types.Container{}
)
filters.Add("label", COMPOSE_LABEL)
filters.Add("label", ComposeLabel)
containers := docker.list(types.ContainerListOptions{All: true, Filters: filters})
for _, container := range containers {
stacks[container.Labels[COMPOSE_LABEL]] = append(stacks[container.Labels[COMPOSE_LABEL]], container)
stacks[container.Labels[ComposeLabel]] = append(stacks[container.Labels[ComposeLabel]], container)
}
return stacks
}
Expand All @@ -89,7 +89,7 @@ func parseStacks() []string {
for stackName, stack := range stacks {
resultMsg = append(resultMsg, strings.Join([]string{
fmt.Sprintf("<b>%v</b>", stackName),
fmt.Sprintf(FORMATED_STR_PADDED, "SERVICES:", len(stack)),
fmt.Sprintf(FormatedStrPadded, "SERVICES:", len(stack)),
}, "\n"))
}
return resultMsg
Expand Down Expand Up @@ -185,9 +185,9 @@ func handleInspect(t *Telegram, c *tb.Callback, payload string) {
}
for index, chunk := range chunkString(response, 3000) {
if index == 0 {
callbackResponse(t, c, err, payload, fmt.Sprintf(FORMATED_STR, chunk))
callbackResponse(t, c, err, payload, fmt.Sprintf(FormatedStr, chunk))
} else {
t.send(c.Message.Chat, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.send(c.Message.Chat, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
}
time.Sleep(250 * time.Millisecond)
}
Expand All @@ -201,10 +201,10 @@ func handleLog(t *Telegram, c *tb.Callback, payload string) {
}
for index, chunk := range logs {
if index == 0 {
callbackResponse(t, c, err, payload, fmt.Sprintf(FORMATED_STR, chunk))
callbackResponse(t, c, err, payload, fmt.Sprintf(FormatedStr, chunk))
}
if index != 0 && chunk != "" {
t.send(c.Message.Chat, fmt.Sprintf(FORMATED_STR, chunk), tb.ModeHTML)
t.send(c.Message.Chat, fmt.Sprintf(FormatedStr, chunk), tb.ModeHTML)
}
time.Sleep(100 * time.Millisecond)
}
Expand Down

0 comments on commit ec36602

Please sign in to comment.