From 7ea49a4869751f93172f276737d094e55121a08d Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sun, 22 Sep 2019 06:19:25 -0400 Subject: Add a build script, go mod file and update the readme --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ build.sh | 9 +++++++++ go.mod | 5 +++++ go.sum | 3 +++ main.go | 13 ++++++++++++- 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100755 build.sh create mode 100644 go.mod create mode 100644 go.sum diff --git a/README.md b/README.md index 3f92052..dee575e 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,46 @@ It's not likely of much use on its own. The real power comes when you tie it together with your shell scripts to run a command, or send the output of something to your chat server if things go wrong. + +With an example configuration file: + +``` +backups: + url: https://discordapp.com/api/webhooks/fill/me/in + schema: + username: 'backups@{{.Vars.Hostname}}' + content: '{{.Vars.Message}} ```{{.Stdin}}```' +``` + +Placed in `$HOME/.hook.yml` + +We can then call say our backup script: + +```bash +$ bin/backup.sh | hook -n backups -V Hostname "$(hostname)" -v Message "Backup output" +``` + +Which will yield a webhook being sent out with the output from your +`bin/backup.sh` script + +A slightly more complex example might be to use it in a shell script: + +```bash +#!/bin/sh + +output="$(mktemp)" + +restic backup /var/www 2>&1 > "$output" +if [ $? -ne 0 ] ; then + hook -n backups \ + -v Hostname "$(hostname)" \ + -v Message "Backup failure! :bomb:" \ + < "$output" +fi + +rm "$output" +``` + +The above example would only send out a webhook if the backup had a failure +by using `$?` to check the status code. + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..0f5b244 --- /dev/null +++ b/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -e +set -x + +export CGO_ENABLED=0 +go build \ + -o hook \ + -ldflags '-X "main.versionString='"$(git log --pretty=format:"%D %h %an %aD" -n 1)"'"' \ + main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e92adba --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.riedstra.us/mitch/hook + +go 1.13 + +require gopkg.in/yaml.v2 v2.2.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bd555a3 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 8524518..b6bdd50 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,10 @@ import ( "text/template" ) +var ( + versionString string +) + type Hook struct { Url string Schema map[string]string @@ -38,7 +42,7 @@ func readConf(fn string, cfg map[string]*Hook) error { func help() { n := os.Args[0] - fmt.Fprintf(os.Stderr, `Usage: %s [-c CONFIG] -n HOOK_NAME [-v VarName VarVal]... + fmt.Fprintf(os.Stderr, `Usage: %s [-V -version] [-c CONFIG] -n HOOK_NAME [-v VarName VarVal]... For example: @@ -110,6 +114,11 @@ func (h *Hook) Post() (*http.Response, error) { return resp, err } +func version() { + fmt.Println(versionString) + os.Exit(0) +} + func main() { cfgFN := "" hookName := "" @@ -126,6 +135,8 @@ func main() { case "-v", "-var", "--var": vars[os.Args[i+1]] = os.Args[i+2] i += 2 + case "-V", "-version", "--version": + version() default: help() } -- cgit v1.2.3