Skip to content

Commit

Permalink
chores: format outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Jun 29, 2024
1 parent 46ae05b commit 0ee5a68
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
Expand Down
32 changes: 21 additions & 11 deletions pkg/bot/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
}

Expand Down
1 change: 1 addition & 0 deletions pkg/bot/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/bot/slack/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 12 additions & 1 deletion pkg/bot/slack/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down

0 comments on commit 0ee5a68

Please sign in to comment.