@@ -29,11 +29,12 @@ import (
2929)
3030
3131var configPath = "config.yaml" // 全局可用
32- var appStatus = "未启动"
3332var extraArgs []string
3433var lastFoundArgs []string // 仅记录 FindProcessByPath 找到的参数(不含exe路径)
3534var currentAppPid int
3635
36+ var appStatus = internal .NewAppStatus (internal .AppNotStarted , 0 , 0 , "初始状态" )
37+
3738var (
3839 idleTimer * time.Timer
3940 idleTimerC <- chan time.Time
@@ -72,14 +73,6 @@ func internalGetConfig() (*internal.Config, error) {
7273 return cfg , nil
7374}
7475
75- func internalGetAppStatus () string {
76- // 用 FindProcessByPath 判断真实运行状态
77- // if pid, _, found := internal.FindProcessByPath(app.Path); found && pid != 0 {
78- // return internal.AppRunning.String()
79- // }
80- return fmt .Sprintf ("%s" , appStatus )
81- }
82-
8376func internalGetActivate () string {
8477 cfg , err := internalGetConfig ()
8578 if err != nil {
@@ -148,10 +141,10 @@ func internalKillCurrentApp() error {
148141 }
149142 err := internal .KillProcessTreeAndWait (currentAppPid )
150143 if err == nil {
151- appStatus = "已终止 (PID=" + fmt . Sprint ( currentAppPid ) + ")"
144+ appStatus = internal . NewAppStatus ( internal . AppExited , currentAppPid , 0 , "已终止" )
152145 setCurrentAppPid (0 )
153146 } else {
154- appStatus = "终止失败 (PID=" + fmt . Sprint ( currentAppPid ) + ")"
147+ appStatus = internal . NewAppStatus ( internal . AppExited , currentAppPid , 0 , "终止失败" )
155148 }
156149 return err
157150}
@@ -187,34 +180,54 @@ func runAppProxy(args []string) {
187180 fmt .Printf ("[DEBUG] finalArgs: %v\n " , finalArgs )
188181
189182 _ , err = internal .StartAppProcess (app .Path , finalArgs , func (status string , pid int , exitErr error ) {
183+ exitCode := 0
184+ if exitErr != nil {
185+ if c , ok := internal .ExtractExitCode (exitErr ); ok {
186+ exitCode = c
187+ }
188+ }
189+
190190 switch status {
191+ case "start_failed" :
192+ appStatus = internal .NewAppStatus (internal .AppExited , 0 , exitCode , "启动失败" )
193+ fmt .Printf ("启动应用失败: %v\n " , exitErr )
191194 case "running" :
192- appStatus = fmt .Sprintf ("运行中 (PID=%d)" , pid )
193195 setCurrentAppPid (pid )
196+ appStatus = internal .NewAppStatus (internal .AppRunning , pid , 0 , "运行中" )
194197 fmt .Printf ("已启动应用: %s (PID=%d)\n " , app .Path , pid )
195198 case "exited" :
196- appStatus = fmt .Sprintf ("已退出 (PID=%d)" , pid )
197- fmt .Println ("应用已正常退出" )
198199 setCurrentAppPid (0 )
200+ appStatus = internal .NewAppStatus (internal .AppExited , pid , exitCode , "已退出" )
201+ fmt .Println ("应用已正常退出" )
199202 case "exit_failed" :
200- appStatus = fmt .Sprintf ("异常退出 (PID=%d)" , pid )
201- fmt .Printf ("应用异常退出,返回码非0: %v\n " , exitErr )
202203 setCurrentAppPid (0 )
204+ code := 1
205+ if exitCode != 0 {
206+ code = exitCode
207+ }
208+ appStatus = internal .NewAppStatus (internal .AppExited , pid , code , "异常退出" )
209+ fmt .Printf ("应用异常退出,返回码非0: %v\n " , exitErr )
203210 case "killed" :
204- appStatus = fmt .Sprintf ("被终止 (PID=%d)" , pid )
205- fmt .Printf ("应用被信号终止: %v\n " , exitErr )
206211 setCurrentAppPid (0 )
212+ code := 1
213+ if exitCode != 0 {
214+ code = exitCode
215+ }
216+ appStatus = internal .NewAppStatus (internal .AppExited , pid , code , "被终止" )
217+ fmt .Printf ("应用被信号终止: %v\n " , exitErr )
207218 case "crashed" :
208- appStatus = fmt .Sprintf ("已崩溃 (PID=%d)" , pid )
209- fmt .Printf ("应用崩溃: %v\n " , exitErr )
210219 setCurrentAppPid (0 )
211- case "start_failed" :
212- appStatus = "启动失败"
213- fmt .Printf ("启动应用失败: %v\n " , exitErr )
220+ code := 1
221+ if exitCode != 0 {
222+ code = exitCode
223+ }
224+ appStatus = internal .NewAppStatus (internal .AppCrashed , pid , code , "已崩溃" )
225+ fmt .Printf ("应用崩溃: %v\n " , exitErr )
214226 }
215227 })
228+
216229 if err != nil {
217- appStatus = "启动失败"
230+ appStatus = internal . NewAppStatus ( internal . AppExited , 0 , 0 , "启动失败" )
218231 fmt .Printf ("启动应用失败: %v\n " , err )
219232 return
220233 }
@@ -263,7 +276,14 @@ func handleConsoleConn(conn net.Conn, configPath string) {
263276 case "activate" :
264277 conn .Write ([]byte (internalGetActivate ()))
265278 case "status" :
266- conn .Write ([]byte (internalGetAppStatus ()))
279+ conn .Write (
280+ // 返回详细状态字符串
281+ fmt .Appendf (nil , "%s | PID=%d | ExitCode=%d" ,
282+ appStatus .Main .String (),
283+ appStatus .Pid ,
284+ appStatus .ExitCode ,
285+ ),
286+ )
267287 case "list" :
268288 cfg , err := internalGetConfig ()
269289 if err != nil {
@@ -378,7 +398,7 @@ func main() {
378398 // 通过进程路径查找是否有已运行实例
379399 if pid , args , found := internal .FindProcessByPath (appPath ); found {
380400 shouldStart = false
381- appStatus = fmt . Sprintf ( "运行中 (PID=%d)" , pid )
401+ appStatus = internal . NewAppStatus ( internal . AppRunning , pid , 0 , "运行中" )
382402 fmt .Printf ("[DEBUG] FindProcessByPath 原始args: %v\n " , args )
383403 if len (args ) > 1 {
384404 fmt .Printf ("[DEBUG] FindProcessByPath 参数部分: %v\n " , args [1 :])
0 commit comments