fix: Use template files on disk (#4)
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
fed6459f9d
commit
38d1dfb2d4
1 changed files with 11 additions and 12 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"embed"
|
"embed"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -79,14 +80,6 @@ func (t *TelegramNotifier) sendMessage(message string) error {
|
||||||
|
|
||||||
// formatMessage to create a message with a template file name (either embeded or on disk)
|
// formatMessage to create a message with a template file name (either embeded or on disk)
|
||||||
func (t *TelegramNotifier) formatMessage(templateFileName string, attachment interface{}) (message string, err error) {
|
func (t *TelegramNotifier) formatMessage(templateFileName string, attachment interface{}) (message string, err error) {
|
||||||
// Deduce if template file is embeded or is a file on disk
|
|
||||||
embeded := true
|
|
||||||
if _, err = os.Stat(templateFileName); os.IsExist(err) {
|
|
||||||
embeded = false
|
|
||||||
}
|
|
||||||
// Reinitialize the error because it was only used for the test
|
|
||||||
err = nil
|
|
||||||
|
|
||||||
// Create template
|
// Create template
|
||||||
templateName := path.Base(templateFileName)
|
templateName := path.Base(templateFileName)
|
||||||
templateFunctions := template.FuncMap{
|
templateFunctions := template.FuncMap{
|
||||||
|
@ -99,12 +92,12 @@ func (t *TelegramNotifier) formatMessage(templateFileName string, attachment int
|
||||||
tmpl := template.New(templateName).Funcs(templateFunctions)
|
tmpl := template.New(templateName).Funcs(templateFunctions)
|
||||||
|
|
||||||
// Parse template
|
// Parse template
|
||||||
if embeded {
|
if fileExists(templateFileName) {
|
||||||
log.Debugf("Parsing embeded template file %s", templateFileName)
|
|
||||||
tmpl, err = tmpl.ParseFS(templateFiles, templateFileName)
|
|
||||||
} else {
|
|
||||||
log.Debugf("Parsing template file %s", templateFileName)
|
log.Debugf("Parsing template file %s", templateFileName)
|
||||||
tmpl, err = tmpl.ParseFiles(templateFileName)
|
tmpl, err = tmpl.ParseFiles(templateFileName)
|
||||||
|
} else {
|
||||||
|
log.Debugf("Parsing embeded template file %s", templateFileName)
|
||||||
|
tmpl, err = tmpl.ParseFS(templateFiles, templateFileName)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("parse failed: %v", err)
|
return "", fmt.Errorf("parse failed: %v", err)
|
||||||
|
@ -122,6 +115,12 @@ func (t *TelegramNotifier) formatMessage(templateFileName string, attachment int
|
||||||
return message, nil
|
return message, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isFileTemplate returns true when the filename is a real file on the filesystem
|
||||||
|
func fileExists(filename string) bool {
|
||||||
|
_, err := os.Stat(filename)
|
||||||
|
return !errors.Is(err, os.ErrNotExist)
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyBalance to format and send a notification when the unpaid balance has changed
|
// NotifyBalance to format and send a notification when the unpaid balance has changed
|
||||||
// Implements the Notifier interface
|
// Implements the Notifier interface
|
||||||
func (t *TelegramNotifier) NotifyBalance(miner Miner) (err error) {
|
func (t *TelegramNotifier) NotifyBalance(miner Miner) (err error) {
|
||||||
|
|
Reference in a new issue