diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-11 17:46:37 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-11 17:46:37 +0800 |
| commit | 363bc4114ccf8e59e7ddbe32361fd8d6eeca93ca (patch) | |
| tree | 9f15930d939490c95490d0fc21717b12eac5478a /brewget.go | |
| parent | ecf45ce14fc747a530b1ed08ac7bf7a4ab039b1a (diff) | |
refactor: separate data processing and printing - `print.go` no longer handles
retrieving atime and other data operations. It just receives info and prints. -
New `statistics.go` gather formulae and cask struct and spits out a unified data
structure for `print.go` to use. Getting atime and reverse dependencies are
moved here as well. - `info.go` is now `brewget.go`, since what it does is
basically running `brew info` and `brew uses` commands to gather raw data.
Diffstat (limited to 'brewget.go')
| -rw-r--r-- | brewget.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/brewget.go b/brewget.go new file mode 100644 index 0000000..202e985 --- /dev/null +++ b/brewget.go @@ -0,0 +1,83 @@ +package main + +import ( + "encoding/json" + "os/exec" + "strings" + "bytes" + "github.com/fatih/color" +) + +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 GetBrewInfo(pkg string) (*FormulaInfo, *CaskInfo) { + out, err := exec.Command("brew", "info", "--json=v2", pkg).Output() + if err != nil { + color.Red("❌ Error fetching info. Please check for typo.") + return nil, nil + } + + var data struct { + Formulae []FormulaInfo `json:"formulae"` + Casks []CaskInfo `json:"casks"` + } + + if err := json.Unmarshal(out, &data); err != nil { + color.Red("❌ Error parsing data.") + return nil, nil + } + + if len(data.Formulae) > 0 { + return &data.Formulae[0], nil + } + if len(data.Casks) > 0 { + return nil, &data.Casks[0] + } + + return nil, nil // not found +} + +func GetBrewUses(pkg string) ([]string) { + cmd := exec.Command("brew", "uses", "--installed", pkg) + var out bytes.Buffer + cmd.Stdout = &out + if err := cmd.Run(); err != nil { + color.Red("❌ Error fetching reverse dependencies.") + return []string{} + } + + // Each line is a formula name + lines := strings.Split(strings.TrimSpace(out.String()), "\n") + if len(lines) == 1 && lines[0] == "" { + return []string{} + } + + return lines +} |
