modularized and documented

This commit is contained in:
Zac 2018-08-30 16:28:01 -07:00 committed by Thomas Jackson
parent e437d8e9a3
commit 6b8aac0b83
2 changed files with 79 additions and 59 deletions

View File

@ -33,7 +33,6 @@ import (
"strconv"
"strings"
"time"
"net/http"
"encoding/json"
"github.com/thockin/glogr"
@ -88,24 +87,6 @@ var flCookieFile = flag.Bool("cookie-file", envBool("GIT_COOKIE_FILE", false),
var flGitCmd = flag.String("git", envString("GIT_SYNC_GIT", "git"),
"the git command to run (subject to PATH search)")
// WebHook structure
type Webhook struct {
// URL for the http/s request
URL string `json:"url"`
// Method for the http/s request
Method string `json:"method"`
// Code to look for when determining if the request was successful.
// If this is not specified, request is sent and forgotten about.
Success int `json:"success"`
}
// Create an http client that has our timeout by default
var netClient = &http.Client{
Timeout: time.Duration(time.Second * time.Duration(*flWebhookTimeout) ),
}
// Webhook collection
var WebhookArray = []Webhook{}
var log = newLoggerOrDie()
@ -221,6 +202,9 @@ func main() {
// From here on, output goes through logging.
log.V(0).Infof("starting up: %q", os.Args)
// Startup webhooks goroutine
go ServeWebhooks()
initialSync := true
failCount := 0
for {
@ -259,24 +243,6 @@ func main() {
}
}
// WebhookCall Do webhook call
func WebHookCall(url string, method string, statusCode int) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
resp, err := netClient.Do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != statusCode {
return fmt.Errorf("received response code %q expected %q", resp.StatusCode, statusCode)
}
return nil
}
func waitTime(seconds float64) time.Duration {
return time.Duration(int(seconds*1000)) * time.Millisecond
}
@ -325,19 +291,6 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) error {
}
log.V(1).Infof("renamed symlink %s to %s", "tmp-link", link)
// // If there is a symlink update callback, call it
// if len(*flSymlinkUpdatePostUrl) > 0 {
// log.V(1).Infof("sending post request to %s", *flSymlinkUpdatePostUrl)
// // Send the post request
// req, err := http.NewRequest("POST", *flSymlinkUpdatePostUrl, nil)
// if err != nil {
// fmt.Errorf("error sending post request (after symlink update): %v", err)
// }
// c := &http.Client{}
// resp, err := c.Do(req)
// resp.Body.Close()
// }
// Clean up previous worktree
if len(currentDir) > 0 {
if err = os.RemoveAll(currentDir); err != nil {
@ -354,15 +307,9 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) error {
log.V(1).Infof("pruned old worktrees")
}
// Calling webhook only after new symlink created - one after another
for _, v := range WebhookArray {
log.V(0).Infof("calling webhook %v\n", v.URL)
if err := WebHookCall(v.URL, v.Method, v.Success); err != nil {
log.Errorf("error calling webhook %v: %v", v.URL, err)
} else {
log.V(0).Infof("calling webhook %v was: OK\n", v.URL)
}
}
// Trigger webhooks to be called
log.V(0).Infof("sending webhook trigger ...\n")
WebhookCallTriggerChannel <- struct{}{}
return nil
}

73
cmd/git-sync/webhooks.go Normal file
View File

@ -0,0 +1,73 @@
package main
import (
"net/http"
"time"
"fmt"
)
// Create an http client that has our timeout by default
var netClient = &http.Client{
Timeout: time.Duration(time.Second * time.Duration(*flWebhookTimeout) ),
}
// Trigger channel for webhook requests. If anything is received into this channel
// it triggers the webhook goroutine to send new requests.
var WebhookCallTriggerChannel = make(chan struct{})
// Webhook collection
var WebhookArray = []Webhook{}
// WebHook structure
type Webhook struct {
// URL for the http/s request
URL string `json:"url"`
// Method for the http/s request
Method string `json:"method"`
// Code to look for when determining if the request was successful.
// If this is not specified, request is sent and forgotten about.
Success *int `json:"success"`
}
// WebhookCall Do webhook call
func WebHookCall(url string, method string, statusCode *int) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
//
resp, err := netClient.Do(req)
if err != nil {
return err
}
resp.Body.Close()
// If the webhook has a success statusCode, check against it
if statusCode != nil && resp.StatusCode != *statusCode {
return fmt.Errorf("received response code %q expected %q", resp.StatusCode, statusCode)
}
return nil
}
// Wait for trigger events from the channel, and send webhooks when triggered
func ServeWebhooks() {
for {
// Wait for trigger
<- WebhookCallTriggerChannel
log.V(0).Infof("webhook trigger received ...\n")
// Calling webhook - one after another
for _, v := range WebhookArray {
log.V(0).Infof("calling webhook %v\n", v.URL)
if err := WebHookCall(v.URL, v.Method, v.Success); err != nil {
log.Errorf("error calling webhook %v: %v", v.URL, err)
} else {
log.V(0).Infof("calling webhook %v was: OK\n", v.URL)
}
}
}
}