bogo-cli 0.2.14 → 0.2.16

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: 0e1628dc08e306638c61210f8c9afcd3b86eb3e2ddf2c15f9649835616bbdae5
4
- data.tar.gz: 68e1430dd4308355dac73e6bf1c4f8afc6b44d3e12fadf80e3ea6afce888ae51
3
+ metadata.gz: e3656ca0e9a54167aca89ef17945dcea51c9e5eddfcb5863bf961d29e607669c
4
+ data.tar.gz: '027490a1df8c80fc7b21740219710ca14a22ee6750d273f4fcd59fd410a345a1'
5
5
  SHA512:
6
- metadata.gz: b15becb12dacf2f1e9366c72428c7001aa3118badeb8ef007aafd7bfd98e50e58fc0e8ca63993d6a384e59f6e2cfae2de7d5c0ad6e5bf553d5e618f8b2e270d9
7
- data.tar.gz: 695dc796b33108aa26f544a4dc129a30a511df08287ebd656a7ab61051abaa8faba320f07f8fa964ced0a5153e9d42aa8350944492eafd52276dd51ae17a099e
6
+ metadata.gz: 65be75f9b91e26d4f2e7e6367ebd005a0e85dece0090927f65de924c9e19cc362bb5d5a7d890311f5917c14e0553b7b0a67a7d73ff3eacc2e6dd68ce6dba9f59
7
+ data.tar.gz: 56fb3b1c21a3063228d96ee1145fd2546760e257d165b653ad5716efd2ea2dcd7b5ab3bda719089eb3f3bb2b12e1b84e3ce38291fa2c355e6268e02724c530ce
@@ -1,3 +1,7 @@
1
+ # v0.2.16
2
+ * Provide default UI instance and use for error output
3
+ * Automatically configure logger on command setup
4
+
1
5
  # v0.2.14
2
6
  * Prevent unexpected exception when no original is wrapped (#11)
3
7
 
@@ -1,20 +1,20 @@
1
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
2
- require 'bogo-cli/version'
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + "/lib/"
2
+ require "bogo-cli/version"
3
3
  Gem::Specification.new do |s|
4
- s.name = 'bogo-cli'
4
+ s.name = "bogo-cli"
5
5
  s.version = Bogo::Cli::VERSION.version
6
- s.summary = 'CLI Helper libraries'
7
- s.author = 'Chris Roberts'
8
- s.email = 'code@chrisroberts.org'
9
- s.homepage = 'https://github.com/spox/bogo-cli'
10
- s.description = 'CLI Helper libraries'
11
- s.require_path = 'lib'
12
- s.license = 'Apache 2.0'
13
- s.add_runtime_dependency 'bogo', '>= 0.1.6', '< 0.6'
14
- s.add_runtime_dependency 'bogo-config', '>= 0.1.15', '< 0.5'
15
- s.add_runtime_dependency 'bogo-ui'
16
- s.add_runtime_dependency 'slop', '~> 3'
17
- s.add_development_dependency 'rake', '~> 10'
18
- s.add_development_dependency 'minitest'
19
- s.files = Dir['lib/**/*'] + %w(bogo-cli.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
6
+ s.summary = "CLI Helper libraries"
7
+ s.author = "Chris Roberts"
8
+ s.email = "code@chrisroberts.org"
9
+ s.homepage = "https://github.com/spox/bogo-cli"
10
+ s.description = "CLI Helper libraries"
11
+ s.require_path = "lib"
12
+ s.license = "Apache 2.0"
13
+ s.add_runtime_dependency "bogo", ">= 0.2.14", "< 0.6"
14
+ s.add_runtime_dependency "bogo-config", ">= 0.1.15", "< 0.5"
15
+ s.add_runtime_dependency "bogo-ui"
16
+ s.add_runtime_dependency "slop", "~> 3"
17
+ s.add_development_dependency "rake", "~> 10"
18
+ s.add_development_dependency "minitest"
19
+ s.files = Dir["lib/**/*"] + %w(bogo-cli.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
20
20
  end
@@ -1,11 +1,19 @@
1
- require 'bogo-ui'
2
- require 'bogo-config'
3
- require 'bogo-cli'
1
+ require "bogo-ui"
2
+ require "bogo-config"
3
+ require "bogo-cli"
4
4
 
5
5
  module Bogo
6
6
  module Cli
7
7
  # Abstract command class
8
8
  class Command
9
+ # Get or set default UI
10
+ #
11
+ # @param u [Bogo::Ui]
12
+ # @return [Bogo::Ui]
13
+ def self.ui(u = nil)
14
+ @ui = u if u
15
+ @ui
16
+ end
9
17
 
10
18
  include Bogo::Memoization
11
19
 
@@ -22,24 +30,25 @@ module Bogo
22
30
  #
23
31
  # @return [self]
24
32
  def initialize(cli_opts, args)
25
- if(cli_opts.is_a?(Slop))
33
+ if (cli_opts.is_a?(Slop))
26
34
  process_cli_options(cli_opts)
27
35
  else
28
36
  @defaults = Smash.new
29
37
  @options = cli_opts.to_hash.to_smash(:snake)
30
38
  [@options, *@options.values].compact.each do |hsh|
31
39
  next unless hsh.is_a?(Hash)
32
- hsh.delete_if{|k,v| v.nil?}
40
+ hsh.delete_if { |k, v| v.nil? }
33
41
  end
34
42
  end
35
43
  @arguments = validate_arguments!(args)
36
44
  load_config!
37
45
  ui_args = Smash.new(
38
46
  :app_name => options.fetch(:app_name,
39
- self.class.name.split('::').first
40
- )
47
+ self.class.name.split("::").first),
41
48
  ).merge(config)
42
49
  @ui = options.delete(:ui) || Ui.new(ui_args)
50
+ Bogo::Cli::Command.ui(ui)
51
+ configure_logger!
43
52
  end
44
53
 
45
54
  # Execute the command
@@ -51,6 +60,16 @@ module Bogo
51
60
 
52
61
  protected
53
62
 
63
+ # Configure the default logger based on current
64
+ # command configuration options
65
+ def configure_logger!
66
+ if config[:debug] || !ENV["DEBUG"].to_s.empty?
67
+ Bogo::Logger.logger.level = :debug
68
+ else
69
+ Bogo::Logger.logger.level = config.fetch(:log, :fatal)
70
+ end
71
+ end
72
+
54
73
  # Provides top level options with command specific options
55
74
  # merged to provide custom overrides
56
75
  #
@@ -66,12 +85,12 @@ module Bogo
66
85
  Smash[
67
86
  options.fetch(
68
87
  Bogo::Utility.snake(
69
- self.class.name.split('::').last
88
+ self.class.name.split("::").last
70
89
  ),
71
90
  Hash.new
72
- ).map{|k,v|
73
- unless(v.nil?)
74
- [k,v]
91
+ ).map { |k, v|
92
+ unless (v.nil?)
93
+ [k, v]
75
94
  end
76
95
  }.compact
77
96
  ]
@@ -82,16 +101,16 @@ module Bogo
82
101
  #
83
102
  # @return [Hash]
84
103
  def load_config!
85
- if(options[:config])
104
+ if (options[:config])
86
105
  config_inst = Config.new(options[:config])
87
- elsif(self.class.const_defined?(:DEFAULT_CONFIGURATION_FILES))
106
+ elsif (self.class.const_defined?(:DEFAULT_CONFIGURATION_FILES))
88
107
  path = self.class.const_get(:DEFAULT_CONFIGURATION_FILES).detect do |check|
89
108
  full_check = File.expand_path(check)
90
109
  File.exists?(full_check)
91
110
  end
92
111
  config_inst = Config.new(path) if path
93
112
  end
94
- if(config_inst)
113
+ if (config_inst)
95
114
  options.delete(:config)
96
115
  defaults_inst = Smash[
97
116
  config_class.new(
@@ -145,20 +164,20 @@ module Bogo
145
164
  ui.info("#{msg}... ", :nonewline)
146
165
  begin
147
166
  result = yield
148
- ui.puts ui.color('complete!', :green, :bold)
149
- if(result)
150
- ui.puts '---> Results:'
167
+ ui.puts ui.color("complete!", :green, :bold)
168
+ if (result)
169
+ ui.puts "---> Results:"
151
170
  case result
152
171
  when Hash
153
- result.each do |k,v|
154
- ui.puts ' ' << ui.color("#{k}: ", :bold) << v
172
+ result.each do |k, v|
173
+ ui.puts " " << ui.color("#{k}: ", :bold) << v
155
174
  end
156
175
  else
157
176
  ui.puts result
158
177
  end
159
178
  end
160
179
  rescue => e
161
- ui.puts ui.color('error!', :red, :bold)
180
+ ui.puts ui.color("error!", :red, :bold)
162
181
  ui.error "Reason - #{e}"
163
182
  raise
164
183
  end
@@ -171,15 +190,15 @@ module Bogo
171
190
  # @param cli_opts [Slop]
172
191
  # @return [NilClass]
173
192
  def process_cli_options(cli_opts)
174
- unless(cli_opts.is_a?(Slop))
193
+ unless (cli_opts.is_a?(Slop))
175
194
  raise TypeError.new "Expecting type `Slop` but received type `#{cli_opts.class}`"
176
195
  end
177
196
  @options = Smash.new
178
197
  @defaults = Smash.new
179
198
  cli_opts.each do |cli_opt|
180
- unless(cli_opt.value.nil?)
199
+ unless (cli_opt.value.nil?)
181
200
  opt_key = Bogo::Utility.snake(cli_opt.key)
182
- if(cli_opt.default?)
201
+ if (cli_opt.default?)
183
202
  @defaults[opt_key] = cli_opt.value
184
203
  else
185
204
  @options[opt_key] = cli_opt.value
@@ -195,20 +214,18 @@ module Bogo
195
214
  # @return [Array<String>]
196
215
  def validate_arguments!(list)
197
216
  chk_idx = list.find_index do |item|
198
- item.start_with?('-')
217
+ item.start_with?("-")
199
218
  end
200
- if(chk_idx)
219
+ if (chk_idx)
201
220
  marker = list.find_index do |item|
202
- item == '--'
221
+ item == "--"
203
222
  end
204
- if(marker.nil? || chk_idx.to_i < marker)
223
+ if (marker.nil? || chk_idx.to_i < marker)
205
224
  raise ArgumentError.new "Unknown CLI option provided `#{list[chk_idx]}`"
206
225
  end
207
226
  end
208
227
  list
209
228
  end
210
-
211
229
  end
212
-
213
230
  end
214
231
  end
@@ -1,32 +1,34 @@
1
1
  # Trigger shutdown on INT or TERM signals
2
- o_int = Signal.trap('INT'){
2
+ o_int = Signal.trap("INT") {
3
3
  o_int.call if o_int.respond_to?(:call)
4
- if(Bogo::Cli.exit_on_signal == false)
5
- Thread.main.raise SignalException.new('SIGINT')
4
+ if (Bogo::Cli.exit_on_signal == false)
5
+ Thread.main.raise SignalException.new("SIGINT")
6
6
  else
7
7
  exit 0
8
8
  end
9
9
  }
10
10
 
11
- o_term = Signal.trap('TERM'){
11
+ o_term = Signal.trap("TERM") {
12
12
  o_int.call if o_int.respond_to?(:call)
13
- if(Bogo::Cli.exit_on_signal == false)
14
- Thread.main.raise SignalException.new('SIGTERM')
13
+ if (Bogo::Cli.exit_on_signal == false)
14
+ Thread.main.raise SignalException.new("SIGTERM")
15
15
  else
16
16
  exit 0
17
17
  end
18
18
  }
19
19
 
20
- require 'bogo-cli'
20
+ require "bogo-cli"
21
21
 
22
22
  class Slop
23
23
  def bogo_cli_run(*args, &block)
24
24
  slop_run(*args, &block)
25
25
  old_runner = @runner
26
- @runner = proc{|*args| old_runner.call(*args); exit 0}
26
+ @runner = proc { |*args| old_runner.call(*args); exit 0 }
27
27
  end
28
+
28
29
  alias_method :slop_run, :run
29
30
  alias_method :run, :bogo_cli_run
31
+
30
32
  class Option
31
33
  def default?
32
34
  @value.nil?
@@ -50,23 +52,45 @@ module Bogo
50
52
  end
51
53
  puts slop_result.help
52
54
  exit -1
53
- rescue Bogo::Config::FileLoadError => e
54
- $stderr.puts "ERROR: #{[e, e.original].compact.join(' - ')}"
55
- if(ENV['DEBUG'] && e.original)
56
- $stderr.puts "Stacktrace: #{e.original.class}: #{e.original.message}\n#{e.original.backtrace.join("\n")}"
57
- end
58
- exit e.respond_to?(:exit_code) ? e.exit_code : -1
59
- rescue StandardError, ScriptError => e
60
- if(ENV['DEBUG'])
61
- $stderr.puts "ERROR: #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
62
- else
63
- $stderr.puts "ERROR: #{e.class}: #{e.message}"
55
+ rescue StandardError, ScriptError => err
56
+ output_error err.message
57
+ if ENV["DEBUG"]
58
+ output_debug "Stacktrace: #{err.class}: " \
59
+ "#{err.message}\n#{err.backtrace.join("\n")}"
60
+ if err.respond_to?(:original) && err.original
61
+ msg = "Original Stacktrace: #{err.original.class}: " \
62
+ "#{err.original.message}\n#{err.original.backtrace.join("\n")}"
63
+ output_debug msg
64
+ end
64
65
  end
65
- exit e.respond_to?(:exit_code) ? e.exit_code : -1
66
+ exit err.respond_to?(:exit_code) ? err.exit_code : -1
66
67
  end
67
68
  true
68
69
  end
69
70
 
71
+ # Write error message to UI. Uses formatted
72
+ # ui if available and falls back to stderr.
73
+ #
74
+ # @param string [String]
75
+ def output_error(string)
76
+ if Command.ui
77
+ Command.ui.error string
78
+ else
79
+ $stderr.puts "ERROR: #{string}"
80
+ end
81
+ end
82
+
83
+ # Write debug message to UI. Uses formatted
84
+ # ui if available and falls back to stderr.
85
+ #
86
+ # @param string [String]
87
+ def output_debug(string)
88
+ if Command.ui
89
+ Command.ui.debug string
90
+ else
91
+ $stderr.puts "DEBUG: #{string}"
92
+ end
93
+ end
70
94
  end
71
95
  end
72
96
  end
@@ -1,6 +1,6 @@
1
1
  module Bogo
2
2
  module Cli
3
3
  # Current library version
4
- VERSION = Gem::Version.new('0.2.14')
4
+ VERSION = Gem::Version.new("0.2.16")
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bogo-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.14
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-26 00:00:00.000000000 Z
11
+ date: 2018-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bogo
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.6
19
+ version: 0.2.14
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '0.6'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.6
29
+ version: 0.2.14
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '0.6'