From 5a721243b75f7f807066292a6cb00dff3486d981 Mon Sep 17 00:00:00 2001 From: Zachary Tsang Date: Thu, 15 Aug 2024 11:06:46 +0800 Subject: [PATCH] Create modular and extensible CLI structure Move all command logic into command.go command reword Readability changes to command.go using String methods Uses util array --- pkg/slack/app.go | 84 +++--------------------------------------------- 1 file changed, 4 insertions(+), 80 deletions(-) diff --git a/pkg/slack/app.go b/pkg/slack/app.go index 306159e..653ebab 100644 --- a/pkg/slack/app.go +++ b/pkg/slack/app.go @@ -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" @@ -24,6 +22,7 @@ type App struct { api *slack.Client store kv.Store commandName string + cli *CLI } type ChannelInfo struct { @@ -72,6 +71,7 @@ func NewApp(logger *zap.Logger, config *Config, store kv.Store) *App { ), store: store, commandName: config.GetCommandName(), + cli: NewCLI(logger), } } @@ -178,6 +178,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(): @@ -199,84 +200,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 {