diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-10 12:37:16 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-10 12:37:16 +0800 |
| commit | 72acc4fad20ccce95ef775f862c13f29719f12ca (patch) | |
| tree | 4565d8b559a65f0a94ed2a22ebab007520f0c8ad /info.go | |
initial commit
Diffstat (limited to 'info.go')
| -rw-r--r-- | info.go | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -0,0 +1,79 @@ +package main + +import ( + "encoding/json" + "os/exec" + "strings" + "bytes" +) + +type FormulaInfo struct { + Name string `json:"name"` + Desc string `json:"desc"` + Homepage string `json:"homepage"` + License string `json:"license"` + Versions struct { + Stable string `json:"stable"` + } `json:"versions"` + Installed []struct { + Version string `json:"version"` + InstalledAsDependency bool `json:"installed_as_dependency"` + InstalledOnRequest bool `json:"installed_on_request"` + } `json:"installed"` + Outdated bool `json:"outdated"` +} + +type CaskInfo struct { + Token string `json:"token"` + Name []string `json:"name"` + Desc string `json:"desc"` + Homepage string `json:"homepage"` + Installed string `json:"installed"` + AutoUpdates bool `json:"auto_updates"` + Outdated bool `json:"outdated"` + Version string `json:"version"` +} + +// GetInfo fetches package info for both formulae and casks. +// It returns one of the two structs depending on the package type. +func GetInfo(pkg string) (*FormulaInfo, *CaskInfo, error) { + out, err := exec.Command("brew", "info", "--json=v2", pkg).Output() + if err != nil { + return nil, nil, err + } + + var data struct { + Formulae []FormulaInfo `json:"formulae"` + Casks []CaskInfo `json:"casks"` + } + + if err := json.Unmarshal(out, &data); err != nil { + return nil, nil, err + } + + if len(data.Formulae) > 0 { + return &data.Formulae[0], nil, nil + } + if len(data.Casks) > 0 { + return nil, &data.Casks[0], nil + } + + return nil, nil, nil // not found +} + +func GetReverseDependencies(pkg string) ([]string, error) { + cmd := exec.Command("brew", "uses", "--installed", pkg) + var out bytes.Buffer + cmd.Stdout = &out + if err := cmd.Run(); err != nil { + return nil, err + } + + // Each line is a formula name + lines := strings.Split(strings.TrimSpace(out.String()), "\n") + if len(lines) == 1 && lines[0] == "" { + return []string{}, nil + } + + return lines, nil +} |
