format

package
v0.7.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormatCmd = &cobra.Command{
	Use:   "format [paths...]",
	Short: "Applies code formatting and auto-fixes linter issues.",
	Long: `Applies standard formatting and auto-fixable linter suggestions.

MODES:
  - essential: (Default) Runs standard tools (gofmt, goimports, go mod tidy). Safe and fast.
  - style:     Runs essential + fixes style issues (revive, whitespace, etc).
  - strict:    Runs essential + fixes strict linting issues.
  - local:     Runs essential + fixes using your local .golangci.yml.
  - security:  (Same as essential - security tools rarely auto-fix).
  - complexity:(Same as essential - complexity tools rarely auto-fix).`,
	Example: `  contextvibes product format                  # Run essential formatting
  contextvibes product format --mode style     # Fix style issues
  contextvibes product format --mode local     # Use local config
  contextvibes product format cmd/factory      # Format specific package`,
	Args: cobra.ArbitraryArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		isValid := false
		for _, m := range supportedModesAsString() {
			if formatMode == m {
				isValid = true
				break
			}
		}
		if !isValid {
			return fmt.Errorf("invalid mode: %s", formatMode)
		}

		presenter.Summary("Applying code formatting and auto-fixes")
		presenter.Info("Mode: %s", formatMode)

		cwd, err := os.Getwd()
		if err != nil {
			return fmt.Errorf("failed to get working directory: %w", err)
		}

		gitClient, err := initGitClient(ctx, cwd)
		if err != nil {
			presenter.Warning("Could not initialize Git client. Safety checks disabled.")
		}

		projType, err := project.Detect(cwd)
		if err != nil {
			return fmt.Errorf("failed to detect project type: %w", err)
		}

		var reportBuffer bytes.Buffer
		initReportBuffer(&reportBuffer)

		formatErrors := runFormatters(ctx, presenter, &reportBuffer, projType, gitClient, args)

		presenter.Newline()
		if len(formatErrors) > 0 {
			return errFormattingFailed
		}

		presenter.Success("All formatting tools completed.")

		if err := dumpUpdatedFiles(ctx, gitClient, &reportBuffer, args); err != nil {
			presenter.Warning("Failed to dump updated file content: %v", err)
		}

		if err := tools.WriteBufferToFile("_contextvibes.md", &reportBuffer); err != nil {
			presenter.Error("Failed to write report: %v", err)
		} else {
			presenter.Success("Full context written to %s", presenter.Highlight("_contextvibes.md"))
		}

		return nil
	},
	SilenceErrors: true,
	SilenceUsage:  true,
}

FormatCmd represents the format command.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL