Merge pull request #6 from drone-plugins/template-lib

Use new template lib
This commit is contained in:
Thomas Boerger
2018-04-25 07:22:59 +02:00
committed by GitHub
6 changed files with 28 additions and 156 deletions
+1
View File
@@ -9,6 +9,7 @@ pipeline:
commands:
- go get -u github.com/golang/dep/cmd/dep
- dep ensure
- dep status
test:
image: golang:1.10
Generated
+16 -5
View File
@@ -3,9 +3,14 @@
[[projects]]
name = "github.com/aymerick/raymond"
packages = [".","ast","lexer","parser"]
revision = "a2232af10b53ef1ae5a767f5178db3a6c1dab655"
version = "v2.0.1"
packages = [
".",
"ast",
"lexer",
"parser"
]
revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11"
version = "v2.0.2"
[[projects]]
name = "github.com/coreos/go-semver"
@@ -13,6 +18,12 @@
revision = "8ab6407b697782a06568d4b7f1db25550ec2e4c6"
version = "v0.2.0"
[[projects]]
branch = "master"
name = "github.com/drone/drone-template-lib"
packages = ["template"]
revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
@@ -23,11 +34,11 @@
branch = "master"
name = "github.com/urfave/cli"
packages = ["."]
revision = "75104e932ac2ddb944a6ea19d9f9f26316ff1145"
revision = "8e01ec4cd3e2d84ab2fe90d8210528ffbb06d8ff"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "a89f1c3cf068c93fd8bf83840a119df2915c502193ca71a2b5ae4b6a6cfa4d70"
inputs-digest = "0fe5369d1efd31b54aea7d9c97316da432b06cf9a01e0a0c4a46ec4a5f148446"
solver-name = "gps-cdcl"
solver-version = 1
+9 -1
View File
@@ -1,11 +1,15 @@
[[constraint]]
name = "github.com/aymerick/raymond"
version = "2.0.1"
version = "2.0.2"
[[constraint]]
name = "github.com/coreos/go-semver"
version = "0.2.0"
[[constraint]]
branch = "master"
name = "github.com/drone/drone-template-lib"
[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
@@ -13,3 +17,7 @@
[[constraint]]
branch = "master"
name = "github.com/urfave/cli"
[prune]
go-tests = true
unused-packages = true
-3
View File
@@ -183,9 +183,6 @@ func run(c *cli.Context) error {
Job: Job{
Started: c.Int64("job.started"),
},
Auto: Auto{
Tags: []string{"1.0", "1"},
},
Config: Config{
Username: c.String("username"),
Password: c.String("password"),
+2 -6
View File
@@ -5,6 +5,7 @@ import (
"log"
"strings"
"github.com/drone/drone-template-lib/template"
"github.com/drone-plugins/drone-manifest/command"
)
@@ -38,10 +39,6 @@ type (
Started int64
}
Auto struct {
Tags []string
}
Config struct {
Username string
Password string
@@ -56,7 +53,6 @@ type (
Repo Repo
Build Build
Job Job
Auto Auto
Config Config
}
)
@@ -77,7 +73,7 @@ func (p *Plugin) Exec() error {
}
if p.Config.Spec != "" {
spec, err := RenderTrim(p.Config.Spec, p)
spec, err := template.RenderTrim(p.Config.Spec, p)
if err != nil {
return err
-141
View File
@@ -1,141 +0,0 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"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)
default:
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{}{
"uppercase": strings.ToUpper,
"lowercase": strings.ToLower,
"trimPrefix": strings.TrimPrefix,
"trimSuffix": strings.TrimSuffix,
"quote": strconv.Quote,
"join": strings.Join,
"uppercasefirst": uppercaseFirst,
"duration": toDuration,
"datetime": toDatetime,
"success": isSuccess,
"failure": isFailure,
"truncate": truncate,
"urlencode": urlencode,
"since": since,
}
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.Sprintln(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 truncate(s string, len int) string {
if utf8.RuneCountInString(s) <= len {
return s
}
runes := []rune(s)
return string(runes[:len])
}
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.Sprintln(now.Sub(time.Unix(start, 0)))
}