diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2019-09-21 22:14:29 -0400 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2019-09-21 22:14:29 -0400 |
| commit | e9a29c85c023e9a91027122bd361244db575d44b (patch) | |
| tree | c20b73ba3518931b863133bb46ce3f0758f7fdd0 | |
| parent | 719ce085779c2ed4b577400d17d0890447e1eec8 (diff) | |
| download | hook-e9a29c85c023e9a91027122bd361244db575d44b.tar.gz hook-e9a29c85c023e9a91027122bd361244db575d44b.tar.xz | |
A little bit more refactoring, added a basic readme
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | example.yml | 1 | ||||
| -rw-r--r-- | main.go | 84 |
3 files changed, 59 insertions, 31 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..3f92052 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# `hook` A command line webhook utility. + +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. diff --git a/example.yml b/example.yml index 052a4d0..eddd34e 100644 --- a/example.yml +++ b/example.yml @@ -4,4 +4,3 @@ backups: schema: username: 'backups@{{.Vars.Hostname}}' content: '{{.Vars.Message}} ```{{.Stdin}}```' - @@ -38,19 +38,48 @@ func readConf(fn string, cfg map[string]*Hook) error { func help() { n := os.Args[0] - fmt.Fprintf(os.Stderr, `Usage: %s [-c cfg] -n name [-v varName varVal]... + fmt.Fprintf(os.Stderr, `Usage: %s [-c CONFIG] -n HOOK_NAME [-v VarName VarVal]... For example: - echo "Contents!" | %s -c hook.yml -n discord -v hostname "$(hostname)" \ - -v message "Backup failure" + echo "Contents!" \ + | %s -c hook.yml -n backups \ + -v Hostname "$(hostname)" \ + -v Message "Backup failure! :bomb:" -See the full documentation on generating the required configuration file. +Where 'hook.yml' contains: + + backups: + url: https://discordapp.com/api/webhooks/fill/me/in + schema: + username: 'backups@{{.Vars.Hostname}}' + content: '{{.Vars.Message}} `+"```{{.Stdin}}```'"+` + +Note how '-v Var Assignment' populates '.Vars'. '.Stdin' is special, and +populated via the command's standard input which is always read. + + +If no configuration file is specified ~/.hook.yml will be used instead. + `, n, n) os.Exit(1) } -func (h *Hook) ProcessSchema(data interface{}) error { +func (h *Hook) ProcessSchema(vars map[string]string) error { + stdin, _ := ioutil.ReadAll(os.Stdin) + + // Unpack the args into an anonymous struct literal to be used + // inside of each template + data := struct { + Vars map[string]string + Stdin string + }{ + Vars: vars, + Stdin: string(stdin), + } + + // Loops over every key/value pair in the `schema:` field, treating + // the text as a template and supplying the command line vars for k, v := range h.Schema { buf := &bytes.Buffer{} templ, err := template.New("schema").Parse(v) @@ -71,13 +100,21 @@ func (h *Hook) ProcessSchema(data interface{}) error { return nil } +func (h *Hook) Post() (*http.Response, error) { + body, err := json.Marshal(h.Schema) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + buf := bytes.NewBuffer(body) + resp, err := http.Post(h.Url, "Application/json", buf) + return resp, err +} + func main() { - cfgFN := "hook.yml" + cfgFN := "" hookName := "" vars := map[string]string{} - // A bit of an unusual loop to handle the unusual way that the - // arguments work for i := 1; i < len(os.Args); i += 1 { switch os.Args[i] { case "-c", "-config", "--config": @@ -96,6 +133,10 @@ func main() { } cfg := make(map[string]*Hook) err := readConf(cfgFN, cfg) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } if hookName == "" { help() @@ -104,33 +145,16 @@ func main() { help() } - // fmt.Println(hookName) - // fmt.Println(cfg) - // fmt.Println(err) - // fmt.Println(vars) - hook := cfg[hookName] - // fmt.Println(hook) - stdin, _ := ioutil.ReadAll(os.Stdin) - - err = hook.ProcessSchema(struct { - Vars map[string]string - Stdin string - }{ - Vars: vars, - Stdin: string(stdin), - }) - - fmt.Println(hook) - fmt.Println(err) + err = hook.ProcessSchema(vars) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } - body, err := json.Marshal(hook.Schema) + _, err = hook.Post() if err != nil { fmt.Fprintln(os.Stderr, err) } - buf := bytes.NewBuffer(body) - resp, err := http.Post(hook.Url, "Application/json", buf) - fmt.Println(resp) } |
