summaryrefslogtreecommitdiff
path: root/brewapi.go
blob: 118840f486b37fbbb280c5e7d168aa7e1c74beab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 {
		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 {
		return nil
	}
	
	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 {
		return nil
	}
	
	return &data
}