Class: CLIClassTool::Common
- Inherits:
-
Object
- Object
- CLIClassTool::Common
- Includes:
- Logger
- Defined in:
- lib/cli_class_tool/common.rb
Overview
Common utility class providing logging, configuration, and shell execution methods
Constant Summary collapse
- ACTION_LIST =
List of available actions for this class
[ :list_actions ]
- ACTION_HELP =
Help text for actions
{}
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Hook to enforce namespace loading at load time.
- .run(path, cmd, env: nil, catch_err: false, silent_err: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(path = ".", caller_obj = self) ⇒ Common
constructor
Simple initializer for a Common object.
-
#list_actions(opts) ⇒ Integer
List available actions.
-
#run(cmd, env: nil, catch_err: false, silent_err: false) ⇒ String
Run a shell command.
-
#runGit(cmd, env: nil, catch_err: false, silent_err: false) ⇒ String
Run a git command.
-
#runGitInteractive(cmd, env: nil, catch_err: false, silent_err: false) ⇒ Boolean
Run a git command interactively.
-
#runSystem(cmd, env: nil, catch_err: false, silent_err: false) ⇒ Boolean
Run a shell command using system() (interactive).
Methods included from Logger
Constructor Details
#initialize(path = ".", caller_obj = self) ⇒ Common
Simple initializer for a Common object
172 173 174 175 |
# File 'lib/cli_class_tool/common.rb', line 172 def initialize(path=".", caller_obj=self) @path = path @parent_module = obj_to_parent_mod(caller_obj) end |
Class Method Details
.inherited(subclass) ⇒ Object
Hook to enforce namespace loading at load time
120 121 122 123 124 125 126 127 |
# File 'lib/cli_class_tool/common.rb', line 120 def self.inherited(subclass) if subclass.name parts = subclass.name.to_s.split('::') if parts.size <= 1 raise "CLIClassTool action classes must be defined within a named module/class namespace" end end end |
.run(path, cmd, env: nil, catch_err: false, silent_err: false) ⇒ Object
194 195 196 197 |
# File 'lib/cli_class_tool/common.rb', line 194 def self.run(path, cmd, env: nil, catch_err: false, silent_err: false) obj = Common.new(path, self) return obj.run(cmd, env: env, catch_err: catch_err, silent_err: silent_err) end |
Instance Method Details
#list_actions(opts) ⇒ Integer
List available actions
254 255 256 257 258 259 |
# File 'lib/cli_class_tool/common.rb', line 254 def list_actions(opts) actions = parent_module.getActionAttr("ACTION_LIST").map(){|x| parent_module.actionToString(x)} actions.reject! { |x| x == "list_actions" } puts actions.join("\n") return 0 end |
#run(cmd, env: nil, catch_err: false, silent_err: false) ⇒ String
Run a shell command
185 186 187 188 189 190 191 192 |
# File 'lib/cli_class_tool/common.rb', line 185 def run(cmd, env: nil, catch_err: false, silent_err: false) cmd_debug('', cmd) env_prefix = env ? "#{env} " : "" redirect = silent_err ? " 2>/dev/null" : "" ret = `cd #{@path} && #{env_prefix}#{cmd}#{redirect}`.chomp() abort_if_err(catch_err, $?, ret) return ret end |
#runGit(cmd, env: nil, catch_err: false, silent_err: false) ⇒ String
Run a git command
224 225 226 227 228 229 230 231 |
# File 'lib/cli_class_tool/common.rb', line 224 def runGit(cmd, env: nil, catch_err: false, silent_err: false) cmd_debug('git', cmd) env_prefix = env ? "#{env} " : "" redirect = silent_err ? " 2>/dev/null" : "" ret = `cd #{@path} && #{env_prefix}git #{cmd}#{redirect}`.chomp() abort_if_err(catch_err, $?, ret) return ret end |
#runGitInteractive(cmd, env: nil, catch_err: false, silent_err: false) ⇒ Boolean
Run a git command interactively
241 242 243 244 245 246 247 248 |
# File 'lib/cli_class_tool/common.rb', line 241 def runGitInteractive(cmd, env: nil, catch_err: false, silent_err: false) cmd_debug('git interactive', cmd) env_prefix = env ? "#{env} " : "" redirect = silent_err ? " 2>/dev/null" : "" ret = system("cd #{@path} && #{env_prefix}git #{cmd}#{redirect}") abort_if_err(catch_err, $?) return ret end |
#runSystem(cmd, env: nil, catch_err: false, silent_err: false) ⇒ Boolean
Run a shell command using system() (interactive)
207 208 209 210 211 212 213 214 |
# File 'lib/cli_class_tool/common.rb', line 207 def runSystem(cmd, env: nil, catch_err: false, silent_err: false) cmd_debug('interactive', cmd) env_prefix = env ? "#{env} " : "" redirect = silent_err ? " 2>/dev/null" : "" ret = system("cd #{@path} && #{env_prefix}#{cmd}#{redirect}") abort_if_err(catch_err, $?) return ret end |