46 lines
863 B
Go
46 lines
863 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
const downloadURL = "https://raw.githubusercontent.com/Fyrd/caniuse/main/data.json"
|
||
|
|
||
|
// Download downloads caniuse data to path
|
||
|
func Download(path string) error {
|
||
|
res, err := http.Get(downloadURL)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not get data: %v", err)
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
|
||
|
if res.StatusCode != 200 {
|
||
|
return fmt.Errorf("could not download data: %s", res.Status)
|
||
|
}
|
||
|
|
||
|
fi, err := os.Create(path)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not create %q: %v", path, err)
|
||
|
}
|
||
|
defer fi.Close()
|
||
|
|
||
|
_, err = io.Copy(fi, res.Body)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func Load(path string) (Data, error) {
|
||
|
var data Data
|
||
|
|
||
|
fi, err := os.Open(path)
|
||
|
if err != nil {
|
||
|
return data, fmt.Errorf("could not read data: %v", err)
|
||
|
}
|
||
|
defer fi.Close()
|
||
|
|
||
|
return data, json.NewDecoder(fi).Decode(&data)
|
||
|
}
|