Skip to content

Commit a10620d

Browse files
committed
Add compare command and file-contents subcommand
1 parent 887896d commit a10620d

File tree

21 files changed

+1863
-12
lines changed

21 files changed

+1863
-12
lines changed

audit-cli/README.md

Lines changed: 191 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A Go CLI tool for extracting and analyzing code examples from MongoDB documentat
1010
- [Extract Commands](#extract-commands)
1111
- [Search Commands](#search-commands)
1212
- [Analyze Commands](#analyze-commands)
13+
- [Compare Commands](#compare-commands)
1314
- [Development](#development)
1415
- [Project Structure](#project-structure)
1516
- [Adding New Commands](#adding-new-commands)
@@ -24,8 +25,9 @@ This CLI tool helps maintain code quality across MongoDB's documentation by:
2425
1. **Extracting code examples** from RST files into individual, testable files
2526
2. **Searching extracted code** for specific patterns or substrings
2627
3. **Analyzing include relationships** to understand file dependencies
27-
4. **Following include directives** to process entire documentation trees
28-
5. **Handling MongoDB-specific conventions** like steps files, extracts, and template variables
28+
4. **Comparing file contents** across documentation versions to identify differences
29+
5. **Following include directives** to process entire documentation trees
30+
6. **Handling MongoDB-specific conventions** like steps files, extracts, and template variables
2931

3032
## Installation
3133

@@ -55,8 +57,10 @@ audit-cli
5557
│ └── code-examples
5658
├── search # Search through extracted content
5759
│ └── find-string
58-
└── analyze # Analyze RST file structures
59-
└── includes
60+
├── analyze # Analyze RST file structures
61+
│ └── includes
62+
└── compare # Compare files across versions
63+
└── file-contents
6064
```
6165

6266
### Extract Commands
@@ -238,6 +242,166 @@ This command helps writers:
238242

239243
The total file count represents **unique files** discovered through include directives. If a file is included multiple times (e.g., file A includes file C, and file B also includes file C), it is counted only once in the total. However, the tree view will show it in all locations where it appears, with subsequent occurrences marked as circular includes in verbose mode.
240244

245+
### Compare Commands
246+
247+
#### `compare file-contents`
248+
249+
Compare file contents to identify differences between files. Supports two modes:
250+
1. **Direct comparison** - Compare two specific files
251+
2. **Version comparison** - Compare the same file across multiple documentation versions
252+
253+
**Use Cases:**
254+
255+
This command helps writers:
256+
- Identify content drift across documentation versions
257+
- Verify that updates have been applied consistently
258+
- Scope maintenance work when updating shared content
259+
- Understand how files have diverged over time
260+
261+
**Basic Usage:**
262+
263+
```bash
264+
# Direct comparison of two files
265+
./audit-cli compare file-contents file1.rst file2.rst
266+
267+
# Compare with diff output
268+
./audit-cli compare file-contents file1.rst file2.rst --show-diff
269+
270+
# Version comparison across MongoDB documentation versions
271+
./audit-cli compare file-contents \
272+
/path/to/manual/manual/source/includes/example.rst \
273+
--product-dir /path/to/manual \
274+
--versions manual,upcoming,v8.0,v7.0
275+
276+
# Show which files differ
277+
./audit-cli compare file-contents \
278+
/path/to/manual/manual/source/includes/example.rst \
279+
--product-dir /path/to/manual \
280+
--versions manual,upcoming,v8.0,v7.0 \
281+
--show-paths
282+
283+
# Show detailed diffs
284+
./audit-cli compare file-contents \
285+
/path/to/manual/manual/source/includes/example.rst \
286+
--product-dir /path/to/manual \
287+
--versions manual,upcoming,v8.0,v7.0 \
288+
--show-diff
289+
290+
# Verbose output (show processing details)
291+
./audit-cli compare file-contents file1.rst file2.rst -v
292+
```
293+
294+
**Flags:**
295+
296+
- `-p, --product-dir <dir>` - Product directory path (required for version comparison)
297+
- `-V, --versions <list>` - Comma-separated list of versions (e.g., `manual,upcoming,v8.0`)
298+
- `--show-paths` - Display file paths grouped by status (matching, differing, not found)
299+
- `-d, --show-diff` - Display unified diff output (implies `--show-paths`)
300+
- `-v, --verbose` - Show detailed processing information
301+
302+
**Comparison Modes:**
303+
304+
**1. Direct Comparison (Two Files)**
305+
306+
Provide two file paths as arguments:
307+
308+
```bash
309+
./audit-cli compare file-contents path/to/file1.rst path/to/file2.rst
310+
```
311+
312+
This mode:
313+
- Compares exactly two files
314+
- Reports whether they are identical or different
315+
- Can show unified diff with `--show-diff`
316+
317+
**2. Version Comparison (Product Directory)**
318+
319+
Provide one file path plus `--product-dir` and `--versions`:
320+
321+
```bash
322+
./audit-cli compare file-contents \
323+
/path/to/manual/manual/source/includes/example.rst \
324+
--product-dir /path/to/manual \
325+
--versions manual,upcoming,v8.0
326+
```
327+
328+
This mode:
329+
- Extracts the relative path from the reference file
330+
- Resolves the same relative path in each version directory
331+
- Compares all versions against the reference file
332+
- Reports matching, differing, and missing files
333+
334+
**Version Directory Structure:**
335+
336+
The tool expects MongoDB documentation to be organized as:
337+
```
338+
product-dir/
339+
├── manual/
340+
│ └── source/
341+
│ └── includes/
342+
│ └── example.rst
343+
├── upcoming/
344+
│ └── source/
345+
│ └── includes/
346+
│ └── example.rst
347+
└── v8.0/
348+
└── source/
349+
└── includes/
350+
└── example.rst
351+
```
352+
353+
**Output Formats:**
354+
355+
**Summary** (default - no flags):
356+
- Total number of versions compared
357+
- Count of matching, differing, and missing files
358+
- Hints to use `--show-paths` or `--show-diff` for more details
359+
360+
**With --show-paths:**
361+
- Summary (as above)
362+
- List of files that match (with ✓)
363+
- List of files that differ (with ✗)
364+
- List of files not found (with -)
365+
366+
**With --show-diff:**
367+
- Summary and paths (as above)
368+
- Unified diff output for each differing file
369+
- Shows added lines (prefixed with +)
370+
- Shows removed lines (prefixed with -)
371+
- Shows context lines around changes
372+
373+
**Examples:**
374+
375+
```bash
376+
# Check if a file is consistent across all versions
377+
./audit-cli compare file-contents \
378+
~/workspace/docs-mongodb-internal/content/manual/manual/source/includes/fact-atlas-search.rst \
379+
--product-dir ~/workspace/docs-mongodb-internal/content/manual \
380+
--versions manual,upcoming,v8.0,v7.0,v6.0
381+
382+
# Find differences and see what changed
383+
./audit-cli compare file-contents \
384+
~/workspace/docs-mongodb-internal/content/manual/manual/source/includes/fact-atlas-search.rst \
385+
--product-dir ~/workspace/docs-mongodb-internal/content/manual \
386+
--versions manual,upcoming,v8.0,v7.0,v6.0 \
387+
--show-diff
388+
389+
# Compare two specific versions of a file
390+
./audit-cli compare file-contents \
391+
~/workspace/docs-mongodb-internal/content/manual/manual/source/includes/example.rst \
392+
~/workspace/docs-mongodb-internal/content/manual/v8.0/source/includes/example.rst \
393+
--show-diff
394+
```
395+
396+
**Exit Codes:**
397+
398+
- `0` - Success (files compared successfully, regardless of whether they match)
399+
- `1` - Error (invalid arguments, file not found, read error, etc.)
400+
401+
**Note on Missing Files:**
402+
403+
Files that don't exist in certain versions are reported separately and do not cause errors. This is expected behavior since features may be added or removed across versions.
404+
241405
## Development
242406

243407
### Project Structure
@@ -262,13 +426,23 @@ audit-cli/
262426
│ │ ├── find_string.go # Command logic
263427
│ │ ├── types.go # Type definitions
264428
│ │ └── report.go # Report generation
265-
│ └── analyze/ # Analyze parent command
266-
│ ├── analyze.go # Parent command definition
267-
│ └── includes/ # Includes analysis subcommand
268-
│ ├── includes.go # Command logic
269-
│ ├── analyzer.go # Include tree building
429+
│ ├── analyze/ # Analyze parent command
430+
│ │ ├── analyze.go # Parent command definition
431+
│ │ └── includes/ # Includes analysis subcommand
432+
│ │ ├── includes.go # Command logic
433+
│ │ ├── analyzer.go # Include tree building
434+
│ │ ├── output.go # Output formatting
435+
│ │ └── types.go # Type definitions
436+
│ └── compare/ # Compare parent command
437+
│ ├── compare.go # Parent command definition
438+
│ └── file-contents/ # File contents comparison subcommand
439+
│ ├── file_contents.go # Command logic
440+
│ ├── file_contents_test.go # Tests
441+
│ ├── comparer.go # Comparison logic
442+
│ ├── differ.go # Diff generation
270443
│ ├── output.go # Output formatting
271-
│ └── types.go # Type definitions
444+
│ ├── types.go # Type definitions
445+
│ └── version_resolver.go # Version path resolution
272446
├── internal/ # Internal packages
273447
│ └── rst/ # RST parsing utilities
274448
│ ├── parser.go # Generic parsing with includes
@@ -281,7 +455,13 @@ audit-cli/
281455
│ ├── *.rst # Test files
282456
│ ├── includes/ # Included RST files
283457
│ └── code-examples/ # Code files for literalinclude
284-
└── expected-output/ # Expected extraction results
458+
├── expected-output/ # Expected extraction results
459+
└── compare/ # Compare command test data
460+
├── product/ # Version structure tests
461+
│ ├── manual/ # Manual version
462+
│ ├── upcoming/ # Upcoming version
463+
│ └── v8.0/ # v8.0 version
464+
└── *.txt # Direct comparison tests
285465
```
286466

287467
### Adding New Commands
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Package compare provides the parent command for comparing files across versions.
2+
//
3+
// This package serves as the parent command for various comparison operations.
4+
// Currently supports:
5+
// - file-contents: Compare file contents across different versions
6+
//
7+
// Future subcommands could include comparing metadata, structure, or other aspects.
8+
package compare
9+
10+
import (
11+
"github.com/mongodb/code-example-tooling/audit-cli/commands/compare/file-contents"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// NewCompareCommand creates the compare parent command.
16+
//
17+
// This command serves as a parent for various comparison operations on documentation files.
18+
// It doesn't perform any operations itself but provides a namespace for subcommands.
19+
func NewCompareCommand() *cobra.Command {
20+
cmd := &cobra.Command{
21+
Use: "compare",
22+
Short: "Compare files across different versions",
23+
Long: `Compare files across different versions of MongoDB documentation.
24+
25+
Currently supports comparing file contents to identify differences between
26+
the same file across multiple documentation versions. This helps writers
27+
understand how content has diverged across versions and identify maintenance work.`,
28+
}
29+
30+
// Add subcommands
31+
cmd.AddCommand(file_contents.NewFileContentsCommand())
32+
33+
return cmd
34+
}
35+

0 commit comments

Comments
 (0)