gpt4 book ai didi

ruby - 如果存在,则使用 chef 更改文件中的文本,如果不存在,则添加

转载 作者:数据小太阳 更新时间:2023-10-29 09:03:14 28 4
gpt4 key购买 nike

我知道 w/chef 如果模式不存在,你可以在文件中添加一行

http://www.rubydoc.info/gems/chef/Chef/Util/FileEdit:insert_line_if_no_match

如果模式确实存在,您甚至可以更新文件中的一行

http://www.rubydoc.info/gems/chef/Chef/Util/FileEdit:search_file_replace_line

是否有方便的方法来添加(如果不存在)并替换(如果存在)?

最佳答案

为什么不自己写一个小 helper 呢?

def update_or_insert(regex, newline)
search_file_replace_line(regex, newline) || insert_line_if_no_match(regex, newline)
end

如果它确实找到任何要替换的内容,它将返回更改,或者如果它发现需要添加行,则返回更改。

关于ruby - 如果存在,则使用 chef 更改文件中的文本,如果不存在,则添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30267768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com