2020-03-01 19:21:20 +00:00
package main
import (
"fmt"
2020-03-01 19:53:03 +00:00
"errors"
2020-03-01 19:21:20 +00:00
"io"
"os/exec"
"os"
"log"
"net/http"
"strings"
)
func myexec ( w io . Writer , main string , params ... string ) error {
cmd := exec . Command ( main , params ... )
cmd . Stdout = w
cmd . Stderr = w
err := cmd . Run ( )
if err != nil {
fmt . Fprintf ( w , "Failed to run: %s %s\n" , main , strings . Join ( params , " " ) )
}
return err
}
2020-03-01 19:53:03 +00:00
func update ( w io . Writer ) error {
2020-03-01 19:21:20 +00:00
fmt . Fprintf ( w , "Start update...\n" )
_ , err := os . Stat ( "./.git" )
if err != nil {
fmt . Fprintf ( w , ".git folder does not exist, creating it...\n" )
err := myexec ( w , "git" , "init" )
if err != nil {
2020-03-01 19:53:03 +00:00
return err
2020-03-01 19:21:20 +00:00
}
}
err = myexec ( w , "git" , "remote" , "get-url" , "origin" )
if err != nil {
repo , exists := os . LookupEnv ( "WEBPULL_REPO" )
if ! exists {
fmt . Fprintf ( w , "You must define WEBPULL_REPO env variable...\n" )
2020-03-01 19:53:03 +00:00
return errors . New ( "Missing environment variable WEBPULL_REPO" )
2020-03-01 19:21:20 +00:00
}
fmt . Fprintf ( w , "git remote is not yet set...\n" )
err := myexec ( w , "git" , "remote" , "add" , "origin" , repo )
if err != nil {
2020-03-01 19:53:03 +00:00
return err
2020-03-01 19:21:20 +00:00
}
}
err = myexec ( w , "git" , "pull" , "origin" , "master" )
if err != nil {
fmt . Fprintf ( w , "Failed to pull...\n" )
2020-03-01 19:53:03 +00:00
return err
2020-03-01 19:21:20 +00:00
}
2020-03-01 19:53:03 +00:00
_ , err = os . Stat ( "./.webpull" )
2020-03-01 19:21:20 +00:00
if err != nil {
2020-03-01 19:53:03 +00:00
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 err
2020-03-01 19:21:20 +00:00
}
2020-03-01 19:53:03 +00:00
err = myexec ( w , "./.webpull" )
2020-03-01 19:21:20 +00:00
if err != nil {
fmt . Fprintf ( w , "An error occured during script execution\n" )
2020-03-01 19:53:03 +00:00
return err
2020-03-01 19:21:20 +00:00
}
fmt . Fprintf ( w , "Success.\n" )
2020-03-01 19:53:03 +00:00
return nil
2020-03-01 19:21:20 +00:00
}
func main ( ) {
2020-03-01 19:53:03 +00:00
token , exists := os . LookupEnv ( "WEBPULL_TOKEN" )
if ! exists {
log . Fatal ( "Environment variable 'WEBPULL_TOKEN' must be defined" )
}
if update ( os . Stdout ) != nil {
log . Fatal ( "Initial 'update' failed" )
}
2020-03-01 19:21:20 +00:00
fs := http . FileServer ( http . Dir ( "./static" ) )
http . HandleFunc ( "/update" , func ( w http . ResponseWriter , r * http . Request ) {
2020-03-01 19:53:03 +00:00
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
}
2020-03-01 19:21:20 +00:00
update ( w )
} )
http . Handle ( "/" , fs )
log . Fatal ( http . ListenAndServe ( ":8080" , nil ) )
}