howzit 2.1.29 → 2.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/howzit/task.rb CHANGED
@@ -5,7 +5,7 @@ require 'English'
5
5
  module Howzit
6
6
  # Task object
7
7
  class Task
8
- attr_reader :type, :title, :action, :arguments, :parent, :optional, :default, :last_status
8
+ attr_reader :type, :title, :action, :arguments, :parent, :optional, :default, :last_status, :log_level
9
9
 
10
10
  ##
11
11
  ## Initialize a Task object
@@ -20,6 +20,7 @@ module Howzit
20
20
  ## @option attributes :title [String] task title
21
21
  ## @option attributes :action [String] task action
22
22
  ## @option attributes :parent [String] title of nested (included) topic origin
23
+ ## @option attributes :log_level [String] log level for this task (debug, info, warn, error)
23
24
  def initialize(attributes, optional: false, default: true)
24
25
  @prefix = "{bw}\u{25B7}\u{25B7} {x}"
25
26
  # arrow = "{bw}\u{279F}{x}"
@@ -30,6 +31,7 @@ module Howzit
30
31
  @parent = attributes[:parent] || nil
31
32
 
32
33
  @action = attributes[:action].render_arguments || nil
34
+ @log_level = attributes[:log_level]
33
35
 
34
36
  @optional = optional
35
37
  @default = default
@@ -62,12 +64,29 @@ module Howzit
62
64
  block = @action
63
65
  script = Tempfile.new('howzit_script')
64
66
  comm_file = ScriptComm.setup
67
+ old_log_level = apply_log_level
65
68
  begin
66
- script.write(block)
69
+ # Ensure support directory exists and install helpers
70
+ ScriptSupport.ensure_support_dir
71
+ ENV['HOWZIT_SUPPORT_DIR'] = ScriptSupport.support_dir
72
+
73
+ # Inject helper script loading
74
+ modified_block, interpreter = ScriptSupport.inject_helper(block)
75
+
76
+ script.write(modified_block)
67
77
  script.close
68
- File.chmod(0o777, script.path)
69
- res = system(%(/bin/sh -c "#{script.path}"))
78
+ File.chmod(0o755, script.path)
79
+
80
+ # Use appropriate interpreter command
81
+ cmd = ScriptSupport.execution_command_for(script.path, interpreter)
82
+ # If interpreter is nil, execute directly (will respect hashbang)
83
+ res = if interpreter.nil?
84
+ system(script.path)
85
+ else
86
+ system(cmd)
87
+ end
70
88
  ensure
89
+ restore_log_level(old_log_level) if old_log_level
71
90
  script.close
72
91
  script.unlink
73
92
  # Process script communication
@@ -97,6 +116,38 @@ module Howzit
97
116
  [output, matches[0].tasks.count]
98
117
  end
99
118
 
119
+ ##
120
+ ## Apply log level for this task
121
+ ##
122
+ def apply_log_level
123
+ return unless @log_level
124
+
125
+ level_map = {
126
+ 'debug' => 0,
127
+ 'info' => 1,
128
+ 'warn' => 2,
129
+ 'warning' => 2,
130
+ 'error' => 3
131
+ }
132
+ level_value = level_map[@log_level.downcase] || @log_level.to_i
133
+ old_level = Howzit.options[:log_level]
134
+ Howzit.options[:log_level] = level_value
135
+ Howzit.console.log_level = level_value
136
+ ENV['HOWZIT_LOG_LEVEL'] = @log_level.downcase
137
+ old_level
138
+ end
139
+
140
+ ##
141
+ ## Restore log level after task execution
142
+ ##
143
+ def restore_log_level(old_level)
144
+ return unless @log_level
145
+
146
+ Howzit.options[:log_level] = old_level
147
+ Howzit.console.log_level = old_level
148
+ ENV.delete('HOWZIT_LOG_LEVEL')
149
+ end
150
+
100
151
  ##
101
152
  ## Execute a run task
102
153
  ##
@@ -116,9 +167,11 @@ module Howzit
116
167
  Howzit.console.info("#{@prefix}{bg}Running {bw}#{display_title}{x}".c)
117
168
  ENV['HOWZIT_SCRIPTS'] = File.expand_path('~/.config/howzit/scripts')
118
169
  comm_file = ScriptComm.setup
170
+ old_log_level = apply_log_level
119
171
  begin
120
172
  res = system(@action)
121
173
  ensure
174
+ restore_log_level(old_log_level) if old_log_level
122
175
  # Process script communication
123
176
  ScriptComm.apply(comm_file) if comm_file
124
177
  end