diff --git a/README.md b/README.md index 7d7cc89..a52201d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,9 @@ Today is 2021-05-02! | name | type | e.g. | |:--------------------|:----------|:--------------------------------------| | `.Filename` | `string` | `today.txt` | +| `.Filepath` | `string` | full path of `.Filename` | | `.TemplateFilename` | `string` | `~/.config/zouch/templates/today.txt` | +| `.TemplateFilepath` | `string` | same as `.TemplateFilename` | ### Functions diff --git a/pkg/commands/preview.go b/pkg/commands/preview.go index ec5d499..2224f17 100644 --- a/pkg/commands/preview.go +++ b/pkg/commands/preview.go @@ -29,7 +29,10 @@ func (cmd *Command) previewFile(filename string) error { return fmt.Errorf("template for %s does not exist", filename) } - data := templateVariables(filename, tpl) + data, err := templateVariables(filename, tpl) + if err != nil { + return err + } if err := cmd.Renderer.RenderTemplate(cmd.Output, tpl, data); err != nil { return err diff --git a/pkg/commands/touch.go b/pkg/commands/touch.go index 980a0ed..75af3da 100644 --- a/pkg/commands/touch.go +++ b/pkg/commands/touch.go @@ -3,6 +3,7 @@ package commands import ( "os" "path" + "path/filepath" "github.com/Ryooooooga/zouch/pkg/errors" "github.com/Ryooooooga/zouch/pkg/repositories" @@ -88,7 +89,10 @@ func (cmd *Command) renderTemplate(filename string, tpl *repositories.TemplateFi } defer output.Close() - data := templateVariables(filename, tpl) + data, err := templateVariables(filename, tpl) + if err != nil { + return err + } if err := cmd.Renderer.RenderTemplate(output, tpl, data); err != nil { return err @@ -102,9 +106,16 @@ func (cmd *Command) renderTemplate(filename string, tpl *repositories.TemplateFi return nil } -func templateVariables(filename string, tpl *repositories.TemplateFile) map[string]any { +func templateVariables(filename string, tpl *repositories.TemplateFile) (map[string]any, error) { + filepath, err := filepath.Abs(filename) + if err != nil { + return nil, err + } + return map[string]any{ "Filename": filename, + "Filepath": filepath, "TemplateFilename": tpl.Path, - } + "TemplateFilepath": tpl.Path, + }, nil }