|
| 1 | +package cli |
| 2 | + |
| 3 | +import "github.com/lu-zhengda/lanchr/internal/agent" |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Service JSON types (list, search, info) |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +// jsonService is the compact representation used by list and search. |
| 10 | +type jsonService struct { |
| 11 | + Label string `json:"label"` |
| 12 | + Domain string `json:"domain"` |
| 13 | + Type string `json:"type"` |
| 14 | + Status string `json:"status"` |
| 15 | + PID int `json:"pid"` |
| 16 | + LastExitStatus int `json:"last_exit_status"` |
| 17 | + PlistPath string `json:"plist_path,omitempty"` |
| 18 | + Program string `json:"program,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +// toJSONServices converts a slice of agent.Service to JSON-serializable form. |
| 22 | +func toJSONServices(services []agent.Service) []jsonService { |
| 23 | + out := make([]jsonService, 0, len(services)) |
| 24 | + for _, svc := range services { |
| 25 | + out = append(out, jsonService{ |
| 26 | + Label: svc.Label, |
| 27 | + Domain: svc.Domain.String(), |
| 28 | + Type: svc.Type.String(), |
| 29 | + Status: svc.Status.String(), |
| 30 | + PID: svc.PID, |
| 31 | + LastExitStatus: svc.LastExitStatus, |
| 32 | + PlistPath: svc.PlistPath, |
| 33 | + Program: svc.BinaryPath(), |
| 34 | + }) |
| 35 | + } |
| 36 | + return out |
| 37 | +} |
| 38 | + |
| 39 | +// jsonServiceDetail is the full representation used by info. |
| 40 | +type jsonServiceDetail struct { |
| 41 | + Label string `json:"label"` |
| 42 | + Domain string `json:"domain"` |
| 43 | + Type string `json:"type"` |
| 44 | + Status string `json:"status"` |
| 45 | + PID int `json:"pid"` |
| 46 | + LastExitStatus int `json:"last_exit_status"` |
| 47 | + PlistPath string `json:"plist_path,omitempty"` |
| 48 | + Program string `json:"program,omitempty"` |
| 49 | + ProgramArgs []string `json:"program_args,omitempty"` |
| 50 | + RunAtLoad bool `json:"run_at_load"` |
| 51 | + KeepAlive interface{} `json:"keep_alive"` |
| 52 | + StartInterval int `json:"start_interval,omitempty"` |
| 53 | + WatchPaths []string `json:"watch_paths,omitempty"` |
| 54 | + QueueDirectories []string `json:"queue_directories,omitempty"` |
| 55 | + WorkingDirectory string `json:"working_directory,omitempty"` |
| 56 | + StandardOutPath string `json:"stdout_path,omitempty"` |
| 57 | + StandardErrorPath string `json:"stderr_path,omitempty"` |
| 58 | + EnvironmentVars map[string]string `json:"environment,omitempty"` |
| 59 | + ExitTimeout int `json:"exit_timeout,omitempty"` |
| 60 | + Disabled bool `json:"disabled"` |
| 61 | + BlameLine string `json:"blame,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +// toJSONServiceDetail converts an agent.Service to its full JSON representation. |
| 65 | +func toJSONServiceDetail(svc *agent.Service) jsonServiceDetail { |
| 66 | + return jsonServiceDetail{ |
| 67 | + Label: svc.Label, |
| 68 | + Domain: svc.Domain.String(), |
| 69 | + Type: svc.Type.String(), |
| 70 | + Status: svc.Status.String(), |
| 71 | + PID: svc.PID, |
| 72 | + LastExitStatus: svc.LastExitStatus, |
| 73 | + PlistPath: svc.PlistPath, |
| 74 | + Program: svc.BinaryPath(), |
| 75 | + ProgramArgs: svc.ProgramArgs, |
| 76 | + RunAtLoad: svc.RunAtLoad, |
| 77 | + KeepAlive: svc.KeepAlive, |
| 78 | + StartInterval: svc.StartInterval, |
| 79 | + WatchPaths: svc.WatchPaths, |
| 80 | + QueueDirectories: svc.QueueDirectories, |
| 81 | + WorkingDirectory: svc.WorkingDirectory, |
| 82 | + StandardOutPath: svc.StandardOutPath, |
| 83 | + StandardErrorPath: svc.StandardErrorPath, |
| 84 | + EnvironmentVars: svc.EnvironmentVars, |
| 85 | + ExitTimeout: svc.ExitTimeout, |
| 86 | + Disabled: svc.Disabled, |
| 87 | + BlameLine: svc.BlameLine, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | +// Action JSON type (enable, disable, restart, load, unload) |
| 93 | +// --------------------------------------------------------------------------- |
| 94 | + |
| 95 | +type jsonAction struct { |
| 96 | + OK bool `json:"ok"` |
| 97 | + Action string `json:"action"` |
| 98 | + Label string `json:"label"` |
| 99 | +} |
| 100 | + |
| 101 | +// --------------------------------------------------------------------------- |
| 102 | +// Doctor JSON types |
| 103 | +// --------------------------------------------------------------------------- |
| 104 | + |
| 105 | +type jsonDoctor struct { |
| 106 | + Findings []jsonFinding `json:"findings"` |
| 107 | + Summary jsonSummary `json:"summary"` |
| 108 | +} |
| 109 | + |
| 110 | +type jsonFinding struct { |
| 111 | + Severity string `json:"severity"` |
| 112 | + Label string `json:"label"` |
| 113 | + PlistPath string `json:"plist_path,omitempty"` |
| 114 | + Message string `json:"message"` |
| 115 | + Suggestion string `json:"suggestion,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +type jsonSummary struct { |
| 119 | + Critical int `json:"critical"` |
| 120 | + Warning int `json:"warning"` |
| 121 | + OK int `json:"ok"` |
| 122 | +} |
| 123 | + |
| 124 | +// toJSONDoctor converts doctor findings to a JSON-serializable structure. |
| 125 | +func toJSONDoctor(findings []agent.Finding) jsonDoctor { |
| 126 | + critical, warning, ok := agent.CountBySeverity(findings) |
| 127 | + |
| 128 | + jf := make([]jsonFinding, 0, len(findings)) |
| 129 | + for _, f := range findings { |
| 130 | + jf = append(jf, jsonFinding{ |
| 131 | + Severity: f.Severity.String(), |
| 132 | + Label: f.Label, |
| 133 | + PlistPath: f.PlistPath, |
| 134 | + Message: f.Message, |
| 135 | + Suggestion: f.Suggestion, |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + return jsonDoctor{ |
| 140 | + Findings: jf, |
| 141 | + Summary: jsonSummary{ |
| 142 | + Critical: critical, |
| 143 | + Warning: warning, |
| 144 | + OK: ok, |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// --------------------------------------------------------------------------- |
| 150 | +// Create JSON type |
| 151 | +// --------------------------------------------------------------------------- |
| 152 | + |
| 153 | +type jsonCreate struct { |
| 154 | + OK bool `json:"ok"` |
| 155 | + Action string `json:"action"` |
| 156 | + Label string `json:"label"` |
| 157 | + PlistPath string `json:"plist_path"` |
| 158 | + Loaded bool `json:"loaded"` |
| 159 | +} |
| 160 | + |
| 161 | +// --------------------------------------------------------------------------- |
| 162 | +// Import JSON type |
| 163 | +// --------------------------------------------------------------------------- |
| 164 | + |
| 165 | +type jsonImport struct { |
| 166 | + OK bool `json:"ok"` |
| 167 | + Action string `json:"action"` |
| 168 | + Label string `json:"label"` |
| 169 | + PlistPath string `json:"plist_path"` |
| 170 | + Loaded bool `json:"loaded"` |
| 171 | +} |
| 172 | + |
| 173 | +// --------------------------------------------------------------------------- |
| 174 | +// Logs JSON type |
| 175 | +// --------------------------------------------------------------------------- |
| 176 | + |
| 177 | +type jsonLogs struct { |
| 178 | + Label string `json:"label"` |
| 179 | + Source string `json:"source"` |
| 180 | + Lines []string `json:"lines"` |
| 181 | +} |
| 182 | + |
| 183 | +// --------------------------------------------------------------------------- |
| 184 | +// Edit JSON type |
| 185 | +// --------------------------------------------------------------------------- |
| 186 | + |
| 187 | +type jsonEdit struct { |
| 188 | + OK bool `json:"ok"` |
| 189 | + Action string `json:"action"` |
| 190 | + Label string `json:"label"` |
| 191 | + PlistPath string `json:"plist_path"` |
| 192 | + ValidationOK bool `json:"validation_ok"` |
| 193 | + Reloaded bool `json:"reloaded"` |
| 194 | +} |
0 commit comments