@@ -25,6 +25,64 @@ class Chef
2525 module Mixin
2626 module Command
2727
28+ # If command is a block, returns true if the block returns true, false if it returns false.
29+ # ("Only run this resource if the block is true")
30+ #
31+ # If the command is not a block, executes the command. If it returns any status other than
32+ # 0, it returns false (clearly, a 0 status code is true)
33+ #
34+ # === Parameters
35+ # command<Block>, <String>:: A block to check, or a string to execute
36+ #
37+ # === Returns
38+ # true:: Returns true if the block is true, or if the command returns 0
39+ # false:: Returns false if the block is false, or if the command returns a non-zero exit code.
40+ def only_if ( command )
41+ if Proc === command
42+ res = command . call
43+ unless res
44+ return false
45+ end
46+ else
47+ status = popen4 ( command ) { |p , i , o , e | i . close }
48+ if status . exitstatus != 0
49+ return false
50+ end
51+ end
52+ true
53+ end
54+
55+ module_function :only_if
56+
57+ # If command is a block, returns false if the block returns true, true if it returns false.
58+ # ("Do not run this resource if the block is true")
59+ #
60+ # If the command is not a block, executes the command. If it returns a 0 exitstatus, returns false.
61+ # ("Do not run this resource if the command returns 0")
62+ #
63+ # === Parameters
64+ # command<Block>, <String>:: A block to check, or a string to execute
65+ #
66+ # === Returns
67+ # true:: Returns true if the block is false, or if the command returns a non-zero exit status.
68+ # false:: Returns false if the block is true, or if the command returns a 0 exit status.
69+ def not_if ( command )
70+ if Proc === command
71+ res = command . call
72+ if res
73+ return false
74+ end
75+ else
76+ status = popen4 ( command ) { |p , i , o , e | i . close }
77+ if status . exitstatus == 0
78+ return false
79+ end
80+ end
81+ true
82+ end
83+
84+ module_function :not_if
85+
2886 def run_command ( args = { } )
2987 if args . has_key? ( :creates )
3088 if File . exists? ( args [ :creates ] )
@@ -33,39 +91,6 @@ def run_command(args={})
3391 end
3492 end
3593
36- if args . has_key? ( :only_if )
37- if Proc === args [ :only_if ]
38- res = args [ :only_if ] . call
39- unless res
40- Chef ::Log . debug ( "Skipping #{ args [ :command_string ] } - only_if #{ args [ :only_if ] } " )
41- return false
42- end
43- else
44- status = popen4 ( args [ :only_if ] ) { |p , i , o , e | }
45- if status . exitstatus != 0
46- Chef ::Log . debug ( "Skipping #{ args [ :command_string ] } - only_if #{ args [ :only_if ] } returned #{ status . exitstatus } " )
47- return false
48- end
49- end
50- end
51-
52- if args . has_key? ( :not_if )
53- if Proc === args [ :not_if ]
54- res = args [ :not_if ] . call
55- unless res
56- Chef ::Log . debug ( "Skipping #{ args [ :command_string ] } - not_if #{ args [ :not_if ] } " )
57- return false
58- end
59- else
60- Chef ::Log . debug ( "I should be running '#{ args [ :not_if ] } '" )
61- status = popen4 ( args [ :not_if ] ) { |p , i , o , e | }
62- if status . exitstatus == 0
63- Chef ::Log . debug ( "Skipping #{ args [ :command_string ] } - not_if #{ args [ :not_if ] } returned #{ status . exitstatus } " )
64- return false
65- end
66- end
67- end
68-
6994 exec_processing_block = lambda do |pid , stdin , stdout , stderr |
7095 stdin . close
7196
0 commit comments