Documentation
¶
Overview ¶
Package format provides the command to auto-format project source code.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var FormatCmd = &cobra.Command{ Use: "format [paths...]", Example: ` contextvibes product format # Format entire project contextvibes product format cmd/factory/scrub # Format specific package`, Args: cobra.ArbitraryArgs, RunE: func(cmd *cobra.Command, args []string) error { presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr()) ctx := cmd.Context() presenter.Summary("Applying code formatting and auto-fixes.") cwd, err := os.Getwd() if err != nil { return fmt.Errorf("failed to get working directory: %w", err) } projType, err := project.Detect(cwd) if err != nil { return fmt.Errorf("failed to detect project type: %w", err) } presenter.Info("Detected project type: %s", presenter.Highlight(string(projType))) var formatErrors []error switch projType { case project.Go: presenter.Header("Go Formatting & Lint Fixes") if globals.ExecClient.CommandExists("goimports") { goimportsArgs := []string{"-w"} if len(args) > 0 { goimportsArgs = append(goimportsArgs, args...) } else { goimportsArgs = append(goimportsArgs, ".") } if err := runFormatCommand(ctx, presenter, globals.ExecClient, cwd, "goimports", goimportsArgs); err != nil { formatErrors = append(formatErrors, err) } else { presenter.Success("✓ goimports applied.") } } else { presenter.Warning("goimports not found. Install 'gotools' for better import management.") } gofmtArgs := []string{"-s", "-w"} if len(args) > 0 { gofmtArgs = append(gofmtArgs, args...) } else { gofmtArgs = append(gofmtArgs, ".") } if err := runFormatCommand(ctx, presenter, globals.ExecClient, cwd, "gofmt", gofmtArgs); err != nil { formatErrors = append(formatErrors, err) } else { presenter.Success("✓ gofmt -s applied.") } lintArgs := []string{"run", "--fix"} if len(args) > 0 { lintArgs = append(lintArgs, args...) } if err := runFormatCommand(ctx, presenter, globals.ExecClient, cwd, "golangci-lint", lintArgs); err != nil { presenter.Warning("'golangci-lint --fix' completed but may have found unfixable issues.") } else { presenter.Success("✓ golangci-lint --fix applied.") } default: presenter.Info("No formatters configured for %s", projType) } presenter.Newline() if len(formatErrors) > 0 { return errFormattingFailed } presenter.Success("All formatting and auto-fixing tools completed.") return nil }, }
FormatCmd represents the format command.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.