-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_doctor.go
More file actions
80 lines (68 loc) · 2.1 KB
/
Copy pathcommand_doctor.go
File metadata and controls
80 lines (68 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package contexting
import (
"fmt"
"github.com/spf13/cobra"
)
func newDoctorCommand() *cobra.Command {
var opts DoctorOptions
var jsonOut bool
cmd := &cobra.Command{
Use: "doctor",
Short: "Check project/config/index health and print actionable fixes",
Long: `Checks project health — config validity, index freshness, gitignore presence, write access. Prints actionable fixes when issues are found.
Checks:
• Config file exists and is valid TOML
• Index file exists and matches current project structure
• .gitignore includes .ctxt/ directory
• Write access to the project directory
Examples:
ctxt doctor Basic health check
ctxt doctor --json JSON output for scripting`,
RunE: func(cmd *cobra.Command, args []string) error {
opts.ConfigPath = configPath
report := RunDoctor(opts)
if jsonOut {
payload, err := report.toJSON()
if err != nil {
return err
}
fmt.Println(payload)
if !report.Healthy {
return fmt.Errorf("doctor reported failing checks")
}
return nil
}
printDoctorReport(report)
if !report.Healthy {
return fmt.Errorf("doctor reported failing checks")
}
return nil
},
}
cmd.Flags().StringVar(&opts.RootPath, "root", "", "Project root path override")
cmd.Flags().StringVar(&opts.IndexPath, "index", "", "Index path override")
cmd.Flags().StringVar(&opts.CachePath, "synonym-cache", "", "Synonym cache path override")
cmd.Flags().BoolVar(&opts.WriteCheck, "write-check", true, "Check write access by creating a temporary file")
cmd.Flags().BoolVar(&jsonOut, "json", false, "Output doctor report as JSON")
return cmd
}
func printDoctorReport(report DoctorReport) {
for _, check := range report.Checks {
prefix := "[PASS]"
switch check.Status {
case DoctorWarn:
prefix = "[WARN]"
case DoctorFail:
prefix = "[FAIL]"
}
LogInfof("%s %s: %s", prefix, check.Name, check.Message)
if check.Suggestion != "" {
LogInfof("fix: %s", check.Suggestion)
}
}
if report.Healthy {
LogInfof("Doctor status: healthy")
} else {
LogWarnf("Doctor status: issues found")
}
}