cli-kit 3.0.1 → 3.1.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 +4 -4
- data/Gemfile +2 -2
- data/Gemfile.lock +15 -16
- data/cli-kit.gemspec +1 -1
- data/gen/lib/gen.rb +5 -1
- data/gen/template/Gemfile +1 -1
- data/gen/template/test/example_test.rb +6 -4
- data/gen/template/test/test_helper.rb +2 -1
- data/lib/cli/kit.rb +2 -0
- data/lib/cli/kit/base_command.rb +7 -2
- data/lib/cli/kit/command_registry.rb +1 -1
- data/lib/cli/kit/config.rb +12 -0
- data/lib/cli/kit/error_handler.rb +1 -1
- data/lib/cli/kit/executor.rb +18 -2
- data/lib/cli/kit/logger.rb +76 -0
- data/lib/cli/kit/system.rb +2 -2
- data/lib/cli/kit/util.rb +189 -0
- data/lib/cli/kit/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 283b38ecb21fd3caac8399447f06f5bccff261af
|
4
|
+
data.tar.gz: fe47b61139d7df53eca531693ba385c44a0c6455
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f8723304b346c20418debde33e203bc3794ae0defc4e34308a7c2fbf5364fb22c41f7b7e800bbe5f2fb76a0ac90ca37e980c5ec641c71b3deb19d277d029b19
|
7
|
+
data.tar.gz: 6f08ef957f6f63b6fe108b6b6e118b5f887a9050ae8aed548445d8b77ff8ec47ff5a488f881b3e95e3682c30ba7098196876096a5cd70fd7fe312dd74292928c
|
data/Gemfile
CHANGED
@@ -4,13 +4,13 @@ source "https://rubygems.org"
|
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
group :development, :test do
|
7
|
-
gem 'rubocop'
|
7
|
+
gem 'rubocop', '~> 0.56.0'
|
8
8
|
gem 'byebug'
|
9
9
|
gem 'method_source'
|
10
10
|
end
|
11
11
|
|
12
12
|
group :test do
|
13
|
-
gem 'mocha', require: false
|
13
|
+
gem 'mocha', '~> 1.5.0', require: false
|
14
14
|
gem 'minitest', '>= 5.0.0', require: false
|
15
15
|
gem 'minitest-reporters', require: false
|
16
16
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
cli-kit (3.0
|
5
|
-
cli-ui (>= 1.1.
|
4
|
+
cli-kit (3.1.0)
|
5
|
+
cli-ui (>= 1.1.4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
ansi (1.5.0)
|
11
|
-
ast (2.
|
11
|
+
ast (2.4.0)
|
12
12
|
builder (3.2.3)
|
13
13
|
byebug (9.0.6)
|
14
|
-
cli-ui (1.1.
|
14
|
+
cli-ui (1.1.4)
|
15
15
|
metaclass (0.0.4)
|
16
16
|
method_source (0.8.2)
|
17
17
|
minitest (5.10.2)
|
@@ -20,24 +20,23 @@ GEM
|
|
20
20
|
builder
|
21
21
|
minitest (>= 5.0)
|
22
22
|
ruby-progressbar
|
23
|
-
mocha (1.
|
23
|
+
mocha (1.5.0)
|
24
24
|
metaclass (~> 0.0.1)
|
25
|
-
parallel (1.
|
26
|
-
parser (2.
|
27
|
-
ast (~> 2.
|
25
|
+
parallel (1.12.1)
|
26
|
+
parser (2.5.1.0)
|
27
|
+
ast (~> 2.4.0)
|
28
28
|
powerpack (0.1.1)
|
29
|
-
rainbow (
|
30
|
-
rake
|
29
|
+
rainbow (3.0.0)
|
31
30
|
rake (10.5.0)
|
32
|
-
rubocop (0.
|
31
|
+
rubocop (0.56.0)
|
33
32
|
parallel (~> 1.10)
|
34
|
-
parser (>= 2.
|
33
|
+
parser (>= 2.5)
|
35
34
|
powerpack (~> 0.1)
|
36
|
-
rainbow (>=
|
35
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
36
|
ruby-progressbar (~> 1.7)
|
38
37
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
39
38
|
ruby-progressbar (1.8.1)
|
40
|
-
unicode-display_width (1.3.
|
39
|
+
unicode-display_width (1.3.2)
|
41
40
|
|
42
41
|
PLATFORMS
|
43
42
|
ruby
|
@@ -49,9 +48,9 @@ DEPENDENCIES
|
|
49
48
|
method_source
|
50
49
|
minitest (>= 5.0.0)
|
51
50
|
minitest-reporters
|
52
|
-
mocha
|
51
|
+
mocha (~> 1.5.0)
|
53
52
|
rake (~> 10.0)
|
54
|
-
rubocop
|
53
|
+
rubocop (~> 0.56.0)
|
55
54
|
|
56
55
|
BUNDLED WITH
|
57
56
|
1.16.1
|
data/cli-kit.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ['lib']
|
23
23
|
|
24
|
-
spec.add_runtime_dependency 'cli-ui', '>= 1.1.
|
24
|
+
spec.add_runtime_dependency 'cli-ui', '>= 1.1.4'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
27
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
data/gen/lib/gen.rb
CHANGED
@@ -8,7 +8,10 @@ module Gen
|
|
8
8
|
|
9
9
|
TOOL_NAME = 'cli-kit'
|
10
10
|
ROOT = File.expand_path('../../..', __FILE__)
|
11
|
-
|
11
|
+
|
12
|
+
TOOL_CONFIG_PATH = File.expand_path(File.join('~', '.config', TOOL_NAME))
|
13
|
+
LOG_FILE = File.join(TOOL_CONFIG_PATH, 'logs', 'log.log')
|
14
|
+
DEBUG_LOG_FILE = File.join(TOOL_CONFIG_PATH, 'logs', 'debug.log')
|
12
15
|
|
13
16
|
autoload(:Generator, 'gen/generator')
|
14
17
|
|
@@ -17,6 +20,7 @@ module Gen
|
|
17
20
|
|
18
21
|
autocall(:Config) { CLI::Kit::Config.new(tool_name: TOOL_NAME) }
|
19
22
|
autocall(:Command) { CLI::Kit::BaseCommand }
|
23
|
+
autocall(:Logger) { CLI::Kit::Logger.new(debug_log_file: DEBUG_LOG_FILE) }
|
20
24
|
|
21
25
|
autocall(:Executor) { CLI::Kit::Executor.new(log_file: LOG_FILE) }
|
22
26
|
autocall(:Resolver) do
|
data/gen/template/Gemfile
CHANGED
@@ -6,10 +6,12 @@ module __App__
|
|
6
6
|
|
7
7
|
def test_example
|
8
8
|
CLI::Kit::System.fake("ls -al", stdout: "a\nb", success: true)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
|
10
|
+
out, = CLI::Kit::System.capture2('ls', '-al')
|
11
|
+
assert_equal %w(a b), out.split("\n")
|
12
|
+
|
13
|
+
errors = assert_all_commands_run(should_raise: false)
|
14
|
+
assert_nil errors, "expected command to run successfully"
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
data/lib/cli/kit.rb
CHANGED
@@ -11,9 +11,11 @@ module CLI
|
|
11
11
|
autoload :Executor, 'cli/kit/executor'
|
12
12
|
autoload :Ini, 'cli/kit/ini'
|
13
13
|
autoload :Levenshtein, 'cli/kit/levenshtein'
|
14
|
+
autoload :Logger, 'cli/kit/logger'
|
14
15
|
autoload :Resolver, 'cli/kit/resolver'
|
15
16
|
autoload :Support, 'cli/kit/support'
|
16
17
|
autoload :System, 'cli/kit/system'
|
18
|
+
autoload :Util, 'cli/kit/util'
|
17
19
|
|
18
20
|
EXIT_FAILURE_BUT_NOT_BUG = 30
|
19
21
|
EXIT_BUG = 1
|
data/lib/cli/kit/base_command.rb
CHANGED
@@ -17,8 +17,7 @@ module CLI
|
|
17
17
|
|
18
18
|
def self.call(args, command_name)
|
19
19
|
cmd = new
|
20
|
-
stats_tags =
|
21
|
-
stats_tags << "subcommand:#{args.first}" if args && args.first && cmd.has_subcommands?
|
20
|
+
stats_tags = cmd.stats_tags(args)
|
22
21
|
begin
|
23
22
|
statsd_increment("cli.command.invoked", tags: stats_tags)
|
24
23
|
statsd_time("cli.command.time", tags: stats_tags) do
|
@@ -31,6 +30,12 @@ module CLI
|
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
33
|
+
def stats_tags(args)
|
34
|
+
tags = ["task:#{self.class}"]
|
35
|
+
tags << "subcommand:#{args.first}" if args&.first && has_subcommands?
|
36
|
+
tags
|
37
|
+
end
|
38
|
+
|
34
39
|
def call(_args, _command_name)
|
35
40
|
raise NotImplementedError
|
36
41
|
end
|
data/lib/cli/kit/config.rb
CHANGED
@@ -27,6 +27,18 @@ module CLI
|
|
27
27
|
all_configs.dig("[#{section}]", name) || false
|
28
28
|
end
|
29
29
|
|
30
|
+
# Coalesce and enforce the value of a config to a boolean
|
31
|
+
def get_bool(section, name)
|
32
|
+
case get(section, name).to_s
|
33
|
+
when "true"
|
34
|
+
true
|
35
|
+
when "false"
|
36
|
+
false
|
37
|
+
else
|
38
|
+
raise CLI::Kit::Abort, "Invalid config: #{section}.#{name} is expected to be true or false"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
30
42
|
# Sets the config value in the config file
|
31
43
|
#
|
32
44
|
# #### Parameters
|
data/lib/cli/kit/executor.rb
CHANGED
@@ -1,22 +1,38 @@
|
|
1
1
|
require 'cli/kit'
|
2
2
|
require 'English'
|
3
|
+
require 'fileutils'
|
3
4
|
|
4
5
|
module CLI
|
5
6
|
module Kit
|
6
7
|
class Executor
|
7
8
|
def initialize(log_file:)
|
9
|
+
FileUtils.mkpath(File.dirname(log_file))
|
8
10
|
@log_file = log_file
|
9
11
|
end
|
10
12
|
|
11
13
|
def call(command, command_name, args)
|
12
|
-
with_traps
|
14
|
+
with_traps do
|
15
|
+
with_logging do |id|
|
16
|
+
begin
|
17
|
+
command.call(args, command_name)
|
18
|
+
rescue => e
|
19
|
+
$stderr.puts "This command ran with ID: #{id}"
|
20
|
+
$stderr.puts "Please include this information in any issues/report along with relevant logs"
|
21
|
+
raise e
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
13
25
|
end
|
14
26
|
|
15
27
|
private
|
16
28
|
|
17
29
|
def with_logging(&block)
|
18
30
|
return yield unless @log_file
|
19
|
-
CLI::UI.log_output_to(@log_file
|
31
|
+
CLI::UI.log_output_to(@log_file) do
|
32
|
+
CLI::UI::StdoutRouter.with_id(on_streams: [CLI::UI::StdoutRouter.duplicate_output_to]) do |id|
|
33
|
+
block.call(id)
|
34
|
+
end
|
35
|
+
end
|
20
36
|
end
|
21
37
|
|
22
38
|
def with_traps
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module CLI
|
5
|
+
module Kit
|
6
|
+
class Logger
|
7
|
+
MAX_LOG_SIZE = 5 * 1024 * 1000 # 5MB
|
8
|
+
MAX_NUM_LOGS = 10
|
9
|
+
|
10
|
+
# Constructor for CLI::Kit::Logger
|
11
|
+
#
|
12
|
+
# @param debug_log_file [String] path to the file where debug logs should be stored
|
13
|
+
def initialize(debug_log_file:)
|
14
|
+
FileUtils.mkpath(File.dirname(debug_log_file))
|
15
|
+
@debug_logger = ::Logger.new(debug_log_file, MAX_NUM_LOGS, MAX_LOG_SIZE)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Functionally equivalent to Logger#info
|
19
|
+
# Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id
|
20
|
+
#
|
21
|
+
# @param msg [String] the message to log
|
22
|
+
# @param debug [Boolean] determines if the debug logger will receive the log (default true)
|
23
|
+
def info(msg, debug: true)
|
24
|
+
$stdout.puts CLI::UI.fmt(msg)
|
25
|
+
@debug_logger.info(format_debug(msg)) if debug
|
26
|
+
end
|
27
|
+
|
28
|
+
# Functionally equivalent to Logger#warn
|
29
|
+
# Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id
|
30
|
+
#
|
31
|
+
# @param msg [String] the message to log
|
32
|
+
# @param debug [Boolean] determines if the debug logger will receive the log (default true)
|
33
|
+
def warn(msg, debug: true)
|
34
|
+
$stdout.puts CLI::UI.fmt("{{yellow:#{msg}}}")
|
35
|
+
@debug_logger.warn(format_debug(msg)) if debug
|
36
|
+
end
|
37
|
+
|
38
|
+
# Functionally equivalent to Logger#error
|
39
|
+
# Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id
|
40
|
+
#
|
41
|
+
# @param msg [String] the message to log
|
42
|
+
# @param debug [Boolean] determines if the debug logger will receive the log (default true)
|
43
|
+
def error(msg, debug: true)
|
44
|
+
$stderr.puts CLI::UI.fmt("{{red:#{msg}}}")
|
45
|
+
@debug_logger.error(format_debug(msg)) if debug
|
46
|
+
end
|
47
|
+
|
48
|
+
# Functionally equivalent to Logger#fatal
|
49
|
+
# Also logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id
|
50
|
+
#
|
51
|
+
# @param msg [String] the message to log
|
52
|
+
# @param debug [Boolean] determines if the debug logger will receive the log (default true)
|
53
|
+
def fatal(msg, debug: true)
|
54
|
+
$stderr.puts CLI::UI.fmt("{{red:{{bold:Fatal:}} #{msg}}}")
|
55
|
+
@debug_logger.fatal(format_debug(msg)) if debug
|
56
|
+
end
|
57
|
+
|
58
|
+
# Similar to Logger#debug, however will not output to STDOUT unless DEBUG env var is set
|
59
|
+
# Logs to the debug file, taking into account CLI::UI::StdoutRouter.current_id
|
60
|
+
#
|
61
|
+
# @param msg [String] the message to log
|
62
|
+
def debug(msg)
|
63
|
+
$stdout.puts CLI::UI.fmt(msg) if ENV['DEBUG']
|
64
|
+
@debug_logger.debug(format_debug(msg))
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def format_debug(msg)
|
70
|
+
msg = CLI::UI.fmt(msg)
|
71
|
+
return msg unless CLI::UI::StdoutRouter.current_id
|
72
|
+
"[#{CLI::UI::StdoutRouter.current_id[:id]}] #{msg}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/lib/cli/kit/system.rb
CHANGED
@@ -117,10 +117,10 @@ module CLI
|
|
117
117
|
|
118
118
|
handlers = if block_given?
|
119
119
|
{ out_r => ->(data) { yield(data.force_encoding(Encoding::UTF_8), '') },
|
120
|
-
err_r => ->(data) { yield('', data.force_encoding(Encoding::UTF_8)) }
|
120
|
+
err_r => ->(data) { yield('', data.force_encoding(Encoding::UTF_8)) } }
|
121
121
|
else
|
122
122
|
{ out_r => ->(data) { STDOUT.write(data) },
|
123
|
-
err_r => ->(data) { STDOUT.write(data) }
|
123
|
+
err_r => ->(data) { STDOUT.write(data) } }
|
124
124
|
end
|
125
125
|
|
126
126
|
previous_trailing = Hash.new('')
|
data/lib/cli/kit/util.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
module CLI
|
2
|
+
module Kit
|
3
|
+
module Util
|
4
|
+
class << self
|
5
|
+
def snake_case(camel_case, seperator = "_")
|
6
|
+
camel_case.to_s # MyCoolThing::MyAPIModule
|
7
|
+
.gsub(/::/, '/') # MyCoolThing/MyAPIModule
|
8
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, "\\1#{seperator}\\2") # MyCoolThing::MyAPI_Module
|
9
|
+
.gsub(/([a-z\d])([A-Z])/, "\\1#{seperator}\\2") # My_Cool_Thing::My_API_Module
|
10
|
+
.downcase # my_cool_thing/my_api_module
|
11
|
+
end
|
12
|
+
|
13
|
+
def dash_case(camel_case)
|
14
|
+
snake_case(camel_case, '-')
|
15
|
+
end
|
16
|
+
|
17
|
+
# The following methods is taken from activesupport
|
18
|
+
# All credit for this method goes to the original authors.
|
19
|
+
# https://github.com/rails/rails/blob/d66e7835bea9505f7003e5038aa19b6ea95ceea1/activesupport/lib/active_support/core_ext/string/strip.rb
|
20
|
+
#
|
21
|
+
# Copyright (c) 2005-2018 David Heinemeier Hansson
|
22
|
+
#
|
23
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
24
|
+
# a copy of this software and associated documentation files (the
|
25
|
+
# "Software"), to deal in the Software without restriction, including
|
26
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
27
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
28
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
29
|
+
# the following conditions:
|
30
|
+
#
|
31
|
+
# The above copyright notice and this permission notice shall be
|
32
|
+
# included in all copies or substantial portions of the Software.
|
33
|
+
#
|
34
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
35
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
36
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
37
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
38
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
39
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
40
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
|
+
#
|
42
|
+
# Strips indentation by removing the amount of leading whitespace in the least indented
|
43
|
+
# non-empty line in the whole string
|
44
|
+
#
|
45
|
+
def strip_heredoc(str)
|
46
|
+
str.gsub(/^#{str.scan(/^[ \t]*(?=\S)/).min}/, "".freeze)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Joins an array with commas and "and", using the Oxford comma.
|
50
|
+
def english_join(array)
|
51
|
+
return "" if array.nil?
|
52
|
+
return array.join(" and ") if array.length < 3
|
53
|
+
|
54
|
+
"#{array[0..-2].join(', ')}, and #{array[-1]}"
|
55
|
+
end
|
56
|
+
|
57
|
+
# Execute a block within the context of a variable enviroment
|
58
|
+
#
|
59
|
+
def with_environment(environment, value)
|
60
|
+
return yield unless environment
|
61
|
+
|
62
|
+
old_env = ENV[environment]
|
63
|
+
begin
|
64
|
+
ENV[environment] = value
|
65
|
+
yield
|
66
|
+
ensure
|
67
|
+
old_env ? ENV[environment] = old_env : ENV.delete(environment)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Converts an integer representing bytes into a human readable format
|
72
|
+
#
|
73
|
+
def to_filesize(bytes, precision: 2, space: false)
|
74
|
+
to_si_scale(bytes, 'B', precision: precision, space: space, factor: 1024)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Converts a number to a human readable format on the SI scale
|
78
|
+
#
|
79
|
+
def to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false)
|
80
|
+
raise ArgumentError, "factor should only be 1000 or 1024" unless [1000, 1024].include?(factor)
|
81
|
+
|
82
|
+
small_scale = %w(m µ n p f a z y)
|
83
|
+
big_scale = %w(k M G T P E Z Y)
|
84
|
+
negative = number < 0
|
85
|
+
number = number.abs.to_f
|
86
|
+
|
87
|
+
if number == 0 || number.between?(1, factor)
|
88
|
+
prefix = ""
|
89
|
+
scale = 0
|
90
|
+
else
|
91
|
+
scale = Math.log(number, factor).floor
|
92
|
+
if number < 1
|
93
|
+
index = [-scale - 1, small_scale.length].min
|
94
|
+
scale = -(index + 1)
|
95
|
+
prefix = small_scale[index]
|
96
|
+
else
|
97
|
+
index = [scale - 1, big_scale.length].min
|
98
|
+
scale = index + 1
|
99
|
+
prefix = big_scale[index]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
divider = (factor**scale)
|
104
|
+
fnum = (number / divider).round(precision)
|
105
|
+
|
106
|
+
# Trim useless decimal
|
107
|
+
fnum = fnum.to_i if (fnum.to_i.to_f * divider) == number
|
108
|
+
|
109
|
+
fnum = -fnum if negative
|
110
|
+
prefix = " " + prefix if space
|
111
|
+
|
112
|
+
"#{fnum}#{prefix}#{unit}"
|
113
|
+
end
|
114
|
+
|
115
|
+
# Dir.chdir, when invoked in block form, complains when we call chdir
|
116
|
+
# again recursively. There's no apparent good reason for this, so we
|
117
|
+
# simply implement our own block form of Dir.chdir here.
|
118
|
+
def with_dir(dir)
|
119
|
+
prev = Dir.pwd
|
120
|
+
Dir.chdir(dir)
|
121
|
+
yield
|
122
|
+
ensure
|
123
|
+
Dir.chdir(prev)
|
124
|
+
end
|
125
|
+
|
126
|
+
def with_tmp_dir
|
127
|
+
require 'fileutils'
|
128
|
+
dir = Dir.mktmpdir
|
129
|
+
with_dir(dir) do
|
130
|
+
yield(dir)
|
131
|
+
end
|
132
|
+
ensure
|
133
|
+
FileUtils.remove_entry(dir)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Standard way of checking for CI / Tests
|
137
|
+
def testing?
|
138
|
+
ci? || ENV['TEST']
|
139
|
+
end
|
140
|
+
|
141
|
+
# Set only in IntegrationTest#session; indicates that the process was
|
142
|
+
# called by `session.execute` from an IntegrationTest subclass.
|
143
|
+
def integration_test_session?
|
144
|
+
ENV['INTEGRATION_TEST_SESSION']
|
145
|
+
end
|
146
|
+
|
147
|
+
# Standard way of checking for CI
|
148
|
+
def ci?
|
149
|
+
ENV['CI']
|
150
|
+
end
|
151
|
+
|
152
|
+
# Must call retry_after on the result in order to execute the block
|
153
|
+
#
|
154
|
+
# Example usage:
|
155
|
+
#
|
156
|
+
# CLI::Kit::Util.begin do
|
157
|
+
# might_raise_if_costly_prep_not_done()
|
158
|
+
# end.retry_after(ExpectedError) do
|
159
|
+
# costly_prep()
|
160
|
+
# end
|
161
|
+
def begin(&block_that_might_raise)
|
162
|
+
Retrier.new(block_that_might_raise)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class Retrier
|
167
|
+
def initialize(block_that_might_raise)
|
168
|
+
@block_that_might_raise = block_that_might_raise
|
169
|
+
end
|
170
|
+
|
171
|
+
def retry_after(exception = StandardError, retries: 1, &before_retry)
|
172
|
+
@block_that_might_raise.call
|
173
|
+
rescue exception => e
|
174
|
+
raise if (retries -= 1) < 0
|
175
|
+
if before_retry
|
176
|
+
if before_retry.arity == 0
|
177
|
+
yield
|
178
|
+
else
|
179
|
+
yield e
|
180
|
+
end
|
181
|
+
end
|
182
|
+
retry
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
private_constant :Retrier
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
data/lib/cli/kit/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cli-kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Burke Libbey
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-07-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: cli-ui
|
@@ -18,14 +18,14 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.1.
|
21
|
+
version: 1.1.4
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: 1.1.
|
28
|
+
version: 1.1.4
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: bundler
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,11 +123,13 @@ files:
|
|
123
123
|
- lib/cli/kit/executor.rb
|
124
124
|
- lib/cli/kit/ini.rb
|
125
125
|
- lib/cli/kit/levenshtein.rb
|
126
|
+
- lib/cli/kit/logger.rb
|
126
127
|
- lib/cli/kit/resolver.rb
|
127
128
|
- lib/cli/kit/ruby_backports/enumerable.rb
|
128
129
|
- lib/cli/kit/support.rb
|
129
130
|
- lib/cli/kit/support/test_helper.rb
|
130
131
|
- lib/cli/kit/system.rb
|
132
|
+
- lib/cli/kit/util.rb
|
131
133
|
- lib/cli/kit/version.rb
|
132
134
|
homepage: https://github.com/shopify/cli-kit
|
133
135
|
licenses:
|