-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_status.go
More file actions
183 lines (162 loc) · 5.02 KB
/
Copy pathcommand_status.go
File metadata and controls
183 lines (162 loc) · 5.02 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package contexting
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
)
type StatusReport struct {
ProjectRoot string `json:"project_root"`
ConfigPath string `json:"config_path"`
ConfigExists bool `json:"config_exists"`
IndexPath string `json:"index_path"`
CachePath string `json:"cache_path"`
RuntimePath string `json:"runtime_path"`
IndexExists bool `json:"index_exists"`
IndexGeneratedAt *time.Time `json:"index_generated_at,omitempty"`
CacheExists bool `json:"cache_exists"`
CacheEntries int `json:"cache_entries,omitempty"`
WatchRunning bool `json:"watch_running"`
WatchPID int `json:"watch_pid,omitempty"`
WatchAddress string `json:"watch_address,omitempty"`
WatchStartedAt *time.Time `json:"watch_started_at,omitempty"`
}
func newStatusCommand() *cobra.Command {
var rootPath string
var jsonOut bool
cmd := &cobra.Command{
Use: "status",
Short: "Show project status: paths, index health, watch state",
RunE: func(cmd *cobra.Command, args []string) error {
var absConfigPath string
if configPath != "" {
var err error
absConfigPath, err = filepath.Abs(configPath)
if err != nil {
return fmt.Errorf("resolve config path: %w", err)
}
}
cfg, _ := LoadContextingConfig(absConfigPath)
common := defaultCommonFlags()
applyCommonConfigNoCLI(&common, cfg.Common)
common.normalize()
if rootPath == "" {
if cfg.Init.RootPath != "" {
rootPath = cfg.Init.RootPath
} else if cfg.Watch.RootPath != "" {
rootPath = cfg.Watch.RootPath
} else {
rootPath = "."
}
}
absRoot, err := filepath.Abs(rootPath)
if err != nil {
return fmt.Errorf("resolve root path: %w", err)
}
var indexPath string
if cfg.Search.IndexPath != "" {
indexPath = cfg.Search.IndexPath
} else {
indexPath = common.OutputPath
}
indexPath = resolveConfigPath(absConfigPath, indexPath)
if indexPath == "" {
indexPath = resolveProjectPath(absRoot, ".ctxt/ctx_index.json")
}
cachePath := resolveProjectPath(absRoot, common.SynonymCache)
runtimeFile := resolveProjectPath(absRoot, ".ctxt/ctx_runtime.json")
report := StatusReport{
ProjectRoot: absRoot,
ConfigPath: absConfigPath,
IndexPath: indexPath,
CachePath: cachePath,
RuntimePath: runtimeFile,
}
if absConfigPath == "" {
report.ConfigPath = resolveProjectPath(absRoot, ".ctxt/ctx_config.toml")
}
if _, err := os.Stat(report.ConfigPath); err == nil {
report.ConfigExists = true
}
if _, err := os.Stat(indexPath); err == nil {
report.IndexExists = true
if index, loadErr := LoadContextIndex(indexPath); loadErr == nil && !index.GeneratedAt.IsZero() {
report.IndexGeneratedAt = &index.GeneratedAt
}
}
if _, err := os.Stat(cachePath); err == nil {
report.CacheExists = true
if cache, loadErr := LoadSynonymCache(cachePath); loadErr == nil {
report.CacheEntries = len(cache)
}
}
if _, err := os.Stat(runtimeFile); err == nil {
if state, loadErr := LoadRuntimeState(runtimeFile); loadErr == nil {
report.WatchPID = state.PID
report.WatchAddress = state.Address
if !state.StartedAt.IsZero() {
report.WatchStartedAt = &state.StartedAt
}
if isProcessAlive(state.PID) {
report.WatchRunning = true
}
}
}
if jsonOut {
bytes, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}
printStatusReport(report)
return nil
},
}
cmd.Flags().StringVar(&rootPath, "root", "", "Project root path (defaults to current working directory)")
cmd.Flags().BoolVar(&jsonOut, "json", true, "Output status as JSON")
return cmd
}
func isProcessAlive(pid int) bool {
process, err := os.FindProcess(pid)
if err != nil {
return false
}
return process.Signal(os.Signal(nil)) == nil
}
func printStatusReport(report StatusReport) {
status := func(label, value string) {
fmt.Printf("%-18s %s\n", label+":", value)
}
boolStr := func(b bool) string {
if b {
return "yes"
}
return "no"
}
status("Project root", report.ProjectRoot)
status("Config", report.ConfigPath)
status("Config exists", boolStr(report.ConfigExists))
status("Index", report.IndexPath)
status("Index exists", boolStr(report.IndexExists))
if report.IndexGeneratedAt != nil {
status("Index generated", report.IndexGeneratedAt.Format(time.RFC3339))
}
status("Cache", report.CachePath)
status("Cache exists", boolStr(report.CacheExists))
if report.CacheExists {
status("Cache entries", fmt.Sprintf("%d", report.CacheEntries))
}
status("Runtime", report.RuntimePath)
status("Watch running", boolStr(report.WatchRunning))
if report.WatchRunning {
status("Watch PID", fmt.Sprintf("%d", report.WatchPID))
status("Watch address", report.WatchAddress)
if report.WatchStartedAt != nil {
status("Watch started", report.WatchStartedAt.Format(time.RFC3339))
}
}
}