vendor

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: 14 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 vendor provides the command to export upstream module source code as reference material. It scans dependencies defined in the configuration and consolidates their source code into a single file, treating external code as a library of knowledge for the developer and AI.

Index

Constants

This section is empty.

Variables

View Source
var VendorCmd = &cobra.Command{
	Use:   "vendor",
	Short: "Exports upstream dependency code as reference material.",

	Long: `Scans the Go module cache for dependencies listed in .contextvibes.yaml (under project.upstreamModules) and exports their source code to a single file.

This treats external code as "Library" material—reference documentation for you and the AI to understand how to use those libraries.`,
	RunE: func(_ *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(os.Stdout, os.Stderr)
		ctx := context.Background()

		modules := globals.LoadedAppConfig.Project.UpstreamModules
		if len(modules) == 0 {
			presenter.Warning("No upstream modules configured in .contextvibes.yaml.")
			presenter.Advice("Add modules to 'project.upstreamModules' list in your config file.")

			return nil
		}

		presenter.Summary("Exporting Vendor Reference Context")
		presenter.Info("Target modules: %v", modules)

		var outputBuffer bytes.Buffer
		fmt.Fprintf(&outputBuffer, "--- Upstream Vendor Context ---\n")
		fmt.Fprintf(&outputBuffer, "Generated at: %s\n\n", time.Now().Format(time.RFC3339))

		for _, mod := range modules {
			presenter.Step("Processing %s...", mod)

			modDir, modVer, err := resolveModule(ctx, mod)

			if err != nil || modDir == "" {
				presenter.Info("  Module content not found locally. Attempting to download...")
				downloadErr := globals.ExecClient.Execute(ctx, ".", "go", "mod", "download", mod)
				if downloadErr != nil {
					presenter.Warning("  Failed to download %s: %v", mod, downloadErr)

					continue
				}

				modDir, modVer, err = resolveModule(ctx, mod)
				if err != nil {
					presenter.Warning("  Skipping %s: %v", mod, err)

					continue
				}
			}

			if modDir == "" {
				presenter.Warning("  Skipping %s: Go reported an empty directory path even after download.", mod)

				continue
			}

			presenter.Detail("Location: %s", modDir)
			presenter.Detail("Version:  %s", modVer)

			err = filepath.WalkDir(modDir, func(path string, entry fs.DirEntry, err error) error {
				if err != nil {
					return err
				}
				if entry.IsDir() {

					if entry.Name() == "vendor" {
						return fs.SkipDir
					}

					return nil
				}

				if !strings.HasSuffix(path, ".go") {
					return nil
				}

				isTest := strings.HasSuffix(path, "_test.go")
				isExample := strings.HasPrefix(entry.Name(), "example") && isTest

				if isTest && !isExample {
					return nil
				}

				content, err := os.ReadFile(path)
				if err != nil {
					return fmt.Errorf("failed to read %s: %w", path, err)
				}

				relPath, _ := filepath.Rel(modDir, path)

				fmt.Fprintf(&outputBuffer, "\n================================================\n")
				fmt.Fprintf(&outputBuffer, "MODULE: %s (%s)\n", mod, modVer)
				fmt.Fprintf(&outputBuffer, "FILE:   %s\n", relPath)
				fmt.Fprintf(&outputBuffer, "================================================\n")
				outputBuffer.Write(content)
				outputBuffer.WriteString("\n")

				return nil
			})

			if err != nil {
				presenter.Error("  Failed to walk module directory: %v", err)
			}
		}

		err := tools.WriteBufferToFile(outputFlag, &outputBuffer)
		if err != nil {

			return fmt.Errorf("failed to write output file: %w", err)
		}

		presenter.Success("Reference context exported to: %s", outputFlag)

		return nil
	},
}

VendorCmd represents the library vendor 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