index

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IndexCmd = &cobra.Command{
	Use:   "index [flags]",
	Short: "Build or rebuild the CANARY token database",
	Long: `Scan the codebase for CANARY tokens and store metadata in SQLite database.

This enables advanced features like priority ordering, keyword search, and checkpoints.
The database is stored at .canary/canary.db by default.

The rebuild is one transaction: either the new index fully replaces the old
one, or the old one is left exactly as it was. A token the scanner cannot
parse fails the run rather than being skipped with a warning.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		prompt, _ := cmd.Flags().GetString("prompt")
		if prompt != "" {
			if _, err := utils.LoadPrompt(prompt); err != nil {
				return err
			}
		}
		dbPath, _ := cmd.Flags().GetString("db")
		rootPath, _ := cmd.Flags().GetString("root")

		projectID, err := utils.WriteProjectID(cmd, rootPath)
		if err != nil {
			return err
		}

		out := cmd.OutOrStdout()
		fmt.Fprintf(out, "Indexing CANARY tokens from: %s\n", rootPath)

		reg, err := sources.LoadFromRoot(rootPath)
		if err != nil {
			return fmt.Errorf("load .canary/project.yaml: %w", err)
		}

		ignorePatterns, err := canaryscan.LoadCanaryIgnore(rootPath)
		if err != nil {
			return fmt.Errorf("load .canaryignore: %w", err)
		}

		records, files, issues, err := canaryscan.ScanTokenRecords(rootPath, nil, nil, ignorePatterns, reg)
		if err != nil {
			return fmt.Errorf("scan %s: %w", rootPath, err)
		}
		if err := reportIssues("token", issues); err != nil {
			return err
		}

		commitHash, branch := gitMetadata(rootPath)

		indexedAt := time.Now().UTC().Format(time.RFC3339)
		tokens := make([]*storage.Token, 0, len(records))
		for _, rec := range records {
			tokens = append(tokens, toToken(rec, projectID, commitHash, branch, indexedAt))
		}

		refs, err := collectRefs(rootPath, reg, ignorePatterns)
		if err != nil {
			return err
		}

		db, err := storage.OpenRW(dbPath)
		if err != nil {
			return fmt.Errorf("open database: %w", err)
		}
		defer func() { _ = db.Close() }()

		meta := storage.IndexMeta{
			Root:         rootPath,
			ProjectID:    projectID,
			CommitSHA:    commitHash,
			ParserSchema: canaryscan.ParserSchemaVersion,
			ScanDigest:   canaryscan.ScanDigest(files),
			IndexedAt:    indexedAt,
		}

		if err := db.ReplaceIndex(projectID, tokens, refs, meta); err != nil {
			return fmt.Errorf("rebuild index: %w", err)
		}

		fmt.Fprintf(out, "Indexed %d diagram reference(s)\n", len(refs["diagram"]))
		fmt.Fprintf(out, "Indexed %d migration note(s)\n", len(refs["migrate"]))
		fmt.Fprintf(out, "\n✅ Indexed %d CANARY tokens\n", len(tokens))
		fmt.Fprintf(out, "Database: %s\n", dbPath)
		fmt.Fprintf(out, "Project: %s\n", projectID)
		if commitHash != "" {
			fmt.Fprintf(out, "Commit: %s\n", commitHash[:8])
		}
		if branch != "" {
			fmt.Fprintf(out, "Branch: %s\n", branch)
		}

		return nil
	},
}

CANARY: REQ=ENG-4307; FEATURE="IndexCmd"; ASPECT=CLI; STATUS=TESTED; TEST=TestAuditF12,TestAuditF12CleanRunCommits,TestCANARY_CP_285_IndexRespectsCanaryIgnore; UPDATED=2026-08-30

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