pull/60/head
Henrique Dias 2016-02-29 19:30:05 +00:00
parent 5dcc44a1ca
commit b214d6318b
1 changed files with 11 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -47,13 +48,15 @@ var (
func GetPath() string { func GetPath() string {
initializeVariables() initializeVariables()
var err error
// Check if Hugo is already on $PATH // Check if Hugo is already on $PATH
if hugo, err := exec.LookPath("hugo"); err == nil { if hugo, err := exec.LookPath("hugo"); err == nil {
return hugo return hugo
} }
// Check if Hugo is on $HOME/.caddy/bin // Check if Hugo is on $HOME/.caddy/bin
if _, err := os.Stat(hugo); err == nil { if _, err = os.Stat(hugo); err == nil {
return hugo return hugo
} }
@ -62,15 +65,17 @@ func GetPath() string {
// Create the neccessary folders // Create the neccessary folders
os.MkdirAll(caddy, 0774) os.MkdirAll(caddy, 0774)
os.Mkdir(bin, 0774) os.Mkdir(bin, 0774)
os.Mkdir(temp, 0774)
if temp, err = ioutil.TempDir("", "caddy-hugo"); err != nil {
fmt.Println(err)
os.Exit(-1)
}
downloadHugo() downloadHugo()
checkSHA256() checkSHA256()
fmt.Print("Unziping... ") fmt.Print("Unziping... ")
var err error
// Unzip or Ungzip the file // Unzip or Ungzip the file
switch runtime.GOOS { switch runtime.GOOS {
case "darwin", "windows": case "darwin", "windows":
@ -104,7 +109,7 @@ func GetPath() string {
} }
fmt.Println("Hugo installed at " + hugo) fmt.Println("Hugo installed at " + hugo)
clean() defer os.RemoveAll(temp)
return hugo return hugo
} }
@ -120,7 +125,6 @@ func initializeVariables() {
caddy = filepath.Join(usr.HomeDir, ".caddy") caddy = filepath.Join(usr.HomeDir, ".caddy")
bin = filepath.Join(caddy, "bin") bin = filepath.Join(caddy, "bin")
temp = filepath.Join(caddy, "temp")
hugo = filepath.Join(bin, "hugo") hugo = filepath.Join(bin, "hugo")
switch runtime.GOOS { switch runtime.GOOS {
@ -149,7 +153,7 @@ func downloadHugo() {
out, err := os.Create(tempfile) out, err := os.Create(tempfile)
out.Chmod(0774) out.Chmod(0774)
if err != nil { if err != nil {
clean() defer os.RemoveAll(temp)
fmt.Println(err) fmt.Println(err)
os.Exit(-1) os.Exit(-1)
} }
@ -280,9 +284,3 @@ 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... ")
os.RemoveAll(temp)
fmt.Println("done.")
}