|
| 1 | +package doctor |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/slack-go/slack" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/triptechtravel/slackbuzz-cli/internal/auth" |
| 9 | + "github.com/triptechtravel/slackbuzz-cli/pkg/cmdutil" |
| 10 | +) |
| 11 | + |
| 12 | +// NewCmdDoctor returns the "doctor" command. |
| 13 | +func NewCmdDoctor(f *cmdutil.Factory) *cobra.Command { |
| 14 | + cmd := &cobra.Command{ |
| 15 | + Use: "doctor", |
| 16 | + Short: "Check token health and required scopes", |
| 17 | + Long: `Validate bot and user tokens, then probe key API scopes to detect |
| 18 | +permission gaps before they cause errors. |
| 19 | +
|
| 20 | +Checks: |
| 21 | + - Bot token validity and channels:read scope |
| 22 | + - User token validity and channels:read scope |
| 23 | + - Reports pass/fail with remediation advice |
| 24 | +
|
| 25 | +Exit code 1 if any check fails.`, |
| 26 | + Example: ` slackbuzz doctor`, |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + return doctorRun(f) |
| 29 | + }, |
| 30 | + } |
| 31 | + return cmd |
| 32 | +} |
| 33 | + |
| 34 | +type checkResult struct { |
| 35 | + name string |
| 36 | + passed bool |
| 37 | + detail string |
| 38 | + fix string |
| 39 | +} |
| 40 | + |
| 41 | +func doctorRun(f *cmdutil.Factory) error { |
| 42 | + ios := f.IOStreams |
| 43 | + cs := ios.ColorScheme() |
| 44 | + |
| 45 | + var results []checkResult |
| 46 | + anyFailed := false |
| 47 | + |
| 48 | + // --- Bot token --- |
| 49 | + botToken, botErr := auth.GetBotToken() |
| 50 | + if botErr != nil || botToken == "" { |
| 51 | + results = append(results, checkResult{ |
| 52 | + name: "Bot token", |
| 53 | + passed: false, |
| 54 | + detail: "not configured", |
| 55 | + fix: "slackbuzz auth login --bot-token", |
| 56 | + }) |
| 57 | + anyFailed = true |
| 58 | + } else { |
| 59 | + results = append(results, checkResult{ |
| 60 | + name: "Bot token", |
| 61 | + passed: true, |
| 62 | + detail: "configured", |
| 63 | + }) |
| 64 | + |
| 65 | + // Probe channels:read with bot token |
| 66 | + botClient := slack.New(botToken) |
| 67 | + _, _, err := botClient.GetConversations(&slack.GetConversationsParameters{ |
| 68 | + Types: []string{"public_channel"}, |
| 69 | + Limit: 1, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + results = append(results, checkResult{ |
| 73 | + name: "Bot channels:read", |
| 74 | + passed: false, |
| 75 | + detail: err.Error(), |
| 76 | + fix: "Add channels:read to bot scopes at api.slack.com/apps > OAuth & Permissions", |
| 77 | + }) |
| 78 | + anyFailed = true |
| 79 | + } else { |
| 80 | + results = append(results, checkResult{ |
| 81 | + name: "Bot channels:read", |
| 82 | + passed: true, |
| 83 | + detail: "OK", |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // --- User token --- |
| 89 | + userToken, userErr := auth.GetUserToken() |
| 90 | + if userErr != nil || userToken == "" { |
| 91 | + results = append(results, checkResult{ |
| 92 | + name: "User token", |
| 93 | + passed: false, |
| 94 | + detail: "not configured", |
| 95 | + fix: "slackbuzz auth login --user-token", |
| 96 | + }) |
| 97 | + anyFailed = true |
| 98 | + } else { |
| 99 | + results = append(results, checkResult{ |
| 100 | + name: "User token", |
| 101 | + passed: true, |
| 102 | + detail: "configured", |
| 103 | + }) |
| 104 | + |
| 105 | + // Probe channels:read with user token |
| 106 | + userClient := slack.New(userToken) |
| 107 | + _, _, err := userClient.GetConversations(&slack.GetConversationsParameters{ |
| 108 | + Types: []string{"public_channel"}, |
| 109 | + Limit: 1, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + results = append(results, checkResult{ |
| 113 | + name: "User channels:read", |
| 114 | + passed: false, |
| 115 | + detail: err.Error(), |
| 116 | + fix: "Add channels:read to user scopes, or re-run: slackbuzz app create", |
| 117 | + }) |
| 118 | + anyFailed = true |
| 119 | + } else { |
| 120 | + results = append(results, checkResult{ |
| 121 | + name: "User channels:read", |
| 122 | + passed: true, |
| 123 | + detail: "OK", |
| 124 | + }) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // --- Print results --- |
| 129 | + fmt.Fprintln(ios.Out) |
| 130 | + fmt.Fprintln(ios.Out, cs.Bold("SlackBuzz Doctor")) |
| 131 | + fmt.Fprintln(ios.Out, "────────────────────────────────────────") |
| 132 | + |
| 133 | + for _, r := range results { |
| 134 | + var icon string |
| 135 | + if r.passed { |
| 136 | + icon = cs.Green("PASS") |
| 137 | + } else { |
| 138 | + icon = cs.Red("FAIL") |
| 139 | + } |
| 140 | + fmt.Fprintf(ios.Out, " [%s] %-22s %s\n", icon, r.name, r.detail) |
| 141 | + if !r.passed && r.fix != "" { |
| 142 | + fmt.Fprintf(ios.Out, " %s %s\n", cs.Yellow("Fix:"), r.fix) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fmt.Fprintln(ios.Out) |
| 147 | + |
| 148 | + if anyFailed { |
| 149 | + fmt.Fprintf(ios.ErrOut, "%s Some checks failed. See above for fixes.\n", cs.Red("!")) |
| 150 | + return &cmdutil.SilentError{Err: fmt.Errorf("doctor checks failed")} |
| 151 | + } |
| 152 | + |
| 153 | + fmt.Fprintf(ios.Out, "%s All checks passed.\n", cs.Green("✓")) |
| 154 | + return nil |
| 155 | +} |
0 commit comments