hairyhenderson / gomplate

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.
https://gomplate.ca
MIT License
2.65k stars 181 forks source link

Turn off following symlinks #2195

Open kmkacpermajor opened 1 month ago

kmkacpermajor commented 1 month ago

Discussed in https://github.com/hairyhenderson/gomplate/discussions/2194

Originally posted by **kmkacpermajor** July 25, 2024 Hi, Is it possible to turn off following symlinks in gomplate.config? I have all files excluded except for !*.tmpl, and it still tries to follow symlinks (even though their names don't end with .tmpl). Because files that symlinks should follow don't exist on my computer, gomplate ends with an error "failed to gather templates for rendering: walkDir: ignore matching failed for ../..: applyPatterns: stat: stat testSymlink: no such file or directory".

I can't seem to find an option to turn off following symlinks in gomplate.

If there would be a config option to change os.Stat() to os.Lstat(), it would be great. It would read the information off of symlink, and then for my usage it would just be excluded.

hairyhenderson commented 1 month ago

@kmkacpermajor thanks for filing this - can you provide some more concrete details? what exactly does your configuration and/or gomplate commandline look like?

kmkacpermajor commented 1 month ago

Sure, To recreate this:

Error: failed to gather templates for rendering: walkDir: ignore matching failed for.: applyPatterns: stat: stat test_link: no such file or directory.

gomplate.config

excludes:
  - '*'
  - '!*.tmpl'

inputDir: .

outputMap:  |
  {{ .in | strings.ReplaceAll ".tmpl" "" }}

I'm on Ubuntu 22.04.1 LTS on WSL2, gomplate v4.1.0

kmkacpermajor commented 1 month ago

I can see that it is a problem with xignore library.

I think that if we operate only on files instead of links, then if the file doesn't exist it would be okay to mark it as unmatched. Problem is that it uses fs.Stat to check if current "file" is a file or a directory (in applyPatterns and makeResult).

Workaround would be to just ignore (continue in for) non existing files. They won't appear neither in matchedFiles or unmatchedFiles, so that is probably not a great fix... It would be great to just use os.Lstat since we only check if it is a dir. It returns FileInfo just like fs.Stat. But I'm not a GO developer, so I don't really know if it would hurt compatibilty-wise or something...