nolint:funlen // TODO: refactor to reduce length
(cmd *cobra.Command, args []string)
| 81 | |
| 82 | //nolint:funlen // TODO: refactor to reduce length |
| 83 | func runSnapshot(cmd *cobra.Command, args []string) error { |
| 84 | flags := GetGlobalFlags() |
| 85 | |
| 86 | scanDir := ResolveScanDir(args) |
| 87 | |
| 88 | // Create scanner and scan for tasks |
| 89 | taskScanner := scanner.NewScanner(scanDir, flags.Verbose, flags.IgnoreDirs) |
| 90 | result, err := taskScanner.Scan() |
| 91 | if err != nil { |
| 92 | return fmt.Errorf("scan failed: %w", err) |
| 93 | } |
| 94 | |
| 95 | tasks := result.Tasks |
| 96 | |
| 97 | // Report any scan errors if verbose |
| 98 | if flags.Verbose && len(result.Errors) > 0 { |
| 99 | fmt.Fprintf(os.Stderr, "\nWarning: encountered %d errors during scan:\n", len(result.Errors)) |
| 100 | for _, scanErr := range result.Errors { |
| 101 | fmt.Fprintf(os.Stderr, " %s: %v\n", scanErr.FilePath, scanErr.Error) |
| 102 | } |
| 103 | fmt.Fprintln(os.Stderr) |
| 104 | } |
| 105 | |
| 106 | warnDuplicateIDs(tasks) |
| 107 | |
| 108 | // Build task map for derived fields |
| 109 | taskMap := buildTaskMap(tasks) |
| 110 | |
| 111 | // Calculate derived fields if requested |
| 112 | var depthMap map[string]int |
| 113 | var topoOrder map[string]int |
| 114 | var criticalPathTasks map[string]bool |
| 115 | |
| 116 | if snapshotDerived { |
| 117 | depthMap = calculateDepthMap(tasks, taskMap) |
| 118 | topoOrder = calculateTopologicalOrder(tasks, taskMap) |
| 119 | criticalPathTasks = calculateCriticalPathTasks(tasks, taskMap) |
| 120 | } |
| 121 | |
| 122 | // Convert tasks to snapshots |
| 123 | snapshots := make([]TaskSnapshot, 0, len(tasks)) |
| 124 | for _, task := range tasks { |
| 125 | snapshot := taskToSnapshot(task, snapshotCore, snapshotDerived, depthMap, topoOrder, criticalPathTasks, taskMap) |
| 126 | snapshots = append(snapshots, snapshot) |
| 127 | } |
| 128 | |
| 129 | // Sort snapshots by ID |
| 130 | sort.Slice(snapshots, func(i, j int) bool { |
| 131 | return snapshots[i].ID < snapshots[j].ID |
| 132 | }) |
| 133 | |
| 134 | // Prepare output |
| 135 | var output any |
| 136 | if snapshotGroupBy != "" { |
| 137 | output = SnapshotOutput{ |
| 138 | Groups: groupSnapshots(snapshots, snapshotGroupBy), |
| 139 | } |
| 140 | } else { |