Skip to content

Commit

Permalink
Create modular and extensible CLI structure
Browse files Browse the repository at this point in the history
Move all command logic into command.go

command reword

Readability changes to command.go using String methods

Uses util array
  • Loading branch information
Kuixz committed Aug 16, 2024
1 parent 4bbc282 commit 57196b3
Showing 1 changed file with 4 additions and 80 deletions.
84 changes: 4 additions & 80 deletions pkg/slack/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/oursky/github-actions-manager/pkg/kv"
"github.com/oursky/github-actions-manager/pkg/utils/array"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
Expand All @@ -24,6 +22,7 @@ type App struct {
api *slack.Client
store kv.Store
commandName string
cli *CLI
}

type ChannelInfo struct {
Expand Down Expand Up @@ -72,6 +71,7 @@ func NewApp(logger *zap.Logger, config *Config, store kv.Store) *App {
),
store: store,
commandName: config.GetCommandName(),
cli: NewCLI(logger),
}
}

Expand Down Expand Up @@ -186,6 +186,7 @@ func (a *App) Start(ctx context.Context, g *errgroup.Group) error {
}

func (a *App) messageLoop(ctx context.Context, client *socketmode.Client) {
a.cli.SetContext(ctx, a)
for {
select {
case <-ctx.Done():
Expand All @@ -207,84 +208,7 @@ func (a *App) messageLoop(ctx context.Context, client *socketmode.Client) {
zap.String("command", data.Command),
zap.String("text", data.Text),
)

if data.Command != "/"+a.commandName {
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Unknown command '%s'\n", data.Command)})
continue
}

args := strings.Split(data.Text, " ")
if len(args) < 2 {
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Please specify subcommand and repo")})
continue
}

repo := args[1]
subcommand := args[0]
conclusions := array.Unique(args[2:])
if !repoRegex.MatchString(repo) {
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Invalid repo '%s'\n", repo),
})
continue
}

switch subcommand {
case "subscribe":
filterLayers := array.Unique(args[2:])
filter, err := NewFilter(filterLayers)
if err != nil {
a.logger.Warn("failed to subscribe", zap.Error(err))
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Failed to subscribe '%s': %s\n", repo, err),
})
}

channelInfo := ChannelInfo{
channelID: data.ChannelID,
filter: filter,
}
err = a.AddChannel(ctx, repo, channelInfo)
if err != nil {
a.logger.Warn("failed to subscribe", zap.Error(err))
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Failed to subscribe '%s': %s\n", repo, err),
})
} else {
if len(conclusions) > 0 {
client.Ack(*e.Request, map[string]interface{}{
"response_type": "in_channel",
"text": fmt.Sprintf("Subscribed to '%s' with conclusions: %s\n", repo, strings.Join(conclusions, ", ")),
})
} else {
client.Ack(*e.Request, map[string]interface{}{
"response_type": "in_channel",
"text": fmt.Sprintf("Subscribed to '%s'\n", repo),
})
}
}

case "unsubscribe":
err := a.DelChannel(ctx, repo, data.ChannelID)
if err != nil {
a.logger.Warn("failed to unsubscribe", zap.Error(err))
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Failed to unsubscribe '%s': %s\n", repo, err),
})
} else {
client.Ack(*e.Request, map[string]interface{}{
"response_type": "in_channel",
"text": fmt.Sprintf("Unsubscribed from '%s'\n", repo),
})
}

default:
client.Ack(*e.Request, map[string]interface{}{
"text": fmt.Sprintf("Unknown subcommand '%s'\n", subcommand),
})
}
client.Ack(*e.Request, a.cli.Parse(data))

default:
if e.Type == socketmode.EventTypeHello {
Expand Down

0 comments on commit 57196b3

Please sign in to comment.