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 /statistics.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 'statistics.go')
| -rw-r--r-- | statistics.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/statistics.go b/statistics.go new file mode 100644 index 0000000..428d19c --- /dev/null +++ b/statistics.go @@ -0,0 +1,70 @@ +package main + +import ( + "os" + "path/filepath" + "syscall" + "time" +) + +type statistics struct { + Name string; + Type string; //formula or cask + Desc string; + Outdated bool; + + InstalledAsDependency bool; + InstalledOnRequest bool; + LastAccessTime time.Time; + + InstalledVersion string; + Homepage string; + License string; + + ReverseDependencies []string; +} + +func UnifyInfo(formula *FormulaInfo, cask *CaskInfo, pkgName string) (*statistics) { + var statObject statistics + + // === formula/cask specific === // + + if formula != nil { + statObject.Name = formula.Name + statObject.Type = "formula" + statObject.Desc = formula.Desc + statObject.Homepage = formula.Homepage + statObject.License = formula.License + statObject.Outdated = formula.Outdated + + // if installed + if len(formula.Installed) != 0 { + statObject.InstalledVersion = formula.Installed[0].Version + statObject.InstalledAsDependency = formula.Installed[0].InstalledAsDependency + statObject.InstalledOnRequest = formula.Installed[0].InstalledOnRequest + } + + // get reverse dependencies + statObject.ReverseDependencies = GetBrewUses(pkgName) + } else if cask != nil { + statObject.Name = cask.Token + statObject.Type = "cask" + statObject.Desc = cask.Desc + statObject.Homepage = cask.Homepage + statObject.InstalledVersion = cask.Installed + statObject.Outdated = cask.Outdated + } + + // === general === // + + // get system atime + binPath := filepath.Join("/opt/homebrew/bin", pkgName) + info, err := os.Stat(binPath) + if err != nil { + return nil // skip if no binary is found + } + atime := info.Sys().(*syscall.Stat_t).Atimespec + statObject.LastAccessTime = time.Unix(atime.Unix()) + + return &statObject +} |
