aboutsummaryrefslogtreecommitdiff
path: root/app/build/webpull/main.go
diff options
context:
space:
mode:
authorQuentin <quentin@dufour.io>2021-01-18 08:18:21 +0100
committerQuentin <quentin@dufour.io>2021-01-18 08:18:21 +0100
commitad6017eea058f7cb6fdf078783f992a4f45a3e15 (patch)
tree6620bcc9e1ea61a5689b763b9ad8280275e35e76 /app/build/webpull/main.go
parent79b7273ff2a487d6721d393682c8ad3927467a75 (diff)
parentc642370def01f09d966b3b9c643cfe416ea115cf (diff)
downloadinfrastructure-ad6017eea058f7cb6fdf078783f992a4f45a3e15.tar.gz
infrastructure-ad6017eea058f7cb6fdf078783f992a4f45a3e15.zip
Merge pull request 'Reorganize app/ and add script for secret management' (#29) from test_reorganize into master
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/infrastructure/pulls/29
Diffstat (limited to 'app/build/webpull/main.go')
-rw-r--r--app/build/webpull/main.go100
1 files changed, 0 insertions, 100 deletions
diff --git a/app/build/webpull/main.go b/app/build/webpull/main.go
deleted file mode 100644
index 46c90b9..0000000
--- a/app/build/webpull/main.go
+++ /dev/null
@@ -1,100 +0,0 @@
-package main
-
-import (
- "fmt"
- "errors"
- "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
-}
-
-func update(w io.Writer) error {
- 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 {
- return err
- }
- }
-
- 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")
- return errors.New("Missing environment variable WEBPULL_REPO")
- }
- fmt.Fprintf(w, "git remote is not yet set...\n")
- err := myexec(w, "git", "remote", "add", "origin", repo)
- if err != nil {
- return err
- }
- }
-
- err = myexec(w, "git", "pull", "origin", "master")
- if err != nil {
- fmt.Fprintf(w, "Failed to pull...\n")
- return err
- }
-
- _, err = os.Stat("./.webpull")
- if err != nil {
- 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
- }
-
- err = myexec(w, "./.webpull")
- if err != nil {
- fmt.Fprintf(w, "An error occured during script execution\n")
- return err
- }
-
- fmt.Fprintf(w, "Success.\n")
- return nil
-}
-
-func main() {
- 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")
- }
-
- fs := http.FileServer(http.Dir("./static"))
- 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)
- })
- http.Handle("/", fs)
-
- log.Fatal(http.ListenAndServe(":8080", nil))
-}