Skip to content

Commit

Permalink
Rename a few vars for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Sep 6, 2024
1 parent edafb26 commit 4408d4e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 45 deletions.
8 changes: 4 additions & 4 deletions examples/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ mail_from: user@test.mail
mail_to: user@test.mail
mail_on_success: false

subject_prefix: >
mail_subject: >
[mof@{{.hostname}}] ({{if .success}}success{{else}}error{{end}})
cmd: {{.command}}, exitcode: {{.exitcode}}
mail_template: |
cmd {{.command}}, exitcode {{.exitcode}}
mail_body: |
<b>Run Start</b>&nbsp;&nbsp;{{.run_start}}</br>
<b>Run End</b>&nbsp;&nbsp;&nbsp;&nbsp;{{.run_end}}</br>
<b>Duration</b>&nbsp;&nbsp;&nbsp;{{.run_duration}}</br></br>
<b>User</b>&nbsp;&nbsp;{{.user}}</br>
<b>Command</b>&nbsp;&nbsp;{{.command}}</br>
</br><b>Output</b></br><pre>{{.output}}</pre></br>
{{if not .success}}
{{if .error}}
</br><b>Error</b></br>{{.error}}</br>
{{end}}
</br><b>Exitcode</b></br>{{.exitcode}}</br>
40 changes: 20 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Synopsis

Mailtofail sends a notification email when a command fails. I hope that it is useful especially for cron jobs.
Mailtofail sends a notification email when a command fails. It might be useful especially for cron jobs.

## Syntax

Expand All @@ -20,7 +20,7 @@ Keep in mind that flags have to precede the command that should be executed. Usa
```shell
mailonfail ls -la

# set config file and log level
# set config file and log level, and run "ls -la"
mailonfail --log-level debug -c myconf.yaml ls -la
```

Expand All @@ -37,17 +37,17 @@ mail_from: user@test.mail
mail_to: user@test.mail
mail_on_success: false

subject_prefix: >
mail_subject: >
[mof@{{.hostname}}] ({{if .success}}success{{else}}error{{end}})
cmd: {{.command}}, exitcode: {{.exitcode}}
mail_template: |
cmd {{.command}}, exitcode {{.exitcode}}
mail_body: |
<b>Run Start</b>&nbsp;&nbsp;{{.run_start}}</br>
<b>Run End</b>&nbsp;&nbsp;&nbsp;&nbsp;{{.run_end}}</br>
<b>Duration</b>&nbsp;&nbsp;&nbsp;{{.run_duration}}</br></br>
<b>User</b>&nbsp;&nbsp;{{.user}}</br>
<b>Command</b>&nbsp;&nbsp;{{.command}}</br>
</br><b>Output</b></br><pre>{{.output}}</pre></br>
{{if not .success}}
{{if .error}}
</br><b>Error</b></br>{{.error}}</br>
{{end}}
</br><b>Exitcode</b></br>{{.exitcode}}</br>
Expand All @@ -56,15 +56,15 @@ mail_template: |
Every config value can be overwritten by an env var. This comes in handy in `crontabs`. Available env vars...

```go mdox-exec="sh/print_av_env_vars.sh"
MOF_MAIL_FROM
MOF_MAIL_ON_SUCCESS
MOF_MAIL_TEMPLATE
MOF_MAIL_TO
MOF_SMTP_HOST
MOF_SMTP_PASS
MOF_SMTP_PORT
MOF_SMTP_USER
MOF_SUBJECT_PREFIX
MOF_SMTP_PASS
MOF_MAIL_FROM
MOF_MAIL_TO
MOF_MAIL_ON_SUCCESS
MOF_MAIL_SUBJECT
MOF_MAIL_BODY
```

If `subject_prefix` and `mail_template` are not set they are loaded from the default [conf](src/default_conf.yaml).
Expand All @@ -74,20 +74,20 @@ If `subject_prefix` and `mail_template` are not set they are loaded from the def
The following variables are available in the mail template. Make sure to use golang template syntax (e.g. `{{.username}}`, `{{.output}}`). Available template vars...

```go mdox-exec="sh/print_av_tpl_vars.sh"
run_start
run_end
run_duration
command
output
error
exitcode
group_id
home
hostname
output
run_duration
run_end
run_start
success
user
hostname
user_id
group_id
user
user_name
home
```

## Help
Expand Down
2 changes: 1 addition & 1 deletion sh/print_av_env_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ basedir="${scriptdir%/*}"

