Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a directory or multiple template file arguments. #41

Open
chasinglogic opened this issue Mar 9, 2018 · 5 comments
Open

Support a directory or multiple template file arguments. #41

chasinglogic opened this issue Mar 9, 2018 · 5 comments

Comments

@chasinglogic
Copy link

I would love to add this to our terraform process since it would fill in the gaps in HCL, however passing it each file or adding a makefile to the project seems like overkill.

Yasha should support a directory or multiple files via bash globbing so that:

yasha .

Would find all j2 templates and render them. Any files found would have the rendered file in the same directory. As an example consider the following directory structure:

myproject/
    mydir/
          sometemplate.tf.j2

When yasha is run as above in the myproject directory it would result in a file sometemplate.tf at the following path ./mydir/sometemplate.tf

I'd be happy to submit a patch for this if this is something that would be accepted.

@kblomqvist
Copy link
Owner

kblomqvist commented Mar 10, 2018

Globbing template files would be nice I think--no directory, as .j2 is not really a template extension. I have actually even think about to implement this. So yes PR would be accepted.

I would prefer something like this:

cd myproject
yasha **/*.j2

Additionally if the variables file is explicitly given it is used for all templates found. But if it is not given variables are looked for every template by the Yasha's automatic look up mechanism. Similarly variables given via cli call are passed to every template found, e.g.,

yasha --foo=bar **/*.j2

What do you think?

@kblomqvist
Copy link
Owner

Also if output file is explicitly given all templates are rendered (appended) into that file.

yasha -o output.txt **/*.j2

@chasinglogic
Copy link
Author

Sounds good to me, I'll get you a PR. Thanks!

@kblomqvist kblomqvist added this to the 5.0 milestone May 1, 2018
@kblomqvist
Copy link
Owner

kblomqvist commented Jul 6, 2018

I was thinking about how this could be supported. Maybe pathlib.Path.glob() could be useful. See, https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob. Maybe create yasha.Path derived from click.Path or click.ParamType. Then use yasha.Path instead of click.File as an option for template file name.

@kblomqvist
Copy link
Owner

kblomqvist commented Jul 17, 2018

I'm working on this. However, the whole cli() needs to be refactored to support it nicely. So far, I have the custom click option to support file globbing:

import click
import pathlib2 as pathlib

class TemplateFile(click.File):

    def glob(self, pattern, param, ctx):
        files = []
        for p in pathlib.Path('.').glob(pattern):
            if p.is_dir():
                continue # skip directories
            p = super().convert(str(p), param, ctx)
            files.append(p)
        return files

    def convert(self, value, param, ctx):
        try:
            return super().convert(value, param, ctx)
        except click.exceptions.BadParameter:
            if '*' in value:
                return self.glob(value, param, ctx)
            else:
                raise

@kblomqvist kblomqvist removed this from the 5.0 milestone Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants