|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + _ "embed" |
| 8 | +) |
| 9 | + |
| 10 | +// EmbeddedCommandExampleManifest contains the checked-in import/follow example catalog. |
| 11 | +// |
| 12 | +//go:embed testdata/command-example-manifest.txt |
| 13 | +var EmbeddedCommandExampleManifest string |
| 14 | + |
| 15 | +type listCommandExamplesOptions struct { |
| 16 | + JSON bool |
| 17 | +} |
| 18 | + |
| 19 | +type commandExampleManifestEntry struct { |
| 20 | + Command string `json:"command"` |
| 21 | + Name string `json:"name"` |
| 22 | + RelativePath string `json:"path"` |
| 23 | + Format string `json:"format"` |
| 24 | +} |
| 25 | + |
| 26 | +type commandExampleManifestReport struct { |
| 27 | + Version string `json:"version"` |
| 28 | + ExampleCount int `json:"example_count"` |
| 29 | + Examples []commandExampleManifestEntry `json:"examples"` |
| 30 | +} |
| 31 | + |
| 32 | +func parseListCommandExamplesOptions(args []string) (listCommandExamplesOptions, error) { |
| 33 | + options := listCommandExamplesOptions{} |
| 34 | + for _, arg := range args { |
| 35 | + switch strings.TrimSpace(arg) { |
| 36 | + case "": |
| 37 | + continue |
| 38 | + case "--json": |
| 39 | + options.JSON = true |
| 40 | + default: |
| 41 | + return listCommandExamplesOptions{}, fmt.Errorf("unknown list-command-examples flag %q", arg) |
| 42 | + } |
| 43 | + } |
| 44 | + return options, nil |
| 45 | +} |
| 46 | + |
| 47 | +func commandExampleManifestReportFromEmbedded() (commandExampleManifestReport, error) { |
| 48 | + return parseCommandExampleManifest(EmbeddedCommandExampleManifest) |
| 49 | +} |
| 50 | + |
| 51 | +func parseCommandExampleManifest(raw string) (commandExampleManifestReport, error) { |
| 52 | + lines := strings.Split(strings.ReplaceAll(raw, "\r\n", "\n"), "\n") |
| 53 | + trimmed := make([]string, 0, len(lines)) |
| 54 | + for _, line := range lines { |
| 55 | + line = strings.TrimSpace(line) |
| 56 | + if line == "" { |
| 57 | + continue |
| 58 | + } |
| 59 | + trimmed = append(trimmed, line) |
| 60 | + } |
| 61 | + if len(trimmed) < 2 { |
| 62 | + return commandExampleManifestReport{}, fmt.Errorf("command example manifest is incomplete") |
| 63 | + } |
| 64 | + const prefix = "command example manifest " |
| 65 | + if !strings.HasPrefix(trimmed[0], prefix) { |
| 66 | + return commandExampleManifestReport{}, fmt.Errorf("invalid command example manifest header %q", trimmed[0]) |
| 67 | + } |
| 68 | + |
| 69 | + report := commandExampleManifestReport{ |
| 70 | + Version: strings.TrimSpace(strings.TrimPrefix(trimmed[0], prefix)), |
| 71 | + } |
| 72 | + for _, line := range trimmed[1:] { |
| 73 | + if strings.HasPrefix(line, "example_count=") { |
| 74 | + var count int |
| 75 | + if _, err := fmt.Sscanf(line, "example_count=%d", &count); err != nil { |
| 76 | + return commandExampleManifestReport{}, fmt.Errorf("parse command example manifest count: %w", err) |
| 77 | + } |
| 78 | + report.ExampleCount = count |
| 79 | + continue |
| 80 | + } |
| 81 | + entry, err := parseCommandExampleManifestEntry(line) |
| 82 | + if err != nil { |
| 83 | + return commandExampleManifestReport{}, err |
| 84 | + } |
| 85 | + report.Examples = append(report.Examples, entry) |
| 86 | + } |
| 87 | + if report.ExampleCount != len(report.Examples) { |
| 88 | + return commandExampleManifestReport{}, fmt.Errorf("command example manifest count mismatch: declared %d actual %d", report.ExampleCount, len(report.Examples)) |
| 89 | + } |
| 90 | + return report, nil |
| 91 | +} |
| 92 | + |
| 93 | +func parseCommandExampleManifestEntry(line string) (commandExampleManifestEntry, error) { |
| 94 | + fields := strings.Fields(line) |
| 95 | + entry := commandExampleManifestEntry{} |
| 96 | + for _, field := range fields { |
| 97 | + key, value, ok := strings.Cut(field, "=") |
| 98 | + if !ok { |
| 99 | + return commandExampleManifestEntry{}, fmt.Errorf("invalid command example manifest field %q", field) |
| 100 | + } |
| 101 | + switch key { |
| 102 | + case "command": |
| 103 | + entry.Command = value |
| 104 | + case "example": |
| 105 | + entry.Name = value |
| 106 | + case "format": |
| 107 | + entry.Format = value |
| 108 | + case "path": |
| 109 | + entry.RelativePath = value |
| 110 | + default: |
| 111 | + return commandExampleManifestEntry{}, fmt.Errorf("unknown command example manifest field %q", key) |
| 112 | + } |
| 113 | + } |
| 114 | + if entry.Command == "" || entry.Name == "" || entry.Format == "" || entry.RelativePath == "" { |
| 115 | + return commandExampleManifestEntry{}, fmt.Errorf("incomplete command example manifest entry %q", line) |
| 116 | + } |
| 117 | + return entry, nil |
| 118 | +} |
| 119 | + |
| 120 | +func formatCommandExampleManifestJSON(report commandExampleManifestReport) (string, error) { |
| 121 | + return marshalIndented(report) |
| 122 | +} |
0 commit comments