Module: Coo::Helper::CM
- Defined in:
- lib/coo/helper/cm.rb
Class Method Summary collapse
-
.open3(command_str, print_command: true, print_command_output: true) ⇒ Object
利用 open3 执行 shell 命令.
-
.sys(command_str) ⇒ Object
利用 system 语句执行 shell 命令.
- .sys_commands(*commands) ⇒ Object
Class Method Details
.open3(command_str, print_command: true, print_command_output: true) ⇒ Object
利用 open3 执行 shell 命令
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/coo/helper/cm.rb', line 43 def self.open3(command_str, print_command: true, print_command_output: true) puts current_time_str(command_str) if print_command result = '' exit_status = nil Open3.popen2e(command_str) do |stdin, stdout_and_stderr, wait_thr| stdout_and_stderr.sync = true stdout_and_stderr.each do |line| puts line.strip if print_command_output result << line end exit_status = wait_thr.value end if exit_status.exitstatus != 0 # 非0,执行失败 puts "error with \"#{command_str}\" !".red end if block_given? yield exit_status.exitstatus, result, command_str end exit_status.exitstatus end |
.sys(command_str) ⇒ Object
利用 system 语句执行 shell 命令
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/coo/helper/cm.rb', line 8 def self.sys(command_str) puts current_time_str(command_str) system command_str result = $? flag = result.to_s.split(' ').last.to_i if flag != 0 # 非0,执行失败 puts "error with \"#{command_str}\" !".red # exit end return flag end |
.sys_commands(*commands) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/coo/helper/cm.rb', line 21 def self.sys_commands(*commands) all_finish = true commands.each_with_index do |command, idx| if !command.kind_of? String puts "sys_commands 参数错误: #{command}" break end flag = self.sys(command) if block_given? flag = yield idx, flag end if flag != 0 puts 'command break 💥'.red all_finish = false break end end return all_finish end |