(name string)
| 335 | } |
| 336 | |
| 337 | func (sm *ServiceManager) StartService(name string) error { |
| 338 | sm.Mu.Lock() |
| 339 | defer sm.Mu.Unlock() |
| 340 | |
| 341 | service, exists := sm.Services[name] |
| 342 | if !exists { |
| 343 | return fmt.Errorf("can't find service: %s", name) |
| 344 | } |
| 345 | |
| 346 | service.mu.Lock() |
| 347 | defer service.mu.Unlock() |
| 348 | |
| 349 | if service.Status == "running" { |
| 350 | return fmt.Errorf("service already working: %s", name) |
| 351 | } |
| 352 | |
| 353 | service.LogBuffer = &LogBuffer{ |
| 354 | Lines: make([]string, 0), |
| 355 | MaxLines: 1000, |
| 356 | } |
| 357 | |
| 358 | var cmd *exec.Cmd |
| 359 | if runtime.GOOS == "windows" { |
| 360 | cmd = exec.CommandContext(sm.Ctx, "cmd", "/C", service.Config.Script) |
| 361 | } else { |
| 362 | cmd = exec.CommandContext(sm.Ctx, "sh", "-c", service.Config.Script) |
| 363 | } |
| 364 | |
| 365 | if service.Config.Workdir != "" { |
| 366 | if !filepath.IsAbs(service.Config.Workdir) { |
| 367 | absWorkdir := filepath.Join(sm.WorkingDir, service.Config.Workdir) |
| 368 | cmd.Dir = absWorkdir |
| 369 | } else { |
| 370 | cmd.Dir = service.Config.Workdir |
| 371 | } |
| 372 | } else { |
| 373 | cmd.Dir = sm.WorkingDir |
| 374 | } |
| 375 | |
| 376 | service.LogBuffer.ServiceName = service.Config.Title |
| 377 | service.LogBuffer.UsePTY = service.Config.UsePTY |
| 378 | |
| 379 | if !service.Config.UsePTY { |
| 380 | cmd.Stdout = service.LogBuffer |
| 381 | cmd.Stderr = service.LogBuffer |
| 382 | |
| 383 | if service.Config.AllowExecutingCommands { |
| 384 | stdin, err := cmd.StdinPipe() |
| 385 | if err != nil { |
| 386 | log.Printf("can't create stdin pipe: %v", err) |
| 387 | } else { |
| 388 | service.Stdin = stdin |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | workDir := cmd.Dir |
| 394 |
no test coverage detected