Skip to content

Commit

Permalink
feat: add .Filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryooooooga committed Mar 24, 2023
1 parent 698dae8 commit 628b800
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions pkg/commands/touch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"os"
"path"
"path/filepath"

"github.com/Ryooooooga/zouch/pkg/errors"
"github.com/Ryooooooga/zouch/pkg/repositories"
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 628b800

Please sign in to comment.