From 629d2092a1a9151fa231e4ce1e5604337cfe2cf9 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 25 Aug 2016 20:45:48 +0100 Subject: [PATCH] stop installing hugo. ask the user to do it --- build/main.go | 94 ------------------------------------------------ hugo.go | 1 - installer.go | 98 --------------------------------------------------- setup.go | 8 ++++- 4 files changed, 7 insertions(+), 194 deletions(-) delete mode 100644 build/main.go delete mode 100644 installer.go diff --git a/build/main.go b/build/main.go deleted file mode 100644 index aeb5cc63..00000000 --- a/build/main.go +++ /dev/null @@ -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) - } -} diff --git a/hugo.go b/hugo.go index d29b8c6e..52827946 100644 --- a/hugo.go +++ b/hugo.go @@ -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/... diff --git a/installer.go b/installer.go deleted file mode 100644 index 57bdcc3d..00000000 --- a/installer.go +++ /dev/null @@ -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) -} diff --git a/setup.go b/setup.go index 4c8a2519..3ff26575 100644 --- a/setup.go +++ b/setup.go @@ -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()