drone-hugo/download/download.go
Thomas Boerger b4fa179180
Compile Hugo on our own, fix extended version and refactoring
Since the extended version is only available for amd64 I have started to
build Hugo on our own in a multi stage Dockerfile. Now we are installing
both binaries, regular hugo and extended hugo, bundled into the Docker
image.

Beside that the download for regular hugo and extended hugo should also
be more solid now.

After that I have also added more aliases for the environment variables.
As mentioned on some GitHub issue we are also installing more required
packages for a downloaded extended Hugo version as this relies on
libc6-compat and libstdc++.
2020-05-15 02:13:51 +02:00

115 lines
1.7 KiB
Go

package download
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
)
var (
url = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_%s-%s.tar.gz"
)
// Get will download the specified hugo verion
func Get(version string, extended bool) (string, error) {
resp, err := http.Get(download(version, extended))
if err != nil {
return "", err
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", err
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := tempfile()
if err != nil {
return "", err
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", fmt.Errorf("no hugo binary found")
}
if err != nil {
return "", err
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
return "", err
}
return hugoPath, nil
}
}
}
func download(version string, extended bool) string {
var (
binary string
osName string
archType string
)
if extended {
binary = "hugo_extended"
} else {
binary = "hugo"
}
switch runtime.GOOS {
case "linux":
osName = "Linux"
case "windows":
osName = "Windows"
default:
osName = "unsupported"
}
switch runtime.GOARCH {
case "amd64":
archType = "64bit"
case "arm64":
archType = "arm64"
case "arm":
archType = "arm"
case "386":
archType = "32bit"
default:
archType = "unsupported"
}
return fmt.Sprintf(url, version, binary, version, osName, archType)
}
func tempfile() (string, io.WriteCloser, error) {
d, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, err
}
f, err := ioutil.TempFile(d, "")
return f.Name(), f, err
}