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

It'd be nice to also resolve templates from files #8

Closed
hairyhenderson opened this issue Jan 24, 2016 · 5 comments
Closed

It'd be nice to also resolve templates from files #8

hairyhenderson opened this issue Jan 24, 2016 · 5 comments
Milestone

Comments

@hairyhenderson
Copy link
Owner

Let's start with JSON support (because encoding/json). I figure usage like this would be useful:

data.json

{
  "name": {
    "first": "Joe"
  }
}

in.txt

Hello {{.data.name.first}}!

(where data comes from the name of the file)

$ gomplate -f data.json < in.txt
Hello Joe!
@hairyhenderson hairyhenderson added this to the v1.0.0 milestone Jan 24, 2016
@drewmacinnis
Copy link

Yeah, a -f would be handy.

@hairyhenderson
Copy link
Owner Author

Something that might be a bit simpler to implement, and would probably flow better with the rest of the built-in functions is:

Hello {{ (jsonfile "data.json").name.first }}
$ gomplate -d data.json < in.txt
Hello Joe!

@hairyhenderson
Copy link
Owner Author

...although if I'm taking a path or filename as the argument to jsonfile, then I don't need to pass it on the commandline...

Conversely, forcing a file to be named a specific way may not be a good plan either. Maybe something like:

Hello {{ (jsonfile "data").name.first }}
$ gomplate -d data.json < in.txt
Hello Joe!

Where "data" is resolved because the file is named data.json. I could support mappings with = like:

$ gomplate -d data=myotherfilename.json < in.txt
Hello Joe!

It'd be interesting to also support other data sources from URLs, so to treat the -d argument like: alias=URL, or:

$ gomplate -d data=https://some.url/where/i/can/find/data.json < in.txt
Hello Joe!

This implies a few things:

  1. jsonfile is a bad name - it should be something more generic like datasource
  2. in the absence of =, the argument is assumed to be a file in the cwd, and the alias is set to the file name (sans extension)
  3. paths (i.e. anything with a directory name in it, not just the filename) and URLs would require the alias

@drmdrew
Copy link

drmdrew commented May 14, 2016

I like the "data source alias" idea. Perhaps always use uri style? Like file:// https://?

@hairyhenderson
Copy link
Owner Author

yeah - well data.json is technically a valid relative URL, so if we always use the cwd as the base file:// url, there shouldn't be any special-casing needed.

I was also thinking I could support lots of different formats this way (eventually!) like yaml or toml or even XML, detecting by Content-Type with a fallback to file extension...

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

3 participants