build

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 10 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 build provides the command to build the project.

Index

Constants

This section is empty.

Variables

View Source
var BuildCmd = &cobra.Command{
	Use: "build [--output <path>] [--debug]",
	Example: `  contextvibes product build                  # Build an optimized binary to ./bin/
  contextvibes product build -o myapp             # Build and name the output 'myapp'
  contextvibes product build --debug              # Build with debug symbols for Delve`,
	RunE: func(cmd *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		presenter.Summary("Building Go application binary.")

		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)
		}
		if projType != project.Go {
			presenter.Info("Build command is only applicable for Go projects. Nothing to do.")

			return nil
		}
		presenter.Info("Go project detected.")

		cmdDir := filepath.Join(cwd, "cmd")
		entries, err := os.ReadDir(cmdDir)
		if err != nil {
			if os.IsNotExist(err) {
				presenter.Error(
					"Directory './cmd/' not found. Cannot determine main package to build.",
				)

				return errors.New("cmd directory not found")
			}
			presenter.Error("Failed to read './cmd/' directory: %v", err)

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

		var mainPackageDirs []string
		for _, entry := range entries {
			if entry.IsDir() {
				mainPackageDirs = append(mainPackageDirs, entry.Name())
			}
		}

		if len(mainPackageDirs) == 0 {
			presenter.Error("No subdirectories found in './cmd/'. Cannot determine main package.")

			return errors.New("no main package found in cmd")
		}
		if len(mainPackageDirs) > 1 {
			presenter.Error("Multiple subdirectories found in './cmd/': %v", mainPackageDirs)

			return errors.New("ambiguous main package in cmd")
		}
		mainPackageName := mainPackageDirs[0]
		sourcePath := "./" + filepath.ToSlash(filepath.Join("cmd", mainPackageName))
		presenter.Info("Main package found: %s", sourcePath)

		outputPath := buildOutputFlag
		if outputPath == "" {
			binDir := filepath.Join(cwd, "bin")

			err := os.MkdirAll(binDir, 0o750)
			if err != nil {
				presenter.Error("Failed to create './bin/' directory: %v", err)

				return fmt.Errorf("failed to create bin directory: %w", err)
			}
			outputPath = filepath.Join("./bin", mainPackageName)
		}
		presenter.Info("Binary will be built to: %s", outputPath)

		buildArgs := []string{"build"}
		if !buildDebugFlag {
			presenter.Info("Compiling optimized binary (without debug symbols).")
			buildArgs = append(buildArgs, "-ldflags", "-s -w")
		} else {
			presenter.Info("Compiling with debug symbols.")
		}
		buildArgs = append(buildArgs, "-o", outputPath, sourcePath)

		presenter.Newline()
		presenter.Step("Running 'go build'...")
		err = globals.ExecClient.Execute(ctx, cwd, "go", buildArgs...)
		if err != nil {
			presenter.Error("'go build' command failed. See output above for details.")

			return errors.New("go build failed")
		}

		presenter.Newline()
		presenter.Success(
			"Build successful. Binary available at: %s",
			presenter.Highlight(outputPath),
		)
		globals.AppLogger.InfoContext(
			ctx,
			"Go build completed successfully",
			"output_path",
			outputPath,
		)

		return nil
	},
}

BuildCmd represents the build 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