Option to load tempate variables from json file (#98)
This commit is contained in:
parent
47352c6cca
commit
f021aab825
30
DOCS.md
30
DOCS.md
@ -186,6 +186,33 @@ Build finished for *{{tpl.app}}* - *{{tpl.env}}*
|
|||||||
{{/success}}
|
{{/success}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Example configuration with a custom message template, with extra vars loaded from file (e.g. from previous build steps):
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- name: send telegram notification
|
||||||
|
image: appleboy/drone-telegram
|
||||||
|
settings:
|
||||||
|
token: xxxxxxxxxx
|
||||||
|
to: telegram_user_id
|
||||||
|
+ template_vars_file: build_report.json
|
||||||
|
+ message: >
|
||||||
|
+ {{#success build.status}}
|
||||||
|
+ build {{build.number}} succeeded, artefact version = {{tpl.artefact_version}}.
|
||||||
|
+ {{else}}
|
||||||
|
+ build {{build.number}} failed. Fix me please.
|
||||||
|
+ {{/success}}
|
||||||
|
```
|
||||||
|
|
||||||
|
Where `build_report.json` is:
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
...
|
||||||
|
"artefact_version": "0.2.3452"
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Example configuration with a custom socks5 URL:
|
Example configuration with a custom socks5 URL:
|
||||||
|
|
||||||
```diff
|
```diff
|
||||||
@ -215,6 +242,9 @@ message_file
|
|||||||
template_vars
|
template_vars
|
||||||
: define additional template vars. Example: `var1: hello` can be used within the template as `tpl.var1`
|
: define additional template vars. Example: `var1: hello` can be used within the template as `tpl.var1`
|
||||||
|
|
||||||
|
template_vars_file
|
||||||
|
: load additional template vars from json file. Example: given file content `{"var1":"hello"}`, variable can be used within the template as `tpl.var1`
|
||||||
|
|
||||||
photo
|
photo
|
||||||
: local file path
|
: local file path
|
||||||
|
|
||||||
|
6
main.go
6
main.go
@ -54,6 +54,11 @@ func main() {
|
|||||||
Usage: "additional template vars to be used in message, as JSON string",
|
Usage: "additional template vars to be used in message, as JSON string",
|
||||||
EnvVar: "PLUGIN_TEMPLATE_VARS,TELEGRAM_TEMPLATE_VARS,INPUT_TEMPLATE_VARS",
|
EnvVar: "PLUGIN_TEMPLATE_VARS,TELEGRAM_TEMPLATE_VARS,INPUT_TEMPLATE_VARS",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "template.vars.file",
|
||||||
|
Usage: "load additional template vars to be used in message, from json file",
|
||||||
|
EnvVar: "PLUGIN_TEMPLATE_VARS_FILE,TELEGRAM_TEMPLATE_VARS_FILE",
|
||||||
|
},
|
||||||
cli.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "photo",
|
Name: "photo",
|
||||||
Usage: "send photo message",
|
Usage: "send photo message",
|
||||||
@ -304,6 +309,7 @@ func run(c *cli.Context) error {
|
|||||||
Message: c.String("message"),
|
Message: c.String("message"),
|
||||||
MessageFile: c.String("message.file"),
|
MessageFile: c.String("message.file"),
|
||||||
TemplateVars: c.String("template.vars"),
|
TemplateVars: c.String("template.vars"),
|
||||||
|
TemplateVarsFile: c.String("template.vars.file"),
|
||||||
Photo: c.StringSlice("photo"),
|
Photo: c.StringSlice("photo"),
|
||||||
Document: c.StringSlice("document"),
|
Document: c.StringSlice("document"),
|
||||||
Sticker: c.StringSlice("sticker"),
|
Sticker: c.StringSlice("sticker"),
|
||||||
|
20
plugin.go
20
plugin.go
@ -74,6 +74,7 @@ type (
|
|||||||
To []string
|
To []string
|
||||||
Message string
|
Message string
|
||||||
MessageFile string
|
MessageFile string
|
||||||
|
TemplateVarsFile string
|
||||||
TemplateVars string
|
TemplateVars string
|
||||||
Photo []string
|
Photo []string
|
||||||
Document []string
|
Document []string
|
||||||
@ -283,6 +284,25 @@ func (p Plugin) Exec() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.Config.TemplateVarsFile != "" {
|
||||||
|
content, err := ioutil.ReadFile(p.Config.TemplateVarsFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to read file with template vars '%s': %v", p.Config.TemplateVarsFile, err)
|
||||||
|
}
|
||||||
|
var vars = make(map[string]string)
|
||||||
|
if err = json.Unmarshal(content, &vars); err != nil {
|
||||||
|
return fmt.Errorf("unable to unmarshall template vars from JSON file '%s': %v", p.Config.TemplateVarsFile, err)
|
||||||
|
}
|
||||||
|
// Merging templates variables from file to the variables form plugin settings (variables from file takes precedence)
|
||||||
|
if p.Tpl == nil {
|
||||||
|
p.Tpl = vars
|
||||||
|
} else {
|
||||||
|
for k, v := range vars {
|
||||||
|
p.Tpl[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var proxyURL *url.URL
|
var proxyURL *url.URL
|
||||||
if proxyURL, err = url.Parse(p.Config.Socks5); err != nil {
|
if proxyURL, err = url.Parse(p.Config.Socks5); err != nil {
|
||||||
return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err)
|
return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err)
|
||||||
|
@ -387,6 +387,37 @@ func TestTemplateVars(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTemplateVarsFile(t *testing.T) {
|
||||||
|
plugin := Plugin{
|
||||||
|
Repo: Repo{
|
||||||
|
Name: "go-hello",
|
||||||
|
Namespace: "appleboy",
|
||||||
|
},
|
||||||
|
Commit: Commit{
|
||||||
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
||||||
|
Author: "Bo-Yi Wu",
|
||||||
|
Branch: "master",
|
||||||
|
Message: "This is a test commit msg",
|
||||||
|
},
|
||||||
|
Build: Build{
|
||||||
|
Number: 101,
|
||||||
|
Status: "success",
|
||||||
|
Link: "https://github.com/appleboy/go-hello",
|
||||||
|
},
|
||||||
|
|
||||||
|
Config: Config{
|
||||||
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||||
|
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||||
|
Format: formatMarkdown,
|
||||||
|
MessageFile: "tests/message_template.txt",
|
||||||
|
TemplateVarsFile: "tests/vars.json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := plugin.Exec()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestProxySendMessage(t *testing.T) {
|
func TestProxySendMessage(t *testing.T) {
|
||||||
plugin := Plugin{
|
plugin := Plugin{
|
||||||
Repo: Repo{
|
Repo: Repo{
|
||||||
|
1
tests/vars.json
Normal file
1
tests/vars.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"env":"testing","version":"1.2.0-SNAPSHOT"}
|
Loading…
Reference in New Issue
Block a user