Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889package main
import ( "context" "io" "log/slog" "os" "os/exec" "path/filepath")
type toolFn func(ctx context.Context, filePath string, r Runner) error
func simpleFormatter(name string, args ...string) toolFn { return func(ctx context.Context, filePath string, r Runner) error { return r.ExecCommand(ctx, name, append(args, filePath)...) }}
// formatDefaults maps file extensions to their built-in formatters.var formatDefaults = map[string]toolFn{ ".bash": simpleFormatter("shfmt", "--write"), ".css": simpleFormatter("prettier", "--write"), ".go": simpleFormatter("goimports", "-w"), ".html": simpleFormatter("prettier", "--write"), ".js": simpleFormatter("prettier", "--write"), ".json": formatJSON, ".jsx": simpleFormatter("prettier", "--write"), ".md": simpleFormatter("prettier", "--write"), ".nix": simpleFormatter("alejandra", "--quiet"), ".py": simpleFormatter("ruff", "format"), ".rb": simpleFormatter("standardrb", "--fix"), ".rs": simpleFormatter("rustfmt"), ".scss": simpleFormatter("prettier", "--write"), ".sh": simpleFormatter("shfmt", "--write"), ".toml": simpleFormatter("taplo", "format"), ".ts": simpleFormatter("prettier", "--write"), ".tsx": simpleFormatter("prettier", "--write"), ".yaml": simpleFormatter("prettier", "--write"), ".yml": simpleFormatter("prettier", "--write"),}
// formatJSON formats a JSON file via jq using a temp file for safe in-place rewriting.// It does not go through Runner because it requires stdout piping.func formatJSON(_ context.Context, filePath string, _ Runner) error { tmp, err := os.CreateTemp(filepath.Dir(filePath), ".auto-tools-json-*") if err != nil { return nil //nolint:nilerr // formatter unavailable; leave file as-is } defer func() { _ = os.Remove(tmp.Name()) }() defer func() { _ = tmp.Close() }()
cmd := exec.Command("jq", ".", filePath) //nolint:gosec // G204: filePath from hook input cmd.Stdout = tmp if err := cmd.Run(); err != nil { return nil //nolint:nilerr // jq not installed or invalid JSON; leave file as-is } if err := tmp.Close(); err != nil { return nil //nolint:nilerr // formatter unavailable on flush failure; leave file as-is } _ = os.Rename(tmp.Name(), filePath) return nil}
// RunFormat reads hook JSON from stdin, resolves the formatter, and runs it.// If the project has a .auto-tools.json config, it takes precedence over the built-in defaults.// Non-zero formatter exit codes are ignored; the hook must always succeed.func RunFormat(ctx context.Context, stdin io.Reader, runner Runner) error { filePath, err := readFilePath(stdin) if err != nil || filePath == "" { return nil }
ext := filepath.Ext(filePath) slog.DebugContext(ctx, "auto-format", "file", filePath, "ext", ext)
if handled, _ := applyProjectConfig(ctx, runner, filePath, ext, "format"); handled { //nolint:errcheck // formatters never block hooks return nil }
if fn, ok := formatDefaults[ext]; ok { slog.DebugContext(ctx, "using default formatter", "ext", ext) _ = fn(ctx, filePath, runner) //nolint:errcheck // formatters never block hooks } else { slog.DebugContext(ctx, "no formatter for extension", "ext", ext) } return nil}