carvel-dev / carvel-dev/ytt

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

Open
#835 1 comment 5 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
1.9k
Forks
167
PR merge metrics
No merged PRs in 30d

Description

**Describe the problem/challenge you have**
When using the `--file-mark` flag, only a filename is able to be specified, from one of the `path`s 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:

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:

## 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'
:
### 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.

Contributor guide

Open the contributing guide

Research direction

Start at the CLI handling for --file-mark and reproduce the a/b and a/dataDir example with --debug. Trace how marked files are selected when basenames collide, then define and test unambiguous path-based selection with the requested output behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.