format

package
v0.6.0 Latest Latest
Warning

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

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

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Jump to

Keyboard shortcuts

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