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

Dynamic filename when input dir is used #982

Closed
taraspos opened this issue Nov 12, 2020 · 2 comments
Closed

Dynamic filename when input dir is used #982

taraspos opened this issue Nov 12, 2020 · 2 comments
Labels

Comments

@taraspos
Copy link

I was looking in the documentation, but couldn't find if it is possible to use variables as part of filename.

For example I have a project like:

$ tree .
├── go.mod
└── {{env.project}}
    ├── data
    │   └── {{env.project}}.txt
    └── {{env.project}}.go

And after running project=serviceA gomplate --input-dir $(pwd) --output-dir /tmp/$project I want to get

$ sh tree /tmp/serviceA
├── go.mod
└── serviceA
    ├── data
    │   └── serviceA.txt
    └── serviceA.go

Then create similarly project serviceB, serviceC, etc.

Is it possible? If not, how do you think does it make sense to implement? How hard would that be?

Thanks for the tool, it is really nice, I like the simplicity of it.

@hairyhenderson
Copy link
Owner

@trane9991 Thanks for logging this, it's an interesting question!

I feel like there's a bit of an overlap here with some of the ideas discussed in #589 (at least in spirit, if not explicitly).

It should be possible to accomplish this with --output-map, and if you can manage to have valid template inputs as filenames (like {{.Env.project}} should work, in theory), then this should work:

$ project=serviceA gomplate --input-dir $(pwd) --output-map='/tmp/{{ .Env.project }}/{{ .in | tmpl.Inline }}'

And indeed it does:

$ echo "hello world" > '{{.Env.project}}.go'
$ mkdir data
$ touch 'data/{{.Env.project}}.txt'
$ project=foobar gomplate --input-dir $(pwd) --output-map='./{{ .Env.project}}/{{ .in | tmpl.Inline }}'
$ tree foobar
foobar
├── data
│   └── foobar.txt
└── foobar.go

1 directory, 2 files

Keep in mind that these file names are going to be a bit sketchy to keep as valid templates, but on most modern UNIX-like filesystems (i.e. on macOS/Linux at least) this sort of thing will work:

$ touch '{{ print "hello world" | toUpper }}'
$ gomplate --input-dir $(pwd) --output-map='out/{{ .in | tmpl.Inline }}'
$ tree .
.
├── out
│   └── HELLO\ WORLD
└── {{\ print\ "hello\ world"\ |\ toUpper\ }}

1 directory, 2 files

It's worth pointing out that you can also use a template file to create entirely new output files - see https://blog.hairyhenderson.ca/post/one_template_many_outputs/ for how to do that.

@hairyhenderson
Copy link
Owner

I think this question has been answered, so I'm closing this now! If not, please start a new discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants