Class: CLIClassTool::Common

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Logger

#confirm, #log

Constructor Details

#initialize(path = ".", caller_obj = self) ⇒ Common

Simple initializer for a Common object

Parameters:

  • path (String) (defaults to: ".") —

    Path to run commands from



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

Parameters:

  • opts (Hash) —

    Options hash

Returns:

  • (Integer) —

    0



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

Parameters:

  • cmd (String) —

    Command to run

  • env (String, nil) (defaults to: nil) —

    Environment variable prefix (e.g., "FOO=bar")

  • catch_err (Boolean) (defaults to: false) —

    If true, do not raise error on failure

  • silent_err (Boolean) (defaults to: false) —

    If true, redirect stderr to /dev/null

Returns:

Raises:

  • (StandardError) —

    If command fails and catch_err is false



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

Parameters:

  • cmd (String) —

    Git command arguments

  • env (String, nil) (defaults to: nil) —

    Environment variable prefix (e.g., "FOO=bar")

  • catch_err (Boolean) (defaults to: false) —

    If true, do not raise error on failure

  • silent_err (Boolean) (defaults to: false) —

    If true, redirect stderr to /dev/null

Returns:

Raises:

  • (StandardError) —

    If command fails and catch_err is false



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

Parameters:

  • cmd (String) —

    Git command arguments

  • env (String, nil) (defaults to: nil) —

    Environment variable prefix (e.g., "FOO=bar")

  • catch_err (Boolean) (defaults to: false) —

    If true, do not raise error on failure

  • silent_err (Boolean) (defaults to: false) —

    If true, redirect stderr to /dev/null

Returns:

  • (Boolean) —

    Command success status

Raises:

  • (StandardError) —

    If command fails and catch_err is false



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)

Parameters:

  • cmd (String) —

    Command to run

  • env (String, nil) (defaults to: nil) —

    Environment variable prefix (e.g., "FOO=bar")

  • catch_err (Boolean) (defaults to: false) —

    If true, do not raise error on failure

  • silent_err (Boolean) (defaults to: false) —

    If true, redirect stderr to /dev/null

Returns:

  • (Boolean) —

    Command success status

Raises:

  • (StandardError) —

    If command fails and catch_err is false



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