forked from Deuxfleurs/infrastructure
[webpull] passing basic tests
This commit is contained in:
parent
e652a83894
commit
c8c2cdf8d4
1 changed files with 32 additions and 12 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os"
|
"os"
|
||||||
|
@ -21,14 +22,14 @@ func myexec(w io.Writer, main string, params ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(w io.Writer) {
|
func update(w io.Writer) error {
|
||||||
fmt.Fprintf(w, "Start update...\n")
|
fmt.Fprintf(w, "Start update...\n")
|
||||||
_, err := os.Stat("./.git")
|
_, err := os.Stat("./.git")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, ".git folder does not exist, creating it...\n")
|
fmt.Fprintf(w, ".git folder does not exist, creating it...\n")
|
||||||
err := myexec(w, "git", "init")
|
err := myexec(w, "git", "init")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,41 +38,60 @@ func update(w io.Writer) {
|
||||||
repo, exists := os.LookupEnv("WEBPULL_REPO")
|
repo, exists := os.LookupEnv("WEBPULL_REPO")
|
||||||
if !exists {
|
if !exists {
|
||||||
fmt.Fprintf(w, "You must define WEBPULL_REPO env variable...\n")
|
fmt.Fprintf(w, "You must define WEBPULL_REPO env variable...\n")
|
||||||
return
|
return errors.New("Missing environment variable WEBPULL_REPO")
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "git remote is not yet set...\n")
|
fmt.Fprintf(w, "git remote is not yet set...\n")
|
||||||
err := myexec(w, "git", "remote", "add", "origin", repo)
|
err := myexec(w, "git", "remote", "add", "origin", repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = myexec(w, "git", "pull", "origin", "master")
|
err = myexec(w, "git", "pull", "origin", "master")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "Failed to pull...\n")
|
fmt.Fprintf(w, "Failed to pull...\n")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat("./.webpool")
|
_, err = os.Stat("./.webpull")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "You must create an executable file named '.webpool' at the root of your repository.\nIf you have nothing to run, just create an empty bash script...\n")
|
fmt.Fprintf(w, "You must create an executable file named '.webpull' at the root of your repository.\nIf you have nothing to run, just create an empty bash script...\n")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = myexec(w, "./.webpool")
|
err = myexec(w, "./.webpull")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "An error occured during script execution\n")
|
fmt.Fprintf(w, "An error occured during script execution\n")
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "Success.\n")
|
fmt.Fprintf(w, "Success.\n")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
token, exists := os.LookupEnv("WEBPULL_TOKEN")
|
||||||
update(os.Stdout)
|
if !exists {
|
||||||
|
log.Fatal("Environment variable 'WEBPULL_TOKEN' must be defined")
|
||||||
|
}
|
||||||
|
|
||||||
|
if update(os.Stdout) != nil {
|
||||||
|
log.Fatal("Initial 'update' failed")
|
||||||
|
}
|
||||||
|
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
keys, ok := r.URL.Query()["token"]
|
||||||
|
if !ok || len(keys[0]) < 1 {
|
||||||
|
http.Error(w, "Missing 'token' query parameter", 401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if keys[0] != token {
|
||||||
|
http.Error(w, "Wrong token", 401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
update(w)
|
update(w)
|
||||||
})
|
})
|
||||||
http.Handle("/", fs)
|
http.Handle("/", fs)
|
||||||
|
|
Loading…
Reference in a new issue