stop installing hugo. ask the user to do it

This commit is contained in:
Henrique Dias 2016-08-25 20:45:48 +01:00
parent f878cc3040
commit 629d2092a1
4 changed files with 7 additions and 194 deletions

View File

@ -1,94 +0,0 @@
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
if len(os.Getenv("TRAVIS")) > 0 || len(os.Getenv("CI")) > 0 {
return
}
goPath := os.Getenv("GOPATH")
hugoPath := filepath.Join(goPath, "src/github.com/spf13/hugo")
if found, err := exists(hugoPath); !found || err != nil {
log.Fatalf("Aborting. Can't find Hugo source on %s.", hugoPath)
}
// NOTE: I assume that 'go get -u' was run before of this and that
// every package and dependency is up to date.
// Get new tags from remote
run("git", []string{"fetch", "--tags"}, hugoPath)
// Get the revision for the latest tag
commit := run("git", []string{"rev-list", "--tags", "--max-count=1"}, hugoPath)
// Get the latest tag
tag := run("git", []string{"describe", "--tags", commit}, hugoPath)
// Checkout the latest tag
run("git", []string{"checkout", tag}, hugoPath)
// Build hugo binary
pluginPath := filepath.Join(goPath, "src/github.com/hacdias/caddy-hugo")
run("go", []string{"build", "-o", "assets/hugo", "github.com/spf13/hugo"}, pluginPath)
updateVersion(pluginPath, tag)
}
func run(command string, args []string, path string) string {
cmd := exec.Command(command, args...)
cmd.Dir = path
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(out))
}
// exists returns whether the given file or directory exists or not
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func updateVersion(path string, version string) {
path = filepath.Join(path, "installer.go")
input, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, "const version") {
lines[i] = "const version = \"" + version + "\""
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(path, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}

View File

@ -1,4 +1,3 @@
//go:generate go run build/main.go
//go:generate go get github.com/jteeuwen/go-bindata
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
//go:generate go-bindata -nomemcopy -pkg hugo -prefix "assets" -o binary.go assets/...

View File

@ -1,98 +0,0 @@
package hugo
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"github.com/mitchellh/go-homedir"
)
// This is automatically set on `go generate`
const version = "v0.16"
// GetPath retrives the Hugo path for the user or install it if it's not found
func getPath() string {
homedir, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
caddy := filepath.Join(homedir, ".caddy")
bin := filepath.Join(caddy, "bin")
hugo := ""
found := false
// Check if Hugo is already on $PATH
if hugo, err = exec.LookPath("hugo"); err == nil {
if checkVersion(hugo) {
return hugo
}
found = true
}
if !found {
hugo = filepath.Join(bin, "hugo")
if runtime.GOOS == "windows" {
hugo += ".exe"
}
// Check if Hugo is on $HOME/.caddy/bin
if _, err = os.Stat(hugo); err == nil {
if checkVersion(hugo) {
return hugo
}
found = true
}
}
if found {
fmt.Println("We will update your hugo to the newest version.")
} else {
fmt.Println("Unable to find Hugo on your computer.")
}
// Create the neccessary folders
os.MkdirAll(caddy, 0774)
os.Mkdir(bin, 0774)
binary, err := Asset("hugo")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
err = ioutil.WriteFile(hugo, binary, 0644)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
binary = nil
// Force memory RAM garbage collector
debug.FreeOSMemory()
fmt.Println("Hugo installed at " + hugo)
return hugo
}
func checkVersion(hugo string) bool {
out, _ := exec.Command(hugo, "version").Output()
r := regexp.MustCompile(`v\d\.\d{2}`)
v := r.FindStringSubmatch(string(out))[0]
return (v == version)
}

View File

@ -143,7 +143,13 @@ func parse(c *caddy.Controller, root string) (*Config, error) {
Root: "./",
}
conf.Hugo = getPath()
if hugo, err = exec.LookPath("hugo"); err == nil {
conf.Hugo = hugo;
} else {
fmt.Println("It seems that you don't have 'hugo' on your PATH.\nAborting...")
os.Exit(0)
}
for c.Next() {
args := c.RemainingArgs()