src="${basedir}/src/conf.go"

cat "${src}" | grep -Po '(?<=case ").*(?=")' | sort
cat "${src}" | grep -Po '(?<=case ").*(?=")'
3 changes: 1 addition & 2 deletions sh/print_av_tpl_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ src="${basedir}/src/mail.go"

cat "${src}" |
grep -Po '"[a-z_]+": .*[a-zA-Z\.()],$' |
grep -Po '(?<=")[a-z_]+' |
sort
grep -Po '(?<=")[a-z_]+'
22 changes: 11 additions & 11 deletions src/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type tConf struct {
MailFrom string `yaml:"mail_from"`
MailTo string `yaml:"mail_to"`
MailOnSuccess bool `yaml:"mail_on_success"`
SubjectPrefix string `yaml:"subject_prefix"`
MailTemplate string `yaml:"mail_template"`
MailSubject string `yaml:"mail_subject"`
MailBody string `yaml:"mail_body"`
}

func initConf(cmd []string, configFile string, dryRun bool) (conf tConf) {
Expand Down Expand Up @@ -98,23 +98,23 @@ func getEnvVars(conf *tConf) *tConf {
case "MOF_MAIL_ON_SUCCESS":
lg.Debug(msg, logseal.F{"key": "mail_on_success", "val": val})
conf.MailOnSuccess = stringToBool(val)
case "MOF_SUBJECT_PREFIX":
conf.SubjectPrefix = val
case "MOF_MAIL_TEMPLATE":
conf.MailTemplate = val
case "MOF_MAIL_SUBJECT":
conf.MailSubject = val
case "MOF_MAIL_BODY":
conf.MailBody = val
}
}
}

var defaultConf tConf
if conf.SubjectPrefix == "" || conf.MailTemplate != "" {
if conf.MailSubject == "" || conf.MailBody != "" {
defaultConf = readDefaultConf()
}
if conf.SubjectPrefix == "" {
conf.SubjectPrefix = defaultConf.SubjectPrefix
if conf.MailSubject == "" {
conf.MailSubject = defaultConf.MailSubject
}
if conf.MailTemplate == "" {
conf.MailTemplate = defaultConf.MailTemplate
if conf.MailBody == "" {
conf.MailBody = defaultConf.MailBody
}
return conf
}
Expand Down
4 changes: 2 additions & 2 deletions src/default_conf.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
subject_prefix: >
mail_subject: >
[mof@{{.hostname}}] ({{if .success}}success{{else}}error{{end}})
cmd {{.command}}, exitcode {{.exitcode}}
mail_template: |
mail_body: |
<b>Run Start</b>&nbsp;&nbsp;{{.run_start}}</br>
<b>Run End</b>&nbsp;&nbsp;&nbsp;&nbsp;{{.run_end}}</br>
<b>Duration</b>&nbsp;&nbsp;&nbsp;{{.run_duration}}</br></br>
Expand Down
10 changes: 5 additions & 5 deletions src/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

func (conf tConf) sendMail(cr tCmdReturn) {
subject := conf.execTemplate(conf.SubjectPrefix, cr)
body := conf.execTemplate(conf.MailTemplate, cr)
subject := conf.execTemplate(conf.MailSubject, cr)
body := conf.execTemplate(conf.MailBody, cr)
lg.Trace("send mail", logseal.F{"body": body, "subject": subject})
if !conf.DryRun {
m := mail.NewMessage()
Expand All @@ -35,20 +35,20 @@ func (conf tConf) execTemplate(s string, cr tCmdReturn) string {
ui := getUserInfo()
templ := template.Must(template.New("tpl").Parse(s))
err := templ.Execute(buf, map[string]interface{}{
"command": fmt.Sprintf("%q", conf.Cmd),
"run_start": cr.RunStart,
"run_end": cr.RunEnd,
"run_duration": cr.RunDuration,
"command": fmt.Sprintf("%q", conf.Cmd),
"output": string(cr.Output),
"error": cr.Error,
"exitcode": cr.Exitcode,
"output": string(cr.Output),
"success": cr.Success,
"hostname": getHostName(),
"user_id": ui.UserID,
"group_id": ui.GroupID,
"user": ui.UserName,
"user_name": ui.Name,
"home": ui.Home,
"success": cr.Success,
})
lg.IfErrError("unable to use mail template", logseal.F{"error": err})
return buf.String()
Expand Down

0 comments on commit 4408d4e

Please sign in to comment.