build

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

cmd/product/build/build.go

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, args []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 err
		}
		projType, err := project.Detect(cwd)
		if err != nil {
			presenter.Error("Failed to detect project type: %v", err)
			return 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 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")
			if err := os.MkdirAll(binDir, 0o750); err != nil {
				presenter.Error("Failed to create './bin/' directory: %v", err)
				return 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