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



157
158
159
160
# File 'lib/cli_class_tool/common.rb', line 157

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



105
106
107
108
109
110
111
112
# File 'lib/cli_class_tool/common.rb', line 105

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, check_err = true) ⇒ Object



175
176
177
178
# File 'lib/cli_class_tool/common.rb', line 175

def self.run(path, cmd, check_err = true)
    obj = Common.new(path, self)
    return obj.run(cmd, check_err)
end

Instance Method Details

#list_actions(opts) ⇒ Integer

List available actions

Parameters:

  • opts (Hash)

    Options hash

Returns:

  • (Integer)

    0



224
225
226
227
# File 'lib/cli_class_tool/common.rb', line 224

def list_actions(opts)
    puts parent_module.getActionAttr("ACTION_LIST").map(){|x| parent_module.actionToString(x)}.join("\n")
    return 0
end

#run(cmd, check_err = true) ⇒ String

Run a shell command

Parameters:

  • cmd (String)

    Command to run

  • check_err (Boolean) (defaults to: true)

    Raise error on failure

Returns:

Raises:

  • (StandardError)

    If command fails and check_err is true



168
169
170
171
172
173
# File 'lib/cli_class_tool/common.rb', line 168

def run(cmd, check_err = true)
    cmd_debug('', cmd)
    ret = `cd #{@path} && #{cmd}`.chomp()
    abort_if_err(check_err, $?, ret)
    return ret
end

#runGit(cmd, opts = {}, check_err = true) ⇒ String

Run a git command

Parameters:

  • cmd (String)

    Git command arguments

  • opts (Hash) (defaults to: {})

    Options (e.g., :env)

  • check_err (Boolean) (defaults to: true)

    Raise error on failure

Returns:

Raises:

  • (StandardError)

    If command fails and check_err is true



199
200
201
202
203
204
# File 'lib/cli_class_tool/common.rb', line 199

def runGit(cmd, opts={}, check_err = true)
    cmd_debug('git', cmd)
    ret = `cd #{@path} && #{opts[:env]} git #{cmd}`.chomp()
    abort_if_err(check_err, $?, ret)
    return ret
end

#runGitInteractive(cmd, opts = {}, check_err = true) ⇒ Boolean

Run a git command interactively

Parameters:

  • cmd (String)

    Git command arguments

  • opts (Hash) (defaults to: {})

    Options (e.g., :env)

  • check_err (Boolean) (defaults to: true)

    Raise error on failure

Returns:

  • (Boolean)

    Command success status

Raises:

  • (StandardError)

    If command fails and check_err is true



213
214
215
216
217
218
# File 'lib/cli_class_tool/common.rb', line 213

def runGitInteractive(cmd, opts={}, check_err = true)
    cmd_debug('git interactive', cmd)
    ret = system("cd #{@path} && #{opts[:env]} git #{cmd}")
    abort_if_err(check_err, $?)
    return ret
end

#runSystem(cmd, check_err = true) ⇒ Boolean

Run a shell command using system() (interactive)

Parameters:

  • cmd (String)

    Command to run

  • check_err (Boolean) (defaults to: true)

    Raise error on failure

Returns:

  • (Boolean)

    Command success status

Raises:

  • (StandardError)

    If command fails and check_err is true



185
186
187
188
189
190
# File 'lib/cli_class_tool/common.rb', line 185

def runSystem(cmd, check_err = true)
    cmd_debug('interactive', cmd)
    ret = system("cd #{@path} && #{cmd}")
    abort_if_err(check_err, $?)
    return ret
end