Skip to content

fsnode: add custom unmarshalers for File/Dir - #1497

Closed
mvo5 wants to merge 3 commits into
osbuild:mainfrom
mvo5:custom-unmarshalers-2-alternative
Closed

mvo5 wants to merge 3 commits into
osbuild:mainfrom
mvo5:custom-unmarshalers-2-alternative

Conversation

@mvo5

@mvo5 mvo5 commented May 6, 2025

Copy link
Copy Markdown
Contributor

Alternative implementation for #1479 that changes the structure of the fsnode as suggested by @achilleas-k and @lzap

I have no super strong opinion. One nice property of the original code with the getters/setters is that after NewFile() it would always be valid. This approach with the exported fields does not protect against changes to invalid data after the creation (or by creation from hand). It also adds a bunch of (extra) churn.


fsnode: add custom unmarshalers for File/Dir

This commit adds custom unmarshalers for fsnode.{File,Directory}
that also do validation. For convenience when unmarshaling a
fsnode.File we support an extra text: foo field so that
in an ImageConfig YAML we can write:

minimal_raw: &minimal_raw:
   image_config:
     files:
       - path: "/root/anaconda-ks.cfg"
         user: "root"
         group: "root"
         text: |
           # Run initial-setup on first boot
           # Created by osbuild
           firstboot --reconfig

The user can also use data: to put a base64 encoded string
in for binary files.


fsnode: remove Getters, export field to make JSON friendly

When we move ImageConfig into YAML we will need support for
fsnode.File as it is used as part of the ImageConfig.

This can be done in multiple ways, one is to add a custom
unmarshaller. This is done in PR#1479 but because of the
way the code was written the custom unmarshaling is rather
ugly.

This commit is an alternative version. As a starting point
the struct fields are simply exported and the getters of
the same name removed. Then we can add a custom unmarshal
that just validates.

mvo5 added 3 commits May 6, 2025 13:53
When validating paths in the fsnode module, use the `filepath`
module instead of the (more generic) `path`.
When we move `ImageConfig` into YAML we will need support for
`fsnode.File` as it is used as part of the ImageConfig.

This can be done in multiple ways, one is to add a custom
unmarshaller. This is done in PR#1479 but because of the
way the code was written the custom unmarshaling is rather
ugly.

This commit is an alternative version. As a starting point
the struct fields are simply exported and the getters of
the same name removed. Then we can add a custom unmarshal
that just validates.
This commit adds custom unmarshalers for fsnode.{File,Directory}
that also do validation. For convenience when unmarshaling a
`fsnode.File` we support an extra `text: foo` field so that
in an ImageConfig YAML we can write:
```yaml
minimal_raw: &minimal_raw:
   image_config:
     files:
       - path: "/root/anaconda-ks.cfg"
         user: "root"
         group: "root"
         text: |
           # Run initial-setup on first boot
           # Created by osbuild
           firstboot --reconfig
```
The user can also use `data:` to put a base64 encoded string
in for binary files.
@mvo5
mvo5 requested review from achilleas-k and lzap May 6, 2025 16:42
@mvo5

mvo5 commented May 6, 2025

Copy link
Copy Markdown
Contributor Author

I also close this one (for now) in favor of #1500 which is less code and (arguably) cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant