From e9a29c85c023e9a91027122bd361244db575d44b Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 21 Sep 2019 22:14:29 -0400 Subject: A little bit more refactoring, added a basic readme --- main.go | 84 ++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 30 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index da9a3c3..8524518 100644 --- a/main.go +++ b/main.go @@ -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) } -- cgit v1.2.3