From 00054724ff05bdccbc985baf2ca4fbeb2f71755a Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 10 Aug 2016 09:54:40 +0100 Subject: [PATCH] #83 on the way --- .gitignore | 2 +- build/main.go | 26 +++++ hugo.go | 3 +- installer.go | 98 ++++++++++++++++++ installer/hashes.go | 25 ----- installer/installer.go | 227 ----------------------------------------- setup.go | 3 +- 7 files changed, 128 insertions(+), 256 deletions(-) create mode 100644 installer.go delete mode 100644 installer/hashes.go delete mode 100644 installer/installer.go diff --git a/.gitignore b/.gitignore index dc631cb8..07c5fd63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ hugo -hugo.gz +binary.go diff --git a/build/main.go b/build/main.go index 1d4b91e1..c293e408 100644 --- a/build/main.go +++ b/build/main.go @@ -1,6 +1,7 @@ package main import ( + "io/ioutil" "log" "os" "os/exec" @@ -34,6 +35,8 @@ func main() { // 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 { @@ -62,3 +65,26 @@ func exists(path string) (bool, error) { 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 079743d8..d29b8c6e 100644 --- a/hugo.go +++ b/hugo.go @@ -1,6 +1,7 @@ +//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 -pkg hugo -prefix "assets" -o binary.go assets/... +//go:generate go-bindata -nomemcopy -pkg hugo -prefix "assets" -o binary.go assets/... // Package hugo makes the bridge between the static website generator Hugo // and the webserver Caddy, also providing an administrative user interface. diff --git a/installer.go b/installer.go new file mode 100644 index 00000000..1f004105 --- /dev/null +++ b/installer.go @@ -0,0 +1,98 @@ +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 = "UNDEFINED" + +// 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/installer/hashes.go b/installer/hashes.go deleted file mode 100644 index f28106ab..00000000 --- a/installer/hashes.go +++ /dev/null @@ -1,25 +0,0 @@ -package installer - -var ( - sha256Hash = map[string]string{ - "hugo_0.16_darwin-arm32.tgz": "683d5d4b4e0ac03a183ca5eb9019981ba696569445c7d6d1efc7e6706bd273a5", - "hugo_0.16_dragonfly-64bit.tgz": "63a3ee9a36d4d2166c77b96bb8bf39b2239affe118e44a83b3d0a44374a8921d", - "hugo_0.16_freebsd-32bit.tgz": "ea3f84900feeeb9d89573dea49a4349753116e70de561eeec4858f7ffc74f8f9", - "hugo_0.16_freebsd-64bit.tgz": "8d9320bb660090a77a4f922ca30b1582593bc6d87c3fd8bd6f5ecbe49cf1d2f2", - "hugo_0.16_freebsd-arm32.tgz": "b4c21296e01ea68709ac50d7eb1d314b738f1c8408ff2be223d06ae76604dbea", - "hugo_0.16_linux-32bit.tgz": "aed82d156f01a4562c39bd1af41aa81699009140da965e0369c370ba874725c9", - "hugo_0.16_linux-64bit.tgz": "13e299dc45bea4fad5bdf8c2640305a5926e2acd02c3aa03b7864403e513920e", - "hugo_0.16_linux-arm32.tgz": "bc836def127d93e2457da9994f9c09b0100523e46d61074cd724ef092b11714f", - "hugo_0.16_linux-arm64.tgz": "d04486918f43f89f1e0359eebedd8a05d96f7ca40f93e7fd8d7c3f2dac115a8d", - "hugo_0.16_netbsd-32bit.tgz": "cb578eebec5b6364b0afd5bb208d94317acab0a3e033b81f04b1511af0669b63", - "hugo_0.16_netbsd-64bit.tgz": "d3c766d9800d7fdd268ffd2f28b7af451f13a4de63901bfdae2ee5c96528b8cc", - "hugo_0.16_netbsd-arm32.tgz": "51162b2637e71b786582af715a44b778f62bdc62a9a354ccc4a7c8384afe194c", - "hugo_0.16_openbsd-32bit.tgz": "2d1e112a7346850897ea77da868c0d987ef90efb7f49c917659437a5a67f89f8", - "hugo_0.16_openbsd-64bit.tgz": "7b33ff2565df5a6253c3e4308813d947e34af04c633fb4e01cac83751066e16e", - "hugo_0.16_osx-32bit.tgz": "6155dda548bbd1e26c26a4a00472e4c0e55fad9fcd46991ce90987385bd5fd0a", - "hugo_0.16_osx-64bit.tgz": "b0cba8f6996946ef34a664184d6461567d79fc2a3e793145d34379902eda0ad9", - "hugo_0.16_solaris-64bit.tgz": "af9557403af5e16eb7faf965c04540417a70699efbbbc4e0a7ae4c4703ad1ae8", - "hugo_0.16_windows-32bit.zip": "1c72d06843fe02cb62348660d87a523c885ed684a683271fc8762e7234c4210b", - "hugo_0.16_windows-64bit.zip": "a3fda0bd30592e4eb3bdde85c8a8ce23a7433073536466d16fd0e97bf7794067", - } -) diff --git a/installer/installer.go b/installer/installer.go deleted file mode 100644 index 006f1a5a..00000000 --- a/installer/installer.go +++ /dev/null @@ -1,227 +0,0 @@ -package installer - -import ( - "crypto/sha256" - "encoding/hex" - "fmt" - "io" - "io/ioutil" - "log" - "net/http" - "os" - "os/exec" - "path/filepath" - "regexp" - "runtime" - - "github.com/hacdias/caddy-hugo/utils/files" - "github.com/mitchellh/go-homedir" - "github.com/pivotal-golang/archiver/extractor" -) - -const ( - version = "0.16" - baseurl = "https://github.com/spf13/hugo/releases/download/v" + version + "/" -) - -var caddy, bin, temp, hugo, tempfile, zipname, exename string - -// GetPath retrives the Hugo path for the user or install it if it's not found -func GetPath() string { - initializeVariables() - - var err error - var hugoPath string - found := false - - // Check if Hugo is already on $PATH - if hugoPath, err = exec.LookPath("hugo"); err == nil { - if checkVersion() { - return hugoPath - } - - found = true - } - - // Check if Hugo is on $HOME/.caddy/bin - if _, err = os.Stat(hugo); err == nil { - if checkVersion() { - 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) - - if temp, err = ioutil.TempDir("", "caddy-hugo"); err != nil { - fmt.Println(err) - os.Exit(-1) - } - - downloadHugo() - checkSHA256() - - fmt.Print("Unzipping... ") - - // Unzip or Ungzip the file - switch runtime.GOOS { - case "windows": - zp := extractor.NewZip() - err = zp.Extract(tempfile, temp) - default: - gz := extractor.NewTgz() - err = gz.Extract(tempfile, temp) - } - - if err != nil { - fmt.Println(err) - os.Exit(-1) - } - - fmt.Println("done.") - - var exetorename string - - err = filepath.Walk(temp, func(path string, f os.FileInfo, err error) error { - if f.Name() == exename { - exetorename = path - } - - return nil - }) - - // Copy the file - fmt.Print("Moving Hugo executable... ") - - err = files.CopyFile(exetorename, hugo) - if err != nil { - fmt.Println(err) - os.Exit(-1) - } - - err = os.Chmod(hugo, 0755) - if err != nil { - fmt.Println(err) - os.Exit(-1) - } - - fmt.Println("done.") - fmt.Println("Hugo installed at " + hugo) - defer os.RemoveAll(temp) - return hugo -} - -func initializeVariables() { - var arch string - switch runtime.GOARCH { - case "amd64": - arch = "64bit" - case "386": - arch = "32bit" - case "arm": - arch = "arm32" - default: - arch = runtime.GOARCH - } - - var ops = runtime.GOOS - if runtime.GOOS == "darwin" && runtime.GOARCH != "arm" { - ops = "osx" - } - - exename = "hugo" - zipname = "hugo_" + version + "_" + ops + "-" + arch - - homedir, err := homedir.Dir() - if err != nil { - fmt.Println(err) - os.Exit(-1) - } - - caddy = filepath.Join(homedir, ".caddy") - bin = filepath.Join(caddy, "bin") - hugo = filepath.Join(bin, "hugo") - - switch runtime.GOOS { - case "windows": - zipname += ".zip" - exename += ".exe" - hugo += ".exe" - default: - zipname += ".tgz" - } -} - -func checkVersion() bool { - out, _ := exec.Command(hugo, "version").Output() - - r := regexp.MustCompile(`v\d\.\d{2}`) - v := r.FindStringSubmatch(string(out))[0] - v = v[1:len(v)] - - return (v == version) -} - -func downloadHugo() { - tempfile = filepath.Join(temp, zipname) - - fmt.Print("Downloading Hugo from GitHub releases... ") - - // Create the file - out, err := os.Create(tempfile) - out.Chmod(0774) - if err != nil { - defer os.RemoveAll(temp) - fmt.Println(err) - os.Exit(-1) - } - defer out.Close() - - // Get the data - resp, err := http.Get(baseurl + zipname) - if err != nil { - 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() - - // Writer the body to file - _, err = io.Copy(out, resp.Body) - if err != nil { - fmt.Println(err) - os.Exit(-1) - } - - fmt.Println("downloaded.") -} - -func checkSHA256() { - fmt.Print("Checking SHA256...") - - 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[zipname] { - fmt.Println("can't verify SHA256.") - os.Exit(-1) - } - - fmt.Println("checked!") -} diff --git a/setup.go b/setup.go index 752d4ef2..1a2e73d6 100644 --- a/setup.go +++ b/setup.go @@ -14,7 +14,6 @@ import ( "github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/directory" "github.com/hacdias/caddy-filemanager/frontmatter" - "github.com/hacdias/caddy-hugo/installer" "github.com/hacdias/caddy-hugo/utils/commands" "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" @@ -142,7 +141,7 @@ func parse(c *caddy.Controller, root string) (*Config, error) { Root: "./", } - conf.Hugo = installer.GetPath() + conf.Hugo = getPath() for c.Next() { args := c.RemainingArgs()