feat: use os.Getenv() read environment variables for tpl

This commit is contained in:
lddsb
2020-12-16 11:30:10 +08:00
committed by Dee Luo
parent 2e94325c0e
commit d409acce48
3 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -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"]
+3 -1
View File
@@ -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",
+12 -1
View File
@@ -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