eac_ruby_utils 0.2.0 → 0.3.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: 5039f5c4f347e71881361fb3bcc462ebd88ee5bcd0140a677be9583591a34d40
4
- data.tar.gz: 29e602fb349f046175a50b770433eccf92070aec71c8d7d8a3b41976254669d3
3
+ metadata.gz: c8b6e4d5bfab257e81419c888fb9463ec1e7766dde3cda6b9e36fa9fd741a35c
4
+ data.tar.gz: c5fdbab98b1e9b659c16ec8919995ba824380a140fee28697600ddee97a0cd4c
5
5
  SHA512:
6
- metadata.gz: 1661013b929f15161d7288505dd7afa52e4f29354a7788d4f1adff038e804bb11c4c6ef7f3b1cf10f959bdda299617247a88125c106b75501adc6edcbc5531f6
7
- data.tar.gz: 9fabf390c9845c25b059b4321858d17ab7ee57f36c6ccfd6b7be977c5dac1f97b20a82ad35e11b5e633e5e7c53a14a804f5833fe4f1d66590994c36e0c1ede7b
6
+ metadata.gz: 5c7279d045fdb2e6af2d3cc90f87605e562c9765a85c7b7988a57fd9da1aca598c14db699fc7af6d0256363352b0ffbebb60cd6ed9fbefe8c473941cee2004f4
7
+ data.tar.gz: 1e91ea3a626b6b0d2a439f30db473fef869a708d795e0a48341b2b7b66146813ae38530d8b029ecb6436f55c1c8927884ad8af0cc72bee3f34bcc4a9c42b139a
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Console
3
5
  class DocoptRunner
@@ -1,33 +1,35 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Console
3
5
  # https://github.com/fazibear/colorize
4
6
  module Speaker
5
- def puts(s = '')
6
- STDERR.puts(s)
7
+ def puts(string = '')
8
+ STDERR.puts(string)
7
9
  end
8
10
 
9
- def out(s = '')
10
- STDOUT.write(s)
11
+ def out(string = '')
12
+ STDOUT.write(string)
11
13
  end
12
14
 
13
- def fatal_error(s)
14
- puts "ERROR: #{s}".white.on_red
15
+ def fatal_error(string)
16
+ puts "ERROR: #{string}".white.on_red
15
17
  Kernel.exit 1
16
18
  end
17
19
 
18
- def title(s)
19
- puts(('-' * (8 + s.length)).green)
20
- puts("--- #{s} ---".green)
21
- puts(('-' * (8 + s.length)).green)
20
+ def title(string)
21
+ puts(('-' * (8 + string.length)).green)
22
+ puts("--- #{string} ---".green)
23
+ puts(('-' * (8 + string.length)).green)
22
24
  puts
23
25
  end
24
26
 
25
- def info(s)
26
- puts s.white
27
+ def info(string)
28
+ puts string.white
27
29
  end
28
30
 
29
- def warn(s)
30
- puts s.yellow
31
+ def warn(string)
32
+ puts string.yellow
31
33
  end
32
34
 
33
35
  def infov(*args)
@@ -42,8 +44,8 @@ module EacRubyUtils
42
44
  puts r.join(', ')
43
45
  end
44
46
 
45
- def success(s)
46
- puts s.green
47
+ def success(string)
48
+ puts string.green
47
49
  end
48
50
  end
49
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class BaseEnv
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class Command
@@ -36,6 +38,7 @@ module EacRubyUtils
36
38
  def execute!(options = {})
37
39
  er = ExecuteResult.new(execute(options), options)
38
40
  return er.result if er.success?
41
+
39
42
  raise "execute! command failed: #{self}\n#{er.r.pretty_inspect}"
40
43
  end
41
44
 
@@ -51,6 +54,7 @@ module EacRubyUtils
51
54
 
52
55
  def system!(options = {})
53
56
  return if system(options)
57
+
54
58
  raise "system! command failed: #{self}"
55
59
  end
56
60
 
@@ -66,30 +70,35 @@ module EacRubyUtils
66
70
  ENV['DEBUG'].to_s.strip != ''
67
71
  end
68
72
 
69
- def append_command_options(c, options)
70
- c = options[:input].command + ' | ' + c if options[:input]
71
- c = "cat #{Shellwords.escape(options[:input_file])} | #{c}" if options[:input_file]
72
- c += ' > ' + Shellwords.escape(options[:output_file]) if options[:output_file]
73
- c
73
+ def append_command_options(command, options)
74
+ command = options[:input].command + ' | ' + command if options[:input]
75
+ if options[:input_file]
76
+ command = "cat #{Shellwords.escape(options[:input_file])}" \
77
+ " | #{command}"
78
+ end
79
+ command += ' > ' + Shellwords.escape(options[:output_file]) if options[:output_file]
80
+ command
74
81
  end
75
82
 
