|
| 1 | +package digest |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "path/filepath" |
| 6 | +) |
| 7 | + |
| 8 | +type JSONOutput struct { |
| 9 | + Summary JSONSummary `json:"summary"` |
| 10 | + Tree []*JSONNode `json:"tree"` |
| 11 | + Files []JSONFile `json:"files"` |
| 12 | + GitInfo *JSONGitInfo `json:"git_info,omitempty"` |
| 13 | +} |
| 14 | + |
| 15 | +type JSONSummary struct { |
| 16 | + Source string `json:"source"` |
| 17 | + TotalFiles int `json:"total_files"` |
| 18 | + TotalSize int64 `json:"total_size"` |
| 19 | + TotalSizeHuman string `json:"total_size_human"` |
| 20 | + ExcludePatterns []string `json:"exclude_patterns"` |
| 21 | + IncludePatterns []string `json:"include_patterns"` |
| 22 | + MaxFileSize int64 `json:"max_file_size"` |
| 23 | +} |
| 24 | + |
| 25 | +type JSONNode struct { |
| 26 | + Name string `json:"name"` |
| 27 | + Path string `json:"path"` |
| 28 | + Type string `json:"type"` |
| 29 | + Size int64 `json:"size,omitempty"` |
| 30 | + Children []*JSONNode `json:"children,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +type JSONFile struct { |
| 34 | + Path string `json:"path"` |
| 35 | + Size int64 `json:"size"` |
| 36 | + Type string `json:"type"` |
| 37 | + Content string `json:"content"` |
| 38 | +} |
| 39 | + |
| 40 | +type JSONGitInfo struct { |
| 41 | + RepoURL string `json:"repo_url,omitempty"` |
| 42 | + Branch string `json:"branch,omitempty"` |
| 43 | + Commit string `json:"commit,omitempty"` |
| 44 | + User string `json:"user,omitempty"` |
| 45 | + RepoName string `json:"repo_name,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +func (r *Result) FormatJSON(opts IngestionOptions) ([]byte, error) { |
| 49 | + output := JSONOutput{ |
| 50 | + Summary: JSONSummary{ |
| 51 | + Source: opts.Source, |
| 52 | + TotalFiles: r.TotalFiles, |
| 53 | + TotalSize: r.TotalSize, |
| 54 | + TotalSizeHuman: formatBytes(r.TotalSize), |
| 55 | + ExcludePatterns: opts.ExcludePatterns, |
| 56 | + IncludePatterns: opts.IncludePatterns, |
| 57 | + MaxFileSize: opts.MaxFileSize, |
| 58 | + }, |
| 59 | + Tree: buildJSONTree(r.RootNode), |
| 60 | + Files: gatherJSONFiles(r.RootNode), |
| 61 | + } |
| 62 | + |
| 63 | + if r.GitInfo != nil { |
| 64 | + output.GitInfo = &JSONGitInfo{ |
| 65 | + RepoURL: r.GitInfo.RepoURL, |
| 66 | + Branch: r.GitInfo.Branch, |
| 67 | + Commit: r.GitInfo.Commit, |
| 68 | + User: r.GitInfo.User, |
| 69 | + RepoName: r.GitInfo.RepoName, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return json.MarshalIndent(output, "", " ") |
| 74 | +} |
| 75 | + |
| 76 | +func buildJSONTree(node *FileNode) []*JSONNode { |
| 77 | + if node == nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + if node.Type == NodeTypeDir && node.Children != nil { |
| 82 | + result := make([]*JSONNode, 0, len(node.Children)) |
| 83 | + for _, child := range node.Children { |
| 84 | + result = append(result, fileNodeToJSON(child)) |
| 85 | + } |
| 86 | + return result |
| 87 | + } |
| 88 | + |
| 89 | + return []*JSONNode{fileNodeToJSON(node)} |
| 90 | +} |
| 91 | + |
| 92 | +func fileNodeToJSON(node *FileNode) *JSONNode { |
| 93 | + jn := &JSONNode{ |
| 94 | + Name: node.Name, |
| 95 | + Path: filepath.ToSlash(node.Path), |
| 96 | + Type: string(node.Type), |
| 97 | + Size: node.Size, |
| 98 | + } |
| 99 | + |
| 100 | + if node.Type == NodeTypeDir && node.Children != nil { |
| 101 | + jn.Children = make([]*JSONNode, 0, len(node.Children)) |
| 102 | + for _, child := range node.Children { |
| 103 | + jn.Children = append(jn.Children, fileNodeToJSON(child)) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return jn |
| 108 | +} |
| 109 | + |
| 110 | +func gatherJSONFiles(node *FileNode) []JSONFile { |
| 111 | + var files []JSONFile |
| 112 | + gatherJSONFilesRecursive(node, &files) |
| 113 | + return files |
| 114 | +} |
| 115 | + |
| 116 | +func gatherJSONFilesRecursive(node *FileNode, files *[]JSONFile) { |
| 117 | + if node.Type == NodeTypeFile { |
| 118 | + f := JSONFile{ |
| 119 | + Path: filepath.ToSlash(node.Path), |
| 120 | + Size: node.Size, |
| 121 | + Type: string(node.Type), |
| 122 | + Content: node.Content, // always include, even if empty |
| 123 | + } |
| 124 | + *files = append(*files, f) |
| 125 | + } else if node.Type == NodeTypeNotText || node.Type == NodeTypeTooLarge { |
| 126 | + *files = append(*files, JSONFile{ |
| 127 | + Path: filepath.ToSlash(node.Path), |
| 128 | + Size: node.Size, |
| 129 | + Type: string(node.Type), |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + if node.Type == NodeTypeDir { |
| 134 | + for _, child := range node.Children { |
| 135 | + gatherJSONFilesRecursive(child, files) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments