cli_class_tool 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b709fe8b17a110ea17968fb9d7d4ff45e2ceac57bed76c3dcda67438880dcdc4
4
- data.tar.gz: 1ff6831f0c5bfbc9d54d8dac31e18fdad3003ec0d527bfb3999e11a472b1591b
3
+ metadata.gz: bad275062a9f72ff201812eb6cd7871b9321b80b57f379f817cab483de42b738
4
+ data.tar.gz: e3cb70b12fe4831a4fdd194eaa4c67118efcfd9ffc0f1903524d74bdaf805e1a
5
5
  SHA512:
6
- metadata.gz: f408128eb985f8ad45949c452be46aa55edc028f1dc060c2870afdbea358016258071e0cc8aadf155e867c0f8e7be4295e8045c699eb0f82c501feaeb9b7502b
7
- data.tar.gz: 75d8bbfd502b9a331be9c57e25b7f824b2bf1d24c94f6fe477f64f2078dde1a76911b4964b0bdcf531098f895a753556778571f0a4354e1d2b733b1fedc56212
6
+ metadata.gz: 43cc9f0156398b92a5d1c7370c6c8c7dcf61d80e4ad9b18bec8a4a5bedceec7aa9e9af40e5d801554c7369aa746bac98c204ad949d2d27b381c4e14cbec93312
7
+ data.tar.gz: '0948e6f13057dd174140d84a7845c33a25e758a9ef018e10afb9c75bb734e87dffaf191e81091e01fe8c1dc9ff66437672364343d7ffc054f76cf0d591ebec1d'
data/CHANGELOG CHANGED
@@ -1,7 +1,14 @@
1
1
  ------------------
2
- 0.3.0 (2026-06-02)
2
+ 0.4.0 (2026-06-05)
3
3
  ------------------
4
4
 
5
+ * Add basic initializer for Common
6
+ * Add Common::run() that will auto spawn a Common object and run the command with it.
7
+ * log function can now be used in alternate object or class by including/extending CLIClassTool::Logger
8
+
9
+ ------------------
10
+ 0.3.0 (2026-06-02)
11
+ ------------------
5
12
 
6
13
  * Enforce module/class namespaces for all action classes (potentially breaking change)
7
14
  * Support automatic project-specific `RunError` generation with customized base error inheritance
@@ -1,39 +1,9 @@
1
1
  # Main module for generic CLI class-based tools and utilities
2
2
  module CLIClassTool
3
3
 
4
- # Common utility class providing logging, configuration, and shell execution methods
5
- class Common
6
- # Hook to enforce namespace loading at load time
7
- def self.inherited(subclass)
8
- if subclass.name
9
- parts = subclass.name.to_s.split('::')
10
- if parts.size <= 1
11
- raise "CLIClassTool action classes must be defined within a named module/class namespace"
12
- end
13
- end
14
- end
15
-
16
- # List of available actions for this class
17
- ACTION_LIST = [ :list_actions ]
18
- # Help text for actions
19
- ACTION_HELP = {}
20
-
4
+ # Logger for CLIClassTool::Common
5
+ module Logger
21
6
  private
22
- # Get the parent module of this class (e.g. KernelWork or XXX)
23
- def parent_module
24
- @parent_module ||= begin
25
- if self.class.name.nil?
26
- Object
27
- else
28
- parts = self.class.name.split('::')
29
- if parts.size <= 1
30
- raise "CLIClassTool action classes must be defined within a named module/class namespace"
31
- end
32
- Object.const_get(parts[0...-1].join('::'))
33
- end
34
- end
35
- end
36
-
37
7
  # Internal log method
38
8
  # @param lvl [String] Log level string (colored)
39
9
  # @param str [String] Message
@@ -50,28 +20,33 @@ module CLIClassTool
50
20
  out.print("# " + lvl.to_s() + ": " + str + "\r")
51
21
  end
52
22
 
53
- # Raise error if system command failed
54
- # @param check_err [Boolean] Whether to check for errors
55
- # @param sysret [Process::Status] System return status
56
- # @param ret [String, nil] Optional return message
23
+ # Compute the parent module of an object or a class
24
+ #
25
+ # @param obj [Object,Class] Object or class to get the Module from
57
26
  # @raise [StandardError] If command failed
58
- def abort_if_err(check_err, sysret, ret = nil)
59
- if sysret.exitstatus != 0 && check_err == true
60
- unless parent_module.const_defined?(:RunError)
61
- raise "CLIClassTool parent module #{parent_module} must extend CLIClassTool::Utils to define RunError"
27
+ def obj_to_parent_mod(obj)
28
+ return obj if obj.is_a?(Module)
29
+ theClass = obj.is_a?(Class) ? obj : obj.class
30
+ if theClass.name.nil?
31
+ return Object
32
+ else
33
+ parts = theClass.name.split('::')
34
+ if parts.size <= 1
35
+ raise "CLIClassTool action classes must be defined within a named module/class namespace"
62
36
  end
63
- raise(parent_module::RunError.new(sysret.exitstatus, ret))
37
+ return Object.const_get(parts[0...-1].join('::'))
64
38
  end
65
39
  end
66
40
 
67
- # Debug command execution
68
- # @param cmd_type [String] Type of command (e.g., 'git')
69
- # @param cmd [String] The command string
70
- def cmd_debug(cmd_type, cmd)
71
- log(:DEBUG, "Called from #{caller[1]}")
72
- log(:DEBUG, "Running #{cmd_type} command '#{cmd}'")
41
+ # Get the parent module of this class (e.g. KernelWork or XXX)
42
+ def parent_module
43
+ return @parent_module if @parent_module != nil
44
+
45
+ @parent_module = obj_to_parent_mod(self)
46
+ return @parent_module
73
47
  end
74
- protected
48
+
49
+ public
75
50
  # Log a message with a specific level
76
51
  #
77
52
  # @param lvl [Symbol] Log level (:DEBUG, :INFO, :WARNING, :ERROR, etc.)
@@ -121,8 +96,61 @@ module CLIClassTool
121
96
  end
122
97
  return rep
123
98
  end
99
+ end
100
+
101
+ # Common utility class providing logging, configuration, and shell execution methods
102
+ class Common
103
+ # Hook to enforce namespace loading at load time
104
+ def self.inherited(subclass)
105
+ if subclass.name
106
+ parts = subclass.name.to_s.split('::')
107
+ if parts.size <= 1
108
+ raise "CLIClassTool action classes must be defined within a named module/class namespace"
109
+ end
110
+ end
111
+ end
112
+
113
+ # List of available actions for this class
114
+ ACTION_LIST = [ :list_actions ]
115
+ # Help text for actions
116
+ ACTION_HELP = {}
117
+
118
+ # Give the Logger mathods to Common
119
+ include CLIClassTool::Logger
120
+
121
+ private
122
+ # Raise error if system command failed
123
+ # @param check_err [Boolean] Whether to check for errors
124
+ # @param sysret [Process::Status] System return status
125
+ # @param ret [String, nil] Optional return message
126
+ # @raise [StandardError] If command failed
127
+ def abort_if_err(check_err, sysret, ret = nil)
128
+ if sysret.exitstatus != 0 && check_err == true
129
+ unless parent_module.const_defined?(:RunError)
130
+ raise "CLIClassTool parent module #{parent_module} must extend CLIClassTool::Utils to define RunError"
131
+ end
132
+ raise(parent_module::RunError.new(sysret.exitstatus, ret))
133
+ end
134
+ end
135
+
136
+ # Debug command execution
137
+ # @param cmd_type [String] Type of command (e.g., 'git')
138
+ # @param cmd [String] The command string
139
+ def cmd_debug(cmd_type, cmd)
140
+ log(:DEBUG, "Called from #{caller[1]}")
141
+ log(:DEBUG, "Running #{cmd_type} command '#{cmd}'")
142
+ end
143
+
124
144
 
125
145
  public
146
+ # Simple initializer for a Common object
147
+ #
148
+ # @param path [String] Path to run commands from
149
+ def initialize(path=".", caller_obj=self)
150
+ @path = path
151
+ @parent_module = obj_to_parent_mod(caller_obj)
152
+ end
153
+
126
154
  # Run a shell command
127
155
  #
128
156
  # @param cmd [String] Command to run
@@ -136,6 +164,10 @@ module CLIClassTool
136
164
  return ret
137
165
  end
138
166
 
167
+ def self.run(path, cmd, check_err = true)
168
+ obj = Common.new(path, self)
169
+ return obj.run(cmd, check_err)
170
+ end
139
171
  # Run a shell command using system() (interactive)
140
172
  #
141
173
  # @param cmd [String] Command to run
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli_class_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Morey
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-06-02 00:00:00.000000000 Z
10
+ date: 2026-06-05 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake