exportupstream

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package exportupstream provides the command to export upstream module source code.

Index

Constants

This section is empty.

Variables

View Source
var ExportUpstreamCmd = &cobra.Command{
	Use:   "export-upstream",
	Short: "Exports source code of upstream dependencies defined in config.",

	Long: `Scans the Go module cache for dependencies listed in .contextvibes.yaml (under project.upstreamModules) and exports their source code to a single file for AI context.`,
	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 Upstream Context")
		presenter.Info("Target modules: %v", modules)

		var outputBuffer bytes.Buffer
		fmt.Fprintf(&outputBuffer, "--- Upstream Context Export ---\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("Context exported to: %s", outputFlag)

		return nil
	},
}

ExportUpstreamCmd represents the export-upstream 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