gitFlow/cmd/gitflow/scan.go
dimitar 012fbf8ac5 feat: phase 6 — polish: completions, notifications, themes, rules, docs
Close out the plan with the remaining polish items and a full README.

Shell completions (cmd/gitflow):
- New `completion [bash|zsh|fish|powershell]` command backed by cobra's
  generators, wired into the root command

Desktop notifications (internal/notify):
- Send() dispatches to notify-send (Linux) with an osascript fallback
  (macOS); Windows is a documented no-op for now; missing notifiers are
  silent, never errors
- watch --notify sends a notification listing repositories whose state
  changed since the previous frame

Color themes (internal/presenter):
- ThemeMode (dark/light) with ParseTheme; light uses bright ANSI variants
  (90-97) that stay legible on light backgrounds; threaded through the
  table, compact, and suggestions renderers; new --theme flag validated
  and dumped by config

Custom rules (internal/rules):
- Rule{name, field (ahead|behind|stash|changes), op (==,!=,<,<=,>,>=),
  value, label} with upfront validation in both Validate and Eval
- Config gains a `rules:` section (yaml/env only, no flag), validated at
  load; matches render as a "FLAGS (custom rules)" section via a new
  presenter.Flags renderer, shown in scan and watch frames

Docs:
- readme.md fully rewritten: features, install, usage, examples, flag
  table, status classes, configuration reference, AI agent behavior and
  --ai-execute guardrails, development layout, CI, and future work
- implementation.md gains an Implementation Progress section recording
  every phase branch and the deviations from the original plan

Multi-platform:
- Verified cross-compilation for windows/amd64 and darwin/arm64; the
  notify package is split behind build tags

Testing:
- rules: validation, operator semantics, Eval ordering, invalid-rule
  errors
- presenter: ParseTheme, light-theme bright codes (and absence of
  dark-theme codes), Flags rendering (empty = silent, matches render)
- config: rules loading from file, invalid-rule rejection, bad theme and
  bad provider rejection
- notify: no-op behaviour when no notifier is installed (skipped when one
  is, to avoid firing real notifications)

Verified: go build, go vet, go test -race (10 packages), gofmt clean,
windows/darwin cross-compile, completion generation, rules + light theme
smoke test, watch --notify graceful shutdown (exit 0, no orphans).
2026-08-02 08:52:50 +02:00

136 lines
3.7 KiB
Go

package main
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"gitea.oblak.solutions/dimitar/gitFlow/internal/ai"
"gitea.oblak.solutions/dimitar/gitFlow/internal/app"
"gitea.oblak.solutions/dimitar/gitFlow/internal/config"
"gitea.oblak.solutions/dimitar/gitFlow/internal/presenter"
"gitea.oblak.solutions/dimitar/gitFlow/internal/rules"
"gitea.oblak.solutions/dimitar/gitFlow/pkg/status"
)
// newScanCmd runs a single scan pass and renders the result.
func newScanCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "scan",
Short: "Scan repositories under a directory once",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := config.Load(cmd.Flags())
if err != nil {
return err
}
// The README asks for the parent directory interactively;
// prompt only when nothing was provided and stdin is a TTY.
if !cmd.Flags().Changed("dir") && isTerminal(os.Stdin) {
if d, err := promptDir(cfg.Dir); err == nil {
cfg.Dir = d
}
}
ctx, stop := signalContext()
defer stop()
a, err := app.New(cfg)
if err != nil {
return err
}
result, err := a.ScanOnce(ctx)
if err != nil {
return err
}
opts := presenter.Options{
Color: presenter.ParseColorMode(cfg.Color),
Theme: presenter.ParseTheme(cfg.Theme),
}
if err := presenter.Present(os.Stdout, cfg.Format, result, opts); err != nil {
return err
}
// AI suggestions are a human-facing section; they are skipped
// in JSON mode so the machine-readable stream stays clean.
if cfg.AI.Enabled && cfg.Format != "json" {
renderSuggestions(ctx, cfg, opts, result)
}
if err := renderRuleFlags(opts, cfg, result); err != nil {
return err
}
return nil
},
}
config.RegisterFlags(cmd.Flags())
return cmd
}
// renderSuggestions asks the configured AI provider for next actions,
// renders them, and — when enabled — runs confirmed commands. Failures are
// warnings: a scan result is still useful without AI.
func renderSuggestions(ctx context.Context, cfg *config.Config, opts presenter.Options, result status.ScanResult) {
provider, err := ai.NewProvider(cfg.AI)
if err != nil {
fmt.Fprintf(os.Stderr, "warning: %v\n", err)
return
}
suggestions, err := provider.Suggest(ctx, result)
if err != nil {
fmt.Fprintf(os.Stderr, "warning: AI suggestions unavailable: %v\n", err)
return
}
if err := presenter.Suggestions(os.Stdout, opts, suggestions); err != nil {
fmt.Fprintf(os.Stderr, "warning: %v\n", err)
return
}
if cfg.AI.Execute && isTerminal(os.Stdin) {
if err := ai.RunConfirmed(ctx, suggestions, confirmSuggestion); err != nil {
fmt.Fprintf(os.Stderr, "warning: %v\n", err)
}
}
}
// confirmSuggestion asks the user to approve running a suggested command.
func confirmSuggestion(s ai.Suggestion) (bool, error) {
return promptYesNo(false, "Run in %s: $ %s", s.RepoPath, s.Command)
}
// renderRuleFlags evaluates the configured rules and renders any matches.
func renderRuleFlags(opts presenter.Options, cfg *config.Config, result status.ScanResult) error {
if len(cfg.Rules) == 0 {
return nil
}
flags, err := rules.Eval(cfg.Rules, result)
if err != nil {
return err
}
return presenter.Flags(os.Stdout, opts, flags)
}
// newConfigCmd prints the effective configuration.
func newConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Show the effective configuration",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg, err := config.Load(cmd.Flags())
if err != nil {
return err
}
out, err := cfg.Dump()
if err != nil {
return err
}
fmt.Print(out)
return nil
},
}
config.RegisterFlags(cmd.Flags())
return cmd
}