76
- def escape(s)
77
- raise "#{s}|#{s.class}" unless s.is_a?(String)
78
- m = /^\@ESC_(.+)$/.match(s)
79
- m ? m[1] : Shellwords.escape(s)
83
+ def escape(arg)
84
+ raise "#{arg}|#{arg.class}" unless arg.is_a?(String)
85
+
86
+ m = /^\@ESC_(.+)$/.match(arg)
87
+ m ? m[1] : Shellwords.escape(arg)
80
88
  end
81
89
 
82
90
  class ExecuteResult
83
91
  attr_reader :r, :options
84
92
 
85
- def initialize(r, options)
86
- @r = r
93
+ def initialize(result, options)
94
+ @r = result
87
95
  @options = options
88
96
  end
89
97
 
90
98
  def result
91
99
  return exit_code_zero_result if exit_code_zero?
92
100
  return expected_error_result if expected_error?
101
+
93
102
  raise 'Failed!'
94
103
  end
95
104
 
@@ -100,7 +109,7 @@ module EacRubyUtils
100
109
  private
101
110
 
102
111
  def exit_code_zero?
103
- r[:exit_code] && r[:exit_code].zero?
112
+ r[:exit_code]&.zero?
104
113
  end
105
114
 
106
115
  def exit_code_zero_result
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class LocalEnv < ::EacRubyUtils::Envs::BaseEnv
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class Process
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class SshEnv < ::EacRubyUtils::Envs::BaseEnv
@@ -5,13 +7,14 @@ module EacRubyUtils
5
7
  def parse_uri(uri)
6
8
  r = parse_user_hostname(uri) || ::Addressable::URI.parse(uri)
7
9
  return r if r.scheme == 'ssh'
10
+
8
11
  raise "URI has no SSH scheme: #{uri}"
9
12
  end
10
13
 
11
14
  private
12
15
 
13
- def parse_user_hostname(s)
14
- m = /\A([^@]+)@([^@]+)\z/.match(s)
16
+ def parse_user_hostname(user_hostname)
17
+ m = /\A([^@]+)@([^@]+)\z/.match(user_hostname)
15
18
  m ? ::Addressable::URI.new(scheme: 'ssh', host: m[2], user: m[1]) : nil
16
19
  rescue Addressable::URI::InvalidURIError
17
20
  nil
@@ -44,7 +47,7 @@ module EacRubyUtils
44
47
 
45
48
  def ssh_command_line_options
46
49
  r = []
47
- uri.query_values.each { |k, v| r += ['-o', "#{k}=#{v}"] } if uri.query_values
50
+ uri.query_values&.each { |k, v| r += ['-o', "#{k}=#{v}"] }
48
51
  r
49
52
  end
50
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EacRubyUtils
2
4
  module Envs
3
5
  class << self
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class OptionsConsumer
5
+ def initialize(data)
6
+ @data = data.with_indifferent_access
7
+ end
8
+
9
+ def consume(key, default_value = nil, &block)
10
+ return default_value unless data.key?(key)
11
+
12
+ value = data.delete(key)
13
+ value = yield(value) if block
14
+ value
15
+ end
16
+
17
+ def validate
18
+ return if data.empty?
19
+
20
+ raise "Invalid keys: #{data.keys}"
21
+ end
22
+
23
+ def left_data
24
+ data.dup
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :data
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'
5
5
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module EacRubyUtils
6
+ class Yaml
7
+ class << self
8
+ def load_common(string)
9
+ ::YAML.safe_load(string, [::Symbol, ::Date])
10
+ end
11
+ end
12
+ end
13
+ end
@@ -7,6 +7,7 @@ require 'pp'
7
7
  require 'net/ssh'
8
8
 
9
9
  module EacRubyUtils
10
+ require 'eac_ruby_utils/options_consumer'
10
11
  require 'eac_ruby_utils/console/docopt_runner'
11
12
  require 'eac_ruby_utils/console/speaker'
12
13
  require 'eac_ruby_utils/envs'
@@ -15,4 +16,5 @@ module EacRubyUtils
15
16
  require 'eac_ruby_utils/envs/local_env'
16
17
  require 'eac_ruby_utils/envs/process'
17
18
  require 'eac_ruby_utils/envs/ssh_env'
19
+ require 'eac_ruby_utils/yaml'
18
20
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-05 00:00:00.000000000 Z
11
+ date: 2018-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: colorize
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +82,9 @@ files:
68
82
  - lib/eac_ruby_utils/envs/local_env.rb
69
83
  - lib/eac_ruby_utils/envs/process.rb
70
84
  - lib/eac_ruby_utils/envs/ssh_env.rb
85
+ - lib/eac_ruby_utils/options_consumer.rb
71
86
  - lib/eac_ruby_utils/version.rb
87
+ - lib/eac_ruby_utils/yaml.rb
72
88
  homepage:
73
89
  licenses:
74
90
  - MIT