diff options
| author | eltrac <hi@eltr.ac> | 2026-05-26 09:11:09 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-05-26 09:11:09 +0800 |
| commit | 98390e99385b9886127f28c0d521b3f3ddb94ccc (patch) | |
| tree | 779e1b5f305e182d6282ca9c8ffa092be62619eb /brewapi.go | |
| parent | 6fd89e79733f8cf43dddc0998f4c375538751425 (diff) | |
feat: add function fetching info from HTTP API
Diffstat (limited to 'brewapi.go')
| -rw-r--r-- | brewapi.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/brewapi.go b/brewapi.go new file mode 100644 index 0000000..d520733 --- /dev/null +++ b/brewapi.go @@ -0,0 +1,43 @@ +package main + +import ( + "net/http" + "io" + "encoding/json" + "github.com/fatih/color" +) + +func GetFormulaFromAPI(pkg string) (*FormulaInfo) { + resp, err := http.Get("https://formulae.brew.sh/api/formula/" + pkg + ".json") + if err != nil { + print(err) + color.Red("❌ Error fetching formula info from brew.sh") + return nil + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + + var data FormulaInfo + if err := json.Unmarshal(body, &data); err != nil { + color.Red("❌ Error parsing API response from brew.sh") + } + + return &data +} + +func GetCaskFromAPI(pkg string) (*CaskInfo) { + resp, err := http.Get("https://formulae.brew.sh/api/cask/" + pkg + ".json") + if err != nil { + color.Red("❌ Error fetching formula info from brew.sh") + return nil + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + + var data CaskInfo + if err := json.Unmarshal(body, &data); err != nil { + color.Red("❌ Error parsing API response from brew.sh") + } + + return &data +} |
