Is it possible to call Git or other command line tools from inside a Thor script?

三世轮回 提交于 2019-12-06 03:20:54

问题


I find that I'm often running a sequence of routine 'cleanup' tasks before and after I make a git commit for my Rails 3 app.

I was thinking about putting these things into a Thor script, but one thing I haven't been able to figure out is how to use Thor (or Rake) to call other tools on the system.

Is it possible to call a command like git log from a Thor or Rake script, and if so what does that look like?

Thanks!


回答1:


Just shell out:

result = %x(git log)
puts result

or

system('git log')

if you just want to pass the output to the terminal.

There is also the grit gem that abstracts the Git tools into a Ruby library:

require 'grit'
repo = Grit::Repo.new("/path/to/repo")
repo.commits.each do |commit|
  puts "#{commit.id}: #{commit.message}"
end



回答2:


Don't forget that those are just Ruby files, so you can use everything in the Ruby arsenal there as well, so things like %x[rm -rf /], system("rm -rf /") and `rm -rf /` are accessible in those scripts too.



来源:https://stackoverflow.com/questions/4801920/is-it-possible-to-call-git-or-other-command-line-tools-from-inside-a-thor-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!