update install script
This commit is contained in:
parent
454c59e8a3
commit
def3c33332
|
@ -1,11 +1,6 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hacdias/caddy-hugo/insthugo"
|
"github.com/hacdias/caddy-hugo/insthugo"
|
||||||
|
@ -23,28 +18,12 @@ type Config struct {
|
||||||
|
|
||||||
// ParseHugo parses the configuration file
|
// ParseHugo parses the configuration file
|
||||||
func ParseHugo(c *setup.Controller) (*Config, error) {
|
func ParseHugo(c *setup.Controller) (*Config, error) {
|
||||||
// First check if Hugo is installed
|
|
||||||
user, err := user.Current()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conf := &Config{
|
conf := &Config{
|
||||||
Public: strings.Replace(c.Root, "./", "", -1),
|
Public: strings.Replace(c.Root, "./", "", -1),
|
||||||
Path: "./",
|
Path: "./",
|
||||||
Hugo: user.HomeDir + "/.caddy/bin/hugo",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
conf.Hugo = insthugo.Install()
|
||||||
conf.Hugo += ".exe"
|
|
||||||
}
|
|
||||||
|
|
||||||
conf.Hugo = filepath.Clean(conf.Hugo)
|
|
||||||
|
|
||||||
if _, err := os.Stat(conf.Hugo); os.IsNotExist(err) {
|
|
||||||
insthugo.Install()
|
|
||||||
}
|
|
||||||
|
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
|
|
|
@ -3,8 +3,11 @@ package insthugo
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
@ -13,99 +16,157 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "0.15"
|
const (
|
||||||
|
version = "0.15"
|
||||||
|
baseurl = "https://github.com/spf13/hugo/releases/download/v" + version + "/"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
usr user.User
|
||||||
|
tempfiles []string
|
||||||
|
filename = "hugo_" + version + "_" + runtime.GOOS + "_" + runtime.GOARCH
|
||||||
|
sha256Hash = map[string]string{
|
||||||
|
"hugo_0.15_darwin_386.zip": "",
|
||||||
|
"hugo_0.15_darwin_amd64.zip": "",
|
||||||
|
"hugo_0.15_dragonfly_amd64.zip": "",
|
||||||
|
"hugo_0.15_freebsd_386.zip": "",
|
||||||
|
"hugo_0.15_freebsd_amd64.zip": "",
|
||||||
|
"hugo_0.15_freebsd_arm.zip": "",
|
||||||
|
"hugo_0.15_linux_386.tar.gz": "",
|
||||||
|
"hugo_0.15_linux_amd64.tar.gz": "",
|
||||||
|
"hugo_0.15_linux_arm.tar.gz": "",
|
||||||
|
"hugo_0.15_netbsd_386.zip": "",
|
||||||
|
"hugo_0.15_netbsd_amd64.zip": "",
|
||||||
|
"hugo_0.15_netbsd_arm.zip": "",
|
||||||
|
"hugo_0.15_openbsd_386.zip": "",
|
||||||
|
"hugo_0.15_openbsd_amd64.zip": "",
|
||||||
|
"hugo_0.15_windows_386_32-bit-only.zip": "0a72f9a1a929f36c0e52fb1c6272b4d37a2bd1a6bd19ce57a6e7b6803b434756",
|
||||||
|
"hugo_0.15_windows_amd64.zip": "9f03602e48ae2199e06431d7436fb3b9464538c0d44aac9a76eb98e1d4d5d727",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Install installs Hugo
|
// Install installs Hugo
|
||||||
func Install() error {
|
func Install() string {
|
||||||
// Sets the base url from where to download
|
usr, err := user.Current()
|
||||||
baseurl := "https://github.com/spf13/hugo/releases/download/v" + version + "/"
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
// The default filename
|
caddy := filepath.Clean(usr.HomeDir + "/.caddy/")
|
||||||
filename := "hugo_" + version + "_" + runtime.GOOS + "_" + runtime.GOARCH
|
bin := filepath.Clean(caddy + "/bin")
|
||||||
|
temp := filepath.Clean(caddy + "/temp")
|
||||||
|
hugo := filepath.Clean(bin + "/hugo")
|
||||||
|
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "darwin", "windows":
|
case "darwin":
|
||||||
|
filename += ".zip"
|
||||||
|
case "windows":
|
||||||
// At least for v0.15 version
|
// At least for v0.15 version
|
||||||
if runtime.GOOS == "windows" && runtime.GOARCH == "386" {
|
if runtime.GOARCH == "386" {
|
||||||
filename += "32-bit-only"
|
filename += "32-bit-only"
|
||||||
}
|
}
|
||||||
|
|
||||||
filename += ".zip"
|
filename += ".zip"
|
||||||
|
hugo += ".exe"
|
||||||
default:
|
default:
|
||||||
filename += ".tar.gz"
|
filename += ".tar.gz"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the current user home directory and creates the .caddy dir
|
// Check if Hugo is already installed
|
||||||
user, err := user.Current()
|
if _, err := os.Stat(hugo); err == nil {
|
||||||
if err != nil {
|
return hugo
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
path := user.HomeDir + "/.caddy/"
|
fmt.Println("Unable to find Hugo on " + caddy)
|
||||||
bin := path + "bin/"
|
|
||||||
temp := path + "temp/"
|
|
||||||
|
|
||||||
err = os.MkdirAll(path, 0666)
|
err = os.MkdirAll(caddy, 0666)
|
||||||
err = os.Mkdir(bin, 0666)
|
err = os.Mkdir(bin, 0666)
|
||||||
err = os.Mkdir(temp, 0666)
|
err = os.Mkdir(temp, 0666)
|
||||||
if err != nil {
|
|
||||||
return err
|
if !os.IsExist(err) {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
tempfile := temp + filename
|
tempfile := temp + "/" + filename
|
||||||
|
|
||||||
// Create the file
|
// Create the file
|
||||||
|
tempfiles = append(tempfiles, tempfile)
|
||||||
out, err := os.Create(tempfile)
|
out, err := os.Create(tempfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
clean()
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
fmt.Println("Downloading Hugo...")
|
fmt.Print("Downloading Hugo from GitHub releases... ")
|
||||||
|
|
||||||
// Get the data
|
// Get the data
|
||||||
resp, err := http.Get(baseurl + filename)
|
resp, err := http.Get(baseurl + filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println("An error ocurred while downloading. If this error persists, try downloading Hugo from \"https://github.com/spf13/hugo/releases/\" and put the executable in " + bin + " and rename it to 'hugo' or 'hugo.exe' if you're on Windows.")
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Writer the body to file
|
// Writer the body to file
|
||||||
_, err = io.Copy(out, resp.Body)
|
_, err = io.Copy(out, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Checking SHA256...")
|
fmt.Println("downloaded.")
|
||||||
// TODO: check sha256
|
fmt.Print("Checking SHA256...")
|
||||||
|
|
||||||
fmt.Println("Unziping...")
|
hasher := sha256.New()
|
||||||
|
f, err := os.Open(tempfile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err := io.Copy(hasher, f); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hex.EncodeToString(hasher.Sum(nil)) != sha256Hash[filename] {
|
||||||
|
fmt.Println("can't verify SHA256.")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("checked!")
|
||||||
|
fmt.Print("Unziping... ")
|
||||||
|
|
||||||
// Unzip or Ungzip the file
|
// Unzip or Ungzip the file
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "darwin", "windows":
|
case "darwin", "windows":
|
||||||
unzip(temp+filename, bin)
|
err = unzip(tempfile, bin)
|
||||||
default:
|
default:
|
||||||
ungzip(temp+filename, bin)
|
err = ungzip(tempfile, bin)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Removing temporary files...")
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
// Removes the temporary file and other files
|
fmt.Println("done.")
|
||||||
os.Remove(tempfile)
|
|
||||||
os.Remove(bin + "README.md")
|
tempfiles = append(tempfiles, bin+"README.md", bin+"LICENSE.md")
|
||||||
os.Remove(bin + "LICENSE.md")
|
clean()
|
||||||
|
|
||||||
|
ftorename := bin + strings.Replace(filename, ".tar.gz", "", 1)
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
os.Rename(bin+strings.Replace(filename, ".zip", ".exe", 1), bin+"hugo.exe")
|
ftorename = bin + strings.Replace(filename, ".zip", ".exe", 1)
|
||||||
|
|
||||||
fmt.Println("Hugo installed at " + filepath.Clean(bin) + "\\hugo.exe")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Rename(bin+strings.Replace(filename, ".tar.gz", "", 1), bin+"hugo")
|
os.Rename(ftorename, hugo)
|
||||||
fmt.Println("Hugo installed at " + filepath.Clean(bin) + "/hugo")
|
fmt.Println("Hugo installed at " + hugo)
|
||||||
return nil
|
return hugo
|
||||||
}
|
}
|
||||||
|
|
||||||
func unzip(archive, target string) error {
|
func unzip(archive, target string) error {
|
||||||
|
@ -168,3 +229,13 @@ func ungzip(source, target string) error {
|
||||||
_, err = io.Copy(writer, archive)
|
_, err = io.Copy(writer, archive)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func clean() {
|
||||||
|
fmt.Print("Removing temporary files... ")
|
||||||
|
|
||||||
|
for _, file := range tempfiles {
|
||||||
|
os.Remove(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("done.")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue