1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-24 04:20:07 +00:00

feat: add --json flag to analyze command for non-TTY environments (#533)

* feat: add --json flag to analyze command

* feat: implement JSON output mode for analyze

* refactor: rename jsonOutput flag to jsonMode to avoid conflict
This commit is contained in:
Noah Qin
2026-03-04 16:22:09 +08:00
committed by GitHub
parent d35f6d0665
commit c88691c2c8
2 changed files with 97 additions and 3 deletions

79
cmd/analyze/json.go Normal file
View File

@@ -0,0 +1,79 @@
//go:build darwin
package main
import (
"encoding/json"
"fmt"
"os"
"sync/atomic"
)
type jsonOutput struct {
Path string `json:"path"`
Entries []jsonEntry `json:"entries"`
TotalSize int64 `json:"total_size"`
TotalFiles int64 `json:"total_files"`
}
type jsonEntry struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
}
func runJSONMode(path string, isOverview bool) {
result := performScanForJSON(path)
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(result); err != nil {
fmt.Fprintf(os.Stderr, "failed to encode JSON: %v\n", err)
os.Exit(1)
}
}
func performScanForJSON(path string) jsonOutput {
var filesScanned, dirsScanned, bytesScanned int64
currentPath := &atomic.Value{}
currentPath.Store("")
items, err := os.ReadDir(path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read directory: %v\n", err)
os.Exit(1)
}
var entries []jsonEntry
var totalSize int64
for _, item := range items {
fullPath := path + "/" + item.Name()
var size int64
if item.IsDir() {
size = calculateDirSizeFast(fullPath, &filesScanned, &dirsScanned, &bytesScanned, currentPath)
} else {
info, err := item.Info()
if err == nil {
size = info.Size()
}
}
totalSize += size
entries = append(entries, jsonEntry{
Name: item.Name(),
Path: fullPath,
Size: size,
IsDir: item.IsDir(),
})
}
return jsonOutput{
Path: path,
Entries: entries,
TotalSize: totalSize,
TotalFiles: filesScanned,
}
}