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++.
This commit is contained in:
Thomas Boerger 2020-05-15 02:13:51 +02:00
commit b4fa179180
No known key found for this signature in database
GPG key ID: 09745AFF9D63C79B
6 changed files with 199 additions and 138 deletions

View file

@ -6,29 +6,87 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
"github.com/pkg/errors"
)
var (
_downloadURL = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_Linux-%s.tar.gz"
url = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_%s-%s.tar.gz"
)
func downloadURL(version string, extended bool) string {
var archType string
var binary string
// 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"
@ -41,55 +99,17 @@ func downloadURL(version string, extended bool) string {
default:
archType = "unsupported"
}
return fmt.Sprintf(_downloadURL, version, binary, version, archType)
return fmt.Sprintf(url, version, binary, version, osName, archType)
}
func getTempFile() (string, io.WriteCloser, error) {
func tempfile() (string, io.WriteCloser, error) {
d, err := ioutil.TempDir("", "")
if err != nil {
return "", nil, errors.Wrap(err, "")
return "", nil, err
}
f, err := ioutil.TempFile(d, "")
return f.Name(), f, err
}
// Get will download the specified hugo verion
func Get(version string, extended bool) (string, error) {
resp, err := http.Get(downloadURL(version, extended))
if err != nil {
return "", errors.Wrap(err, "")
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", errors.Wrap(err, "")
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := getTempFile()
if err != nil {
log.Printf("ERROR: %s", err)
return "", errors.Wrap(err, "")
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", errors.New("no hugo binary found")
}
if err != nil {
return "", errors.Wrap(err, "")
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
log.Fatal(err)
}
return hugoPath, nil
}
}
}