From d409acce48802be27abf70c7c6c192e5ca7c2531 Mon Sep 17 00:00:00 2001 From: lddsb Date: Wed, 16 Dec 2020 11:30:10 +0800 Subject: [PATCH] feat: use os.Getenv() read environment variables for tpl --- Dockerfile | 2 +- main.go | 4 +++- plugin.go | 13 ++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7f4f7b1..f2e08b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,6 @@ RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux go build -a -o drone-dingtalk . FROM alpine:latest RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* COPY --from=builder /app/drone-dingtalk /bin -COPY --from=builder /app/tpls /app/tpls +COPY --from=builder /app/tpls /app/drone/dingtalk/message/tpls ENTRYPOINT ["/bin/drone-dingtalk"] diff --git a/main.go b/main.go index 12d4915..2f9a441 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "time" "github.com/joho/godotenv" "github.com/urfave/cli" @@ -16,7 +17,8 @@ func main() { app := cli.NewApp() app.Name = "Drone DingTalk Message Plugin" app.Usage = "Sending message to DingTalk group by robot using WebHook" - app.Copyright = "© 2018 Dee Luo" + year := time.Now().Year() + app.Copyright = fmt.Sprintf("© 2018-%d Dee Luo", year) app.Authors = []cli.Author{ { Name: "Dee Luo", diff --git a/plugin.go b/plugin.go index a6ba3f9..247cf9c 100644 --- a/plugin.go +++ b/plugin.go @@ -137,6 +137,12 @@ type ( // Exec execute WebHook func (p *Plugin) Exec() error { + if p.Config.Debug { + for _, e := range os.Environ() { + log.Println(e) + } + } + var err error if "" == p.Config.AccessToken { msg := "missing DingTalk access token" @@ -194,7 +200,7 @@ func fileExists(filePath string) bool { // getTpl get tpl from local file or remote file func (p *Plugin) getTpl() (tpl string, err error) { //var tpl string - tplDir := "/app/tpls" + tplDir := "/app/drone/dingtalk/message/tpls" if "" == p.Custom.Tpl { p.Custom.Tpl = fmt.Sprintf("%s/%s.tpl", tplDir, strings.ToLower(p.Config.MsgType)) } @@ -249,6 +255,11 @@ func (p *Plugin) fillTpl(tpl string) string { reg := regexp.MustCompile(`\[([^\[\]]*)]`) match := reg.FindAllStringSubmatch(tpl, -1) for _, m := range match { + // from environment + if envStr := os.Getenv(m[1]); envStr != "" { + tpl = strings.ReplaceAll(tpl, m[0], envStr) + } + // check if the keyword is legal if _, ok := envs[m[1]]; ok { // replace keyword