-
Notifications
You must be signed in to change notification settings - Fork 4
Add Supabase plugin for status line #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
himattm
wants to merge
4
commits into
main
Choose a base branch
from
claude/supabase-prism-plugin-ideas-amRrf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f7894f
Add Supabase plugin for status line
claude d37583c
Use Supabase brand green and show on second line
claude ad9f9ba
Move supabase to its own second line, spotify to third
claude 42eb137
Document the rationale behind default section line groupings
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/himattm/prism/internal/cache" | ||
| "github.com/himattm/prism/internal/plugin" | ||
| ) | ||
|
|
||
| // SupabasePlugin displays Supabase local dev stack status and pending migrations | ||
| type SupabasePlugin struct { | ||
| cache *cache.Cache | ||
| } | ||
|
|
||
| // supabaseConfig holds plugin configuration | ||
| type supabaseConfig struct { | ||
| showMigrations bool | ||
| showWhenStopped bool | ||
| } | ||
|
|
||
| func (p *SupabasePlugin) Name() string { | ||
| return "supabase" | ||
| } | ||
|
|
||
| func (p *SupabasePlugin) SetCache(c *cache.Cache) { | ||
| p.cache = c | ||
| } | ||
|
|
||
| // OnHook invalidates Supabase cache when Claude becomes idle | ||
| func (p *SupabasePlugin) OnHook(ctx context.Context, hookType HookType, hookCtx HookContext) (string, error) { | ||
| if hookType == HookIdle && p.cache != nil { | ||
| p.cache.DeleteByPrefix("supabase:") | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| func (p *SupabasePlugin) Execute(ctx context.Context, input plugin.Input) (string, error) { | ||
| projectDir := input.Prism.ProjectDir | ||
| if projectDir == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| // Check cache for full output | ||
| cacheKey := "supabase:output:" + projectDir | ||
| if p.cache != nil { | ||
| if cached, ok := p.cache.Get(cacheKey); ok { | ||
| return cached, nil | ||
| } | ||
| } | ||
|
|
||
| // Detect Supabase project | ||
| if !p.isSupabaseProject(projectDir) { | ||
| return "", nil | ||
| } | ||
|
|
||
| // Check CLI availability | ||
| supabasePath, err := exec.LookPath("supabase") | ||
| if err != nil { | ||
| return "", nil | ||
| } | ||
|
|
||
| cfg := parseSupabaseConfig(input.Config) | ||
|
|
||
| // Check local stack status | ||
| running := p.checkLocalStatus(ctx, supabasePath, projectDir) | ||
|
|
||
| if !running && !cfg.showWhenStopped { | ||
| return "", nil | ||
| } | ||
|
|
||
| // Build output | ||
| green := input.Colors["supabase_green"] | ||
| gray := input.Colors["gray"] | ||
| reset := input.Colors["reset"] | ||
|
|
||
| var result strings.Builder | ||
| if running { | ||
| result.WriteString(green) | ||
| } else { | ||
| result.WriteString(gray) | ||
| } | ||
| result.WriteString("⚡") | ||
|
|
||
| // Fetch migrations (idle-only for fresh data, use cache otherwise) | ||
| if running && cfg.showMigrations { | ||
| migrationKey := "supabase:migrations:" + projectDir | ||
| if input.Prism.IsIdle { | ||
| pending := countPendingMigrations(ctx, supabasePath, projectDir) | ||
| migrationFragment := "" | ||
| if pending > 0 { | ||
| migrationFragment = fmt.Sprintf(" ↑%d", pending) | ||
| } | ||
| if p.cache != nil { | ||
| p.cache.Set(migrationKey, migrationFragment, cache.ConfigTTL) | ||
| } | ||
| result.WriteString(migrationFragment) | ||
| } else if p.cache != nil { | ||
| if cached, ok := p.cache.Get(migrationKey); ok { | ||
| result.WriteString(cached) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| result.WriteString(reset) | ||
| output := result.String() | ||
|
|
||
| if p.cache != nil { | ||
| p.cache.Set(cacheKey, output, cache.SupabaseTTL) | ||
| } | ||
|
|
||
| return output, nil | ||
| } | ||
|
|
||
| // isSupabaseProject checks for supabase/config.toml in the project directory | ||
| func (p *SupabasePlugin) isSupabaseProject(projectDir string) bool { | ||
| detectKey := "supabase:detect:" + projectDir | ||
| if p.cache != nil { | ||
| if cached, ok := p.cache.Get(detectKey); ok { | ||
| return cached == "true" | ||
| } | ||
| } | ||
|
|
||
| configPath := filepath.Join(projectDir, "supabase", "config.toml") | ||
| _, err := os.Stat(configPath) | ||
| exists := err == nil | ||
|
|
||
| if p.cache != nil { | ||
| val := "false" | ||
| if exists { | ||
| val = "true" | ||
| } | ||
| p.cache.Set(detectKey, val, cache.ConfigTTL) | ||
| } | ||
|
|
||
| return exists | ||
| } | ||
|
|
||
| // checkLocalStatus runs `supabase status --output json` to determine if the local stack is running | ||
| func (p *SupabasePlugin) checkLocalStatus(ctx context.Context, supabasePath, projectDir string) bool { | ||
| cmd := exec.CommandContext(ctx, supabasePath, "status", "--output", "json") | ||
| cmd.Dir = projectDir | ||
| var out bytes.Buffer | ||
| cmd.Stdout = &out | ||
| cmd.Stderr = &bytes.Buffer{} | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| return false | ||
| } | ||
|
|
||
| // If the command succeeds and produces output, the stack is running | ||
| return out.Len() > 0 | ||
| } | ||
|
|
||
| // countPendingMigrations runs `supabase migration list` and counts unapplied migrations | ||
| func countPendingMigrations(ctx context.Context, supabasePath, projectDir string) int { | ||
| cmd := exec.CommandContext(ctx, supabasePath, "migration", "list") | ||
| cmd.Dir = projectDir | ||
| var out bytes.Buffer | ||
| cmd.Stdout = &out | ||
| cmd.Stderr = &bytes.Buffer{} | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| return 0 | ||
| } | ||
|
|
||
| return parseMigrationOutput(out.String()) | ||
| } | ||
|
|
||
| // parseMigrationOutput counts lines containing "Not Applied" in migration list output | ||
| func parseMigrationOutput(output string) int { | ||
| count := 0 | ||
| for _, line := range strings.Split(output, "\n") { | ||
| if strings.Contains(strings.ToLower(line), "not applied") { | ||
| count++ | ||
| } | ||
| } | ||
| return count | ||
| } | ||
|
|
||
| func parseSupabaseConfig(config map[string]any) supabaseConfig { | ||
| cfg := supabaseConfig{ | ||
| showMigrations: true, | ||
| showWhenStopped: false, | ||
| } | ||
|
|
||
| supabaseCfg, ok := config["supabase"].(map[string]any) | ||
| if !ok { | ||
| return cfg | ||
| } | ||
|
|
||
| if v, ok := supabaseCfg["show_migrations"].(bool); ok { | ||
| cfg.showMigrations = v | ||
| } | ||
| if v, ok := supabaseCfg["show_when_stopped"].(bool); ok { | ||
| cfg.showWhenStopped = v | ||
| } | ||
|
|
||
| return cfg | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
checkLocalStatushas two areas for improvement:out.Len() > 0is not a reliable way to determine if the service is running, especially when requesting JSON output. ThesupabaseCLI might returnnullor an empty JSON object ({}) when stopped, both of which have a length greater than 0 and would lead to an incorrect status.I recommend refactoring this to separate the parsing logic into its own function, similar to the pattern you've used for
countPendingMigrationsandparseMigrationOutput. This would allow you to add unit tests for the parsing logic to cover various outputs from thesupabaseCLI (e.g., empty string,null, valid JSON) and make the status detection more resilient.