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
|
||||||
|
|
||||||
|
44
main.go
44
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",
|
||||||
@ -296,25 +301,26 @@ func run(c *cli.Context) error {
|
|||||||
DeployTo: c.String("deploy.to"),
|
DeployTo: c.String("deploy.to"),
|
||||||
},
|
},
|
||||||
Config: Config{
|
Config: Config{
|
||||||
Token: c.String("token"),
|
Token: c.String("token"),
|
||||||
Debug: c.Bool("debug"),
|
Debug: c.Bool("debug"),
|
||||||
MatchEmail: c.Bool("match.email"),
|
MatchEmail: c.Bool("match.email"),
|
||||||
WebPreview: c.Bool("webpage.preview"),
|
WebPreview: c.Bool("webpage.preview"),
|
||||||
To: c.StringSlice("to"),
|
To: c.StringSlice("to"),
|
||||||
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"),
|
||||||
Photo: c.StringSlice("photo"),
|
TemplateVarsFile: c.String("template.vars.file"),
|
||||||
Document: c.StringSlice("document"),
|
Photo: c.StringSlice("photo"),
|
||||||
Sticker: c.StringSlice("sticker"),
|
Document: c.StringSlice("document"),
|
||||||
Audio: c.StringSlice("audio"),
|
Sticker: c.StringSlice("sticker"),
|
||||||
Voice: c.StringSlice("voice"),
|
Audio: c.StringSlice("audio"),
|
||||||
Location: c.StringSlice("location"),
|
Voice: c.StringSlice("voice"),
|
||||||
Video: c.StringSlice("video"),
|
Location: c.StringSlice("location"),
|
||||||
Venue: c.StringSlice("venue"),
|
Video: c.StringSlice("video"),
|
||||||
Format: c.String("format"),
|
Venue: c.StringSlice("venue"),
|
||||||
GitHub: c.Bool("github"),
|
Format: c.String("format"),
|
||||||
Socks5: c.String("socks5"),
|
GitHub: c.Bool("github"),
|
||||||
|
Socks5: c.String("socks5"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
plugin.go
58
plugin.go
@ -67,25 +67,26 @@ type (
|
|||||||
|
|
||||||
// Config for the plugin.
|
// Config for the plugin.
|
||||||
Config struct {
|
Config struct {
|
||||||
Token string
|
Token string
|
||||||
Debug bool
|
Debug bool
|
||||||
MatchEmail bool
|
MatchEmail bool
|
||||||
WebPreview bool
|
WebPreview bool
|
||||||
To []string
|
To []string
|
||||||
Message string
|
Message string
|
||||||
MessageFile string
|
MessageFile string
|
||||||
TemplateVars string
|
TemplateVarsFile string
|
||||||
Photo []string
|
TemplateVars string
|
||||||
Document []string
|
Photo []string
|
||||||
Sticker []string
|
Document []string
|
||||||
Audio []string
|
Sticker []string
|
||||||
Voice []string
|
Audio []string
|
||||||
Location []string
|
Voice []string
|
||||||
Video []string
|
Location []string
|
||||||
Venue []string
|
Video []string
|
||||||
Format string
|
Venue []string
|
||||||
GitHub bool
|
Format string
|
||||||
Socks5 string
|
GitHub bool
|
||||||
|
Socks5 string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin values.
|
// Plugin values.
|
||||||
@ -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