我想围绕 knife 创建一个很好的包装类,以允许程序以可读的方式运行 knife 命令。我目前正在尝试使用 chef gem 中的 knife.rb 文件作为取得成功的指南。但是,我在关闭编辑器时遇到问题。如果我运行以下代码:
require 'chef/knife'
knife = Chef::Knife.new
knife.run(['client', 'create', 'new-client'], '--disable-editing')
它会导致以下错误:
NoMethodError: undefined method `merge!' for "--disable-editing":String
有人知道如何成功地做到这一点吗?有没有碰巧已经存在的图书馆可以满足我的需要?
最佳答案
所以我能够解决这个问题。它确实需要一个散列,但它希望它是 Mixlib::CLI 类的一个子集。因此,这是通过 knife 以编程方式创建客户端所需的代码:
class MyCLI
include Mixlib::CLI
end
#Add the option for disable editing. If you look in knife help, it's --disable-editing
MyCLI.option(:disable_editing, :long => "--disable-editing", :boolean => true)
#instantiate knife object and add the disable-editing flag to it
knife = Chef::Knife.new
knife.options=MyCLI.options
#set up client creation arguments and run
args = ['client', 'create', 'new_client', '--disable-editing' ]
new_client = Chef::Knife.run(args, MyCLI.options)
这不是最优雅的解决方案,但它确实通过命令行使用 knife 并且避免了必须使用系统调用才能使用它的人。
关于ruby - 在 ruby 类中调用 Knife ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826003/