services/magefiles/download_gotosocial.go

110 lines
2.4 KiB
Go
Raw Normal View History

//go:build mage
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/magefile/mage/sh"
)
func downloadGoToSocial(version string) error {
downloadFolder := filepath.Join(rootBuildDir, "gotosocial")
if err := os.MkdirAll(downloadFolder, 0o750); err != nil {
return fmt.Errorf("unable to make %s; %w", downloadFolder, err)
}
binaryTarUrl := fmt.Sprintf(
"https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/gotosocial_%s_linux_amd64.tar.gz",
version,
version,
)
binaryTarFilepath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_linux_amd64.tar.gz", version))
webAssetsTarUrl := fmt.Sprintf(
"https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/gotosocial_%s_web-assets.tar.gz",
version,
version,
)
webAssetsFilepath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_web-assets.tar.gz", version))
checksumUrl := fmt.Sprintf(
"https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/checksums.txt",
version,
)
checksumFilePath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_checksums.txt", version))
_, err := os.Stat(binaryTarFilepath)
if err == nil {
fmt.Printf("GoToSocial %s is already downloaded.\n", version)
return nil
}
downloads := []struct {
url string
path string
}{
{
url: binaryTarUrl,
path: binaryTarFilepath,
},
{
url: webAssetsTarUrl,
path: webAssetsFilepath,
},
{
url: checksumUrl,
path: checksumFilePath,
},
}
for _, v := range downloads {
if err := func() error {
download, err := os.Create(v.path)
if err != nil {
return fmt.Errorf("unable to create %s; %w", v.path, err)
}
defer download.Close()
client := http.Client{
CheckRedirect: func(r *http.Request, _ []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := client.Get(v.url)
if err != nil {
return err
}
defer resp.Body.Close()
size, err := io.Copy(download, resp.Body)
if err != nil {
return err
}
fmt.Printf("Downloaded %s with size %d.\n", v.path, size)
return nil
}(); err != nil {
return err
}
}
if err := os.Chdir(downloadFolder); err != nil {
return err
}
if err := sh.Run("sha256sum", "--check", "--ignore-missing", fmt.Sprintf("gotosocial_%s_checksums.txt", version)); err != nil {
return err
}
return nil
}