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

feature request: raw=TRUE in write_yaml #123

Open
bschilder opened this issue Nov 9, 2022 · 2 comments
Open

feature request: raw=TRUE in write_yaml #123

bschilder opened this issue Nov 9, 2022 · 2 comments

Comments

@bschilder
Copy link

bschilder commented Nov 9, 2022

Hello!,

Thanks for the awesome package. I'm currently using it to read/write GitHub Actions (GHA) workflow files in my new R package / GH Action called rworkflows.

I have a scenario where I have a GHA workflow yaml that has the key "on". Thanks to the handler arg I managed to read in the yaml file.

However, I couldn't seem to figure out how to prevent the "on" key being written as 'on': (in single quotes) instead of on: when writing back to disk with write_yaml. This is an issue for GHA as their parser doesn't recognize keys with quotes.

After some playing around, I figured out that the solution was substituting the quoted character and then using file(..., raw=TRUE), but this feature isn't currently exposed to users via write_yaml (as far as I can tell).

Reprex

path <- tempfile(fileext = ".yml")
yml <- yaml::read_yaml("https://raw.githubusercontent.com/neurogenomics/rworkflows/master/inst/yaml/rworkflows_template.yml")

Issue

yaml::write_yaml(yml, path)

yaml file looks like:

name: rworkflows
'on':
  push:
  - master
  - main
  pull_request:
  - master
  - main

Solution

result <- gsub(shQuote("on"),"on",yaml::as.yaml(yml))
path <- file(path, "w", encoding = "UTF-8", raw=TRUE)
open(path, "w")
on.exit(close(path))
cat(result, file = path, sep = "")
name: rworkflows
on:
  push:
  - master
  - main
  pull_request:
  - master
  - main

If it's helpful, I'd be happy to implement the raw arg in write_yaml and make a Pull Request. Does that sound ok to you (maintainer[s])?

Thanks!,
Brian

Session info

``` R version 4.2.1 (2022-06-23) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] yaml_2.3.6

loaded via a namespace (and not attached):
[1] compiler_4.2.1 tools_4.2.1

</details>
@bschilder
Copy link
Author

Ah, actually I've just realized that I had misinterpreted the GHA error messages. These were happening bc i had forgotten to include the branch: part. This works fine, regardless of whether 'on': is quoted or not.

Still, if you think the raw arg might be helpful, still happy to add it.

@spgarbet
Copy link
Member

spgarbet commented Nov 9, 2022

This package sees a lot of traffic. I'm open to pull requests. The number one constraint is things can be added, but no existing behavior can be changed.

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

No branches or pull requests

2 participants