From 97b59ccec6ea6f46dcee63725e65df16c748fd4c Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Sat, 14 Apr 2018 23:02:06 +0200 Subject: [PATCH] Use new template lib --- Gopkg.lock | 18 +++++-- Gopkg.toml | 4 +- defaults.go | 16 ++++++ main.go | 2 + plugin.go | 27 ++++++----- template.go | 137 ---------------------------------------------------- 6 files changed, 51 insertions(+), 153 deletions(-) create mode 100644 defaults.go delete mode 100644 template.go diff --git a/Gopkg.lock b/Gopkg.lock index ed9fea5..22f50ae 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -9,8 +9,20 @@ "lexer", "parser" ] - revision = "a2232af10b53ef1ae5a767f5178db3a6c1dab655" - version = "v2.0.1" + revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11" + version = "v2.0.2" + +[[projects]] + branch = "master" + name = "github.com/drone/drone-template-lib" + packages = ["template"] + revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918" + +[[projects]] + name = "github.com/pkg/errors" + packages = ["."] + revision = "645ef00459ed84a119197bfb8d8205042c6df63d" + version = "v0.8.0" [[projects]] name = "github.com/urfave/cli" @@ -21,6 +33,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "f0f9f5cbe5ccddecfff2301c3783e001bde86bec1082c3026fda1ec005c18af5" + inputs-digest = "2f1e60cee9e7125c15754093974fd20cd1b6e9ac4d8b6a33a0364477f96461f7" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index b11bf48..299461b 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -1,6 +1,6 @@ [[constraint]] - name = "github.com/aymerick/raymond" - version = "2.0.1" + branch = "master" + name = "github.com/drone/drone-template-lib" [[constraint]] name = "github.com/urfave/cli" diff --git a/defaults.go b/defaults.go new file mode 100644 index 0000000..6e99e0e --- /dev/null +++ b/defaults.go @@ -0,0 +1,16 @@ +package main + +const respFormat = `Webhook %d + URL: %s + RESPONSE STATUS: %s + RESPONSE BODY: %s +` + +const debugFormat = `Webhook %d + URL: %s + METHOD: %s + HEADERS: %s + REQUEST BODY: %s + RESPONSE STATUS: %s + RESPONSE BODY: %s +` diff --git a/main.go b/main.go index a11a7be..41603e1 100644 --- a/main.go +++ b/main.go @@ -151,6 +151,7 @@ func main() { Usage: "source env file", }, } + if err := app.Run(os.Args); err != nil { log.Fatal(err) } @@ -191,5 +192,6 @@ func run(c *cli.Context) error { SkipVerify: c.Bool("skip-verify"), }, } + return plugin.Exec() } diff --git a/plugin.go b/plugin.go index 01dd395..9730ff1 100644 --- a/plugin.go +++ b/plugin.go @@ -10,11 +10,8 @@ import ( "net/url" "os" "strings" -) -const ( - respFormat = "Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n" - debugRespFormat = "Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n" + "github.com/drone/drone-template-lib/template" ) type ( @@ -63,8 +60,10 @@ type ( ) func (p Plugin) Exec() error { - var buf bytes.Buffer - var b []byte + var ( + buf bytes.Buffer + b []byte + ) if p.Config.Template == "" { data := struct { @@ -76,15 +75,17 @@ func (p Plugin) Exec() error { fmt.Printf("Error: Failed to encode JSON payload. %s\n", err) return err } + b = buf.Bytes() } else { - txt, err := RenderTrim(p.Config.Template, p) + txt, err := template.RenderTrim(p.Config.Template, p) + if err != nil { return err } + text := txt b = []byte(text) - } // build and execute a request for each url. @@ -101,7 +102,6 @@ func (p Plugin) Exec() error { } r := bytes.NewReader(b) - req, err := http.NewRequest(p.Config.Method, uri.String(), r) if err != nil { @@ -121,13 +121,17 @@ func (p Plugin) Exec() error { } client := http.DefaultClient + if p.Config.SkipVerify { client = &http.Client{ Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, }, } } + resp, err := client.Do(req) if err != nil { @@ -146,7 +150,7 @@ func (p Plugin) Exec() error { if p.Config.Debug { fmt.Printf( - debugRespFormat, + debugFormat, i+1, req.URL, req.Method, @@ -166,5 +170,6 @@ func (p Plugin) Exec() error { } } } + return nil } diff --git a/template.go b/template.go deleted file mode 100644 index 880efd1..0000000 --- a/template.go +++ /dev/null @@ -1,137 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "net/http" - "net/url" - "strings" - "time" - "unicode" - "unicode/utf8" - - "github.com/aymerick/raymond" -) - -func init() { - raymond.RegisterHelpers(funcs) -} - -// Render parses and executes a template, returning the results in string format. -func Render(template string, payload interface{}) (s string, err error) { - u, err := url.Parse(template) - if err == nil { - switch u.Scheme { - case "http", "https": - res, err := http.Get(template) - if err != nil { - return s, err - } - defer res.Body.Close() - out, err := ioutil.ReadAll(res.Body) - if err != nil { - return s, err - } - template = string(out) - - case "file": - out, err := ioutil.ReadFile(u.Path) - if err != nil { - return s, err - } - template = string(out) - } - } - - return raymond.Render(template, payload) -} - -// RenderTrim parses and executes a template, returning the results in string -// format. The result is trimmed to remove left and right padding and newlines -// that may be added unintentially in the template markup. -func RenderTrim(template string, playload interface{}) (string, error) { - out, err := Render(template, playload) - return strings.Trim(out, " \n"), err -} - -var funcs = map[string]interface{}{ - "uppercasefirst": uppercaseFirst, - "uppercase": strings.ToUpper, - "lowercase": strings.ToLower, - "duration": toDuration, - "datetime": toDatetime, - "success": isSuccess, - "failure": isFailure, - "truncate": truncate, - "urlencode": urlencode, - "since": since, -} - -func truncate(s string, len int) string { - if utf8.RuneCountInString(s) <= len { - return s - } - runes := []rune(s) - return string(runes[:len]) - -} - -func uppercaseFirst(s string) string { - a := []rune(s) - a[0] = unicode.ToUpper(a[0]) - s = string(a) - return s -} - -func toDuration(started, finished float64) string { - return fmt.Sprint(time.Duration(finished-started) * time.Second) -} - -func toDatetime(timestamp float64, layout, zone string) string { - if len(zone) == 0 { - return time.Unix(int64(timestamp), 0).Format(layout) - } - loc, err := time.LoadLocation(zone) - if err != nil { - return time.Unix(int64(timestamp), 0).Local().Format(layout) - } - return time.Unix(int64(timestamp), 0).In(loc).Format(layout) -} - -func isSuccess(conditional bool, options *raymond.Options) string { - if !conditional { - return options.Inverse() - } - - switch options.ParamStr(0) { - case "success": - return options.Fn() - default: - return options.Inverse() - } -} - -func isFailure(conditional bool, options *raymond.Options) string { - if !conditional { - return options.Inverse() - } - - switch options.ParamStr(0) { - case "failure", "error", "killed": - return options.Fn() - default: - return options.Inverse() - } -} - -func urlencode(options *raymond.Options) string { - return url.QueryEscape(options.Fn()) -} - -func since(start int64) string { - // NOTE: not using `time.Since()` because the fractional second component - // will give us something like "40m12.917523438s" vs "40m12s". We lose - // some precision, but the format is much more readable. - now := time.Unix(time.Now().Unix(), 0) - return fmt.Sprint(now.Sub(time.Unix(start, 0))) -}