🏥 HealthCheck
执行一次性全面的项目健康检查,汇总环境、配置、过时包、安全漏洞与废弃包状态。
何时使用
需要给项目出一键式健康报告(CI 门禁、监控面板、部署前自检)时使用,无需手动组合多个单独命令。
签名
go
func (c *Composer) HealthCheck() (*HealthStatus, error)参数
| 参数 | 类型 | 说明 |
|---|---|---|
| 无 | — | 该方法不接收参数 |
返回值
*HealthStatus:健康状态,包含环境/文件/依赖各项检查字段及OverallStatuserror:执行过程中的错误
go
type HealthStatus struct {
ComposerInstalled bool `json:"composer_installed"`
ComposerVersion string `json:"composer_version,omitempty"`
PHPAvailable bool `json:"php_available"`
PHPVersion string `json:"php_version,omitempty"`
HasComposerJson bool `json:"has_composer_json"`
HasComposerLock bool `json:"has_composer_lock"`
HasVendorDir bool `json:"has_vendor_dir"`
Valid bool `json:"valid,omitempty"`
OutdatedCount int `json:"outdated_count,omitempty"`
VulnerabilityCount int `json:"vulnerability_count,omitempty"`
AbandonedCount int `json:"abandoned_count,omitempty"`
OverallStatus string `json:"overall_status"` // "healthy" / "warning" / "critical"
Issues []string `json:"issues,omitempty"`
}示例
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
health, err := comp.HealthCheck()
if err != nil {
log.Fatalf("健康检查失败: %v", err)
}
fmt.Printf("总体状态: %s\n", health.OverallStatus)
fmt.Printf("Composer %s, PHP %s\n", health.ComposerVersion, health.PHPVersion)
for _, issue := range health.Issues {
fmt.Println("- " + issue)
}
}进阶
OverallStatus取值:healthy(全部通过)、warning(过时/废弃包等非致命问题)、critical(Composer/PHP 缺失、安全漏洞、配置无效)- 检查项内部复用 GetPHPVersion 风格的环境探测、
ValidateStructured、GetOutdatedInfo、GetAuditInfo、GetAbandonedPackagesFromLock - 若需 JSON 形式输出,使用
GetHealthAsJSON(见 health_check.go) - 单项诊断可改用 DiagnoseStructured