Skip to content

Commit

Permalink
output something to http please
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Jul 14, 2020
1 parent 6bdea0a commit 398417f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ build/webhooker-%: $(SOURCE)
@mkdir -p $(@D)
CGO_ENABLED=0 GOOS=$(firstword $($*) $*) GOARCH=amd64 go build -o $@

release: $(ALL)
ifndef desc
@echo "Run it as 'make release desc=tralala'"
release:
@echo "Push a tag and run this as 'make release desc=tralala'"
else
release: $(ALL)
github-release release -u piranha -r webhooker -t "$(TAG)" -n "$(TAG)" --description "$(desc)"
@for x in $(ALL); do \
echo "Uploading $$x" && \
Expand Down
37 changes: 27 additions & 10 deletions webhooker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"encoding/json"
"fmt"
flags "github.com/jessevdk/go-flags"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"io/ioutil"
)

/// Globals

var Version = "0.3"
var Version = "0.4"

var opts struct {
Interface string `short:"i" long:"interface" default:"127.0.0.1" description:"ip to listen on"`
Expand All @@ -43,7 +44,7 @@ func GetPath(p Payload) string {

type Rule interface {
Match(path string) bool
Run(data Payload) error
Run(data Payload) (string, error)
String() string
}

Expand Down Expand Up @@ -117,7 +118,7 @@ func (r *PatRule) String() string {
return fmt.Sprintf("%s='%s'", r.Pattern, r.Command)
}

func (r *PatRule) Run(data Payload) error {
func (r *PatRule) Run(data Payload) (string, error) {
cmd := exec.Command("sh", "-c", r.Command)

cmd.Env = append(data.EnvData(),
Expand All @@ -128,7 +129,7 @@ func (r *PatRule) Run(data Payload) error {

out, err := cmd.CombinedOutput()
log.Printf("'%s' for %s output: %s", r.Command, data.RepoName(), out)
return err
return fmt.Sprintf("'%s' for %s output:\n%s", r.Command, data.RepoName(), out), err
}

/// actual work
Expand All @@ -152,7 +153,7 @@ func (c *Config) ParsePatterns(input []string) error {
return nil
}

func (c Config) ExecutePayload(data Payload) error {
func (c Config) ExecutePayload(data Payload) (string, error) {
path := GetPath(data)

for _, rule := range c {
Expand All @@ -161,8 +162,9 @@ func (c Config) ExecutePayload(data Payload) error {
}
}

log.Printf("No handlers for %s", path)
return nil
msg := fmt.Sprintf("No handlers for %s", path)
log.Print(msg)
return msg, nil
}

func (c Config) HandleRequest(w http.ResponseWriter, r *http.Request) {
Expand All @@ -178,11 +180,26 @@ func (c Config) HandleRequest(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, err.Error(), 500)
return
}

c.ExecutePayload(data)
out, err := c.ExecutePayload(data)
if err != nil {
log.Println(err)
if out == "" {
http.Error(w, err.Error(), 500)
} else {
http.Error(w, out, 500)
}
return
}

_, err = io.WriteString(w, out)
if err != nil {
log.Println(err)
return
}
}

/// Main
Expand Down

0 comments on commit 398417f

Please sign in to comment.