format

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 11 Imported by: 0

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 {
			presenter.Error("Failed to get current working directory: %v", err)

			return fmt.Errorf("failed to get working directory: %w", err)
		}

		projType, err := project.Detect(cwd)
		if err != nil {
			presenter.Error("Failed to detect project type: %v", err)

			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")

			lintArgs := []string{"run", "--fix"}
			if len(args) > 0 {
				lintArgs = append(lintArgs, args...)
			}

			err := runFormatCommand(
				ctx,
				presenter,
				globals.ExecClient,
				cwd,
				"golangci-lint",
				lintArgs,
			)
			if err != nil {
				presenter.Warning(
					"'golangci-lint --fix' completed but may have found unfixable issues.",
				)
			} else {
				presenter.Success("✓ golangci-lint completed.")
			}

		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.

Jump to

Keyboard shortcuts

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