carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.66k stars 135 forks source link

Allow for disambiguation of identical file names in different directories when using `--file-mark` #835

Open nbatista-aexp opened 1 year ago

nbatista-aexp commented 1 year ago

Describe the problem/challenge you have When using the --file-mark flag, only a filename is able to be specified, from one of the paths specified with the -f, --file flag. E.g., given the following directory structure:

a/
  b/
     some-file.yaml
  dataDir/
     some-file.yaml

with the following contents in b/some-file.yaml:

#@ load("@ytt:data", "data")
#@ load("@ytt:yaml", "yaml")
#@ load("@ytt:struct", "struct")
#@ vals = struct.encode(yaml.decode(data.read("some-file.yaml")))

data: #@ vals.foo.bar

and the following contents in dataDir/some-file.yaml:

foo:
  bar: "baz"

Running ytt -f a/b -f a/dataDir/ --file-mark some-file.yaml:type=data --output-files foo --debug (note use of the --debug flag) produces the following output:

-  (private false)
  files:
  - some-file.yaml
  - some-file.yaml
  libraries:
    <none>
total: 409.132µs

with no files in directory foo, where the intended output would be a file foo/some-file.yaml with contents:

data: baz

If we rename b/some-file.yaml to b/other-file.yaml, and run the same command, the output is now:

-  (private false)
  files:
  - other-file.yaml
  - some-file.yaml
  libraries:
    <none>
## file other-file.yaml (opts yamlmeta.DocSetOpts{WithoutComments:false, Strict:false, AssociatedName:"other-file.yaml"})
## file other-file.yaml (opts yamlmeta.DocSetOpts{WithoutComments:false, Strict:false, AssociatedName:"other-file.yaml"})
## file other-file.yaml (opts yamlmeta.DocSetOpts{WithoutComments:false, Strict:false, AssociatedName:"other-file.yaml"})
### ast (templated)
????: docset (obj=0xc00022eb60)
       1: doc (obj=0xc0001b6cd0)
           1: map (obj=0xc0001b6c80)
           6: key=data (obj=0xc0001829c0)
        comment:    1: '@ load("@ytt:data", "data")'
        comment:    2: '@ load("@ytt:yaml", "yaml")'
        comment:    3: '@ load("@ytt:struct", "struct")'
        comment:    4: '@ vals = struct.encode(yaml.decode(data.read("some-file.yaml")))'
        comment:    6: '@ vals.foo.bar'
            : <nil>
### template
src:  tmpl: code: | srccode
????:    1: __ytt_tpl2_set_ctx_type("yaml") |
????:    2: __ytt_tpl2_start_ctx("yaml") |
????:    3: __ytt_tpl2_start_node(1) # *yamlmeta.DocumentSet |
   1:    4: __ytt_tpl2_start_node(2) # *yamlmeta.Document | #@ load("@ytt:data", "data")
   1:    5: __ytt_tpl2_start_node(3) # *yamlmeta.Map | #@ load("@ytt:data", "data")
   1:    6: load("@ytt:data", "data") | #@ load("@ytt:data", "data")
   2:    7: load("@ytt:yaml", "yaml") | #@ load("@ytt:yaml", "yaml")
   3:    8: load("@ytt:struct", "struct") | #@ load("@ytt:struct", "struct")
   4:    9: vals = struct.encode(yaml.decode(data.read("some-file.yaml"))) | #@ vals = struct.encode(yaml.decode(data.read("some-file.yaml")))
   6:   10: __ytt_tpl2_start_node(4) # *yamlmeta.MapItem key=data | data: #@ vals.foo.bar
   6:   11: __ytt_tpl2_set_node(4, (vals.foo.bar)) # *yamlmeta.MapItem key=data | data: #@ vals.foo.bar
????:   12: __ytt_tpl2_end_ctx(None) | ### other-file.yaml result
data: baz
creating: foo/other-file.yaml
total: 2.204487ms

which produces the expected output file foo/other-file.yaml.

Describe the solution you'd like Allow --file-mark to accept absolute paths, or introduce an additional flag, something like --file-mark-absolute which allows for absolute paths to files to be marked.

Anything else you would like to add: This issue is similar to the discussion at https://github.com/carvel-dev/ytt/discussions/326, which is, in my opinion, fundamentally the same issue, but the proposed approach is a bit more specific; to allow for OS paths to specify filenames. Note as well that the discussion does not appear to have ever been resolved.


Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible" 👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

vmunishwar commented 1 year ago

Sorry for the late reply. Thanks for adding this issue. We are trying to reproduce this issue. Meanwhile, if you are interested to contribute to ytt, you are welcome to raise a PR for this issue. Thanks for reaching out!