From 0ee5a689dc03203091d3d6793365e329299f1926 Mon Sep 17 00:00:00 2001 From: motoki317 Date: Sat, 29 Jun 2024 14:09:56 +0900 Subject: [PATCH] chores: format outputs --- cmd/root.go | 5 +++-- pkg/bot/command.go | 32 +++++++++++++++++++++----------- pkg/bot/help.go | 1 + pkg/bot/slack/bot.go | 2 +- pkg/bot/slack/context.go | 13 ++++++++++++- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index c878d23..f1ab347 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,8 +12,9 @@ import ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "DevOpsBot", - Short: "A traQ bot for DevOps command execution", + Use: "DevOpsBot", + Short: "A traQ bot for DevOps command execution", + SilenceUsage: true, PreRun: func(cmd *cobra.Command, args []string) { fmt.Printf("DevOpsBot v%s initializing\n", utils.Version()) }, diff --git a/pkg/bot/command.go b/pkg/bot/command.go index 89cb7c9..da3f1bb 100644 --- a/pkg/bot/command.go +++ b/pkg/bot/command.go @@ -180,7 +180,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error { if c.commandFile == "" { // Sub-commands do not match, and self-command is not defined - return ctx.ReplyBad(fmt.Sprintf("Unrecognized sub-command `%s`, try /help", subVerb)) + return ctx.ReplyBad(fmt.Sprintf("Unrecognized sub-command `%s`, try `%shelp`", subVerb, config.C.Prefix)) } } @@ -190,6 +190,7 @@ func (c *CommandInstance) Execute(ctx domain.Context) error { // If this command has sub-commands, display help var lines []string lines = append(lines, fmt.Sprintf("## `%s` Usage", c.matcher())) + lines = append(lines, "") lines = append(lines, c.HelpMessage(0)...) return ctx.ReplyBad(lines...) } else { @@ -232,11 +233,15 @@ func (c *CommandInstance) Execute(ctx domain.Context) error { ) } - return ctx.ReplySuccess( - "```", - utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit), - "```", - ) + var replyMessage []string + if buf.Len() > 0 { + replyMessage = append(replyMessage, "```") + replyMessage = append(replyMessage, utils.LimitLog(utils.SafeConvertString(buf.Bytes()), logLimit)) + replyMessage = append(replyMessage, "```") + } else { + replyMessage = append(replyMessage, "*No output*") + } + return ctx.ReplySuccess(replyMessage...) } func (dc *RootCommand) HelpMessage(_ int) []string { @@ -254,11 +259,16 @@ func (c *CommandInstance) HelpMessage(indent int) []string { var lines []string // Command (self) usage - operators := strings.Join( - lo.Map(c.operators, func(s string, _ int) string { return `:@` + s + `:` }), - "", - ) - if operators == "" { + var operators string + if config.C.Mode == "traq" { + operators = strings.Join( + lo.Map(c.operators, func(s string, _ int) string { return `:@` + s + `:` }), + "", + ) + } else { + operators = fmt.Sprintf("%d operator%s", len(c.operators), lo.Ternary(len(c.operators) == 1, "", "s")) + } + if len(c.operators) == 0 { operators = "everyone" } diff --git a/pkg/bot/help.go b/pkg/bot/help.go index acfc59a..4165a88 100644 --- a/pkg/bot/help.go +++ b/pkg/bot/help.go @@ -18,6 +18,7 @@ type HelpCommand struct { func (h *HelpCommand) Execute(ctx domain.Context) error { var lines []string lines = append(lines, fmt.Sprintf("## DevOpsBot v%s", utils.Version())) + lines = append(lines, "") lines = append(lines, h.root.HelpMessage(0)...) return ctx.ReplySuccess(lines...) } diff --git a/pkg/bot/slack/bot.go b/pkg/bot/slack/bot.go index 312fdcf..46ff68e 100644 --- a/pkg/bot/slack/bot.go +++ b/pkg/bot/slack/bot.go @@ -134,7 +134,7 @@ func (s *slackBot) handleSlashEvent(e *slack.SlashCommand) error { // Prepare a new message to add reaction to commandText := fmt.Sprintf("%s %s", e.Command, e.Text) - responseText := fmt.Sprintf("%s <@%s|%s> used slash command: %s", + responseText := fmt.Sprintf("%s (<@%s|%s>) used slash command: %s", e.UserName, e.UserID, e.UserName, commandText) _, ts, err := s.sock.PostMessage(e.ChannelID, slack.MsgOptionText(responseText, false)) diff --git a/pkg/bot/slack/context.go b/pkg/bot/slack/context.go index 40f2014..a2af462 100644 --- a/pkg/bot/slack/context.go +++ b/pkg/bot/slack/context.go @@ -45,7 +45,18 @@ func (ctx *slackContext) L() *zap.Logger { func (ctx *slackContext) sendSlackMessage(channelID string, text string) error { api := ctx.api return utils.WithRetry(ctx, 10, func(ctx context.Context) error { - _, _, err := api.PostMessage(channelID, slack.MsgOptionText(text, false)) + _, _, err := api.PostMessage(channelID, slack.MsgOptionBlocks( + slack.NewSectionBlock( + slack.NewTextBlockObject( + slack.MarkdownType, + text, + false, + false, + ), + nil, + nil, + ), + )) return err }) }