eac_ruby_utils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5fa1cf02f09675fc58d1578a7a65385540c409f
4
+ data.tar.gz: 3ae2e091eafcfadab182d82101fcabccd5d57ff6
5
+ SHA512:
6
+ metadata.gz: 28c5eb7ca9b13a7a94b8a3bf1be07d69f9e53f0aeb7eccbcae1a697b60051c38e4c26dc6371aa87ac7c39167151e992168c8e2fe7e61e93867d792a29fa78968
7
+ data.tar.gz: 72fea88ff5a5b6c28318d5ea0399172001f4c932081f6d324e53089430012c800720afcf9b02625a5b6b41710092d1e20bdc0d03751e79fe451b2523406240fb
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = E.A.C. Ruby Utilities
2
+
3
+ Utilities for E.A.C.'s Ruby projects.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+ require 'docopt'
5
+ require 'open3'
6
+ require 'pp'
7
+
8
+ module EacRubyUtils
9
+ require 'eac_ruby_utils/console/docopt_runner'
10
+ require 'eac_ruby_utils/console/speaker'
11
+ require 'eac_ruby_utils/envs'
12
+ require 'eac_ruby_utils/envs/base_env'
13
+ require 'eac_ruby_utils/envs/command'
14
+ require 'eac_ruby_utils/envs/local_env'
15
+ require 'eac_ruby_utils/envs/process'
16
+ require 'eac_ruby_utils/envs/ssh_env'
17
+ end
@@ -0,0 +1,24 @@
1
+ module EacRubyUtils
2
+ module Console
3
+ class DocoptRunner
4
+ attr_reader :options
5
+
6
+ def initialize
7
+ @options = Docopt.docopt(doc)
8
+ run
9
+ rescue Docopt::Exit => e
10
+ puts e.message
11
+ end
12
+
13
+ private
14
+
15
+ def doc
16
+ self.class.const_get('DOC').gsub('__PROGRAM__', program)
17
+ end
18
+
19
+ def program
20
+ ENV['MYSELF_PROGRAM'] || $PROGRAM_NAME
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ module EacRubyUtils
2
+ module Console
3
+ # https://github.com/fazibear/colorize
4
+ module Speaker
5
+ def puts(s = '')
6
+ STDERR.puts(s)
7
+ end
8
+
9
+ def out(s = '')
10
+ STDOUT.write(s)
11
+ end
12
+
13
+ def fatal_error(s)
14
+ puts "ERROR: #{s}".white.on_red
15
+ Kernel.exit 1
16
+ end
17
+
18
+ def title(s)
19
+ puts(('-' * (8 + s.length)).green)
20
+ puts("--- #{s} ---".green)
21
+ puts(('-' * (8 + s.length)).green)
22
+ puts
23
+ end
24
+
25
+ def info(s)
26
+ puts s.white
27
+ end
28
+
29
+ def warn(s)
30
+ puts s.yellow
31
+ end
32
+
33
+ def infov(*args)
34
+ r = []
35
+ args.each_with_index do |v, i|
36
+ if i.even?
37
+ r << "#{v}: ".cyan
38
+ else
39
+ r.last << v.to_s
40
+ end
41
+ end
42
+ puts r.join(', ')
43
+ end
44
+
45
+ def success(s)
46
+ puts s.green
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module EacRubyUtils
2
+ module Envs
3
+ class << self
4
+ def local
5
+ @local ||= ::EacRubyUtils::Envs::LocalEnv.new
6
+ end
7
+
8
+ def ssh(user_hostname)
9
+ ::EacRubyUtils::Envs::SshEnv.new(user_hostname)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module EacRubyUtils
2
+ module Envs
3
+ class BaseEnv
4
+ def command(*args)
5
+ ::EacRubyUtils::Envs::Command.new(self, args)
6
+ end
7
+
8
+ def file_exist?(file)
9
+ command(['stat', file]).execute[:exit_code].zero?
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,120 @@
1
+ module EacRubyUtils
2
+ module Envs
3
+ class Command
4
+ include EacRubyUtils::Console::Speaker
5
+
6
+ def initialize(env, command)
7
+ @env = env
8
+ if command.count == 1 && command.first.is_a?(Array)
9
+ @command = command.first
10
+ elsif command.is_a?(Array)
11
+ @command = command
12
+ else
13
+ raise "Invalid argument command: #{command}|#{command.class}"
14
+ end
15
+ end
16
+
17
+ def append(args)
18
+ self.class.new(@env, @command + args)
19
+ end
20
+
21
+ def prepend(args)
22
+ self.class.new(@env, args + @command)
23
+ end
24
+
25
+ def to_s
26
+ "#{@command} [ENV: #{@env}]"
27
+ end
28
+
29
+ def command(options = {})
30
+ c = @command
31
+ c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable)
32
+ c = @env.command_line(c)
33
+ append_command_options(c, options)
34
+ end
35
+
36
+ def execute!(options = {})
37
+ er = ExecuteResult.new(execute(options), options)
38
+ return er.result if er.success?
39
+ raise "execute! command failed: #{self}\n#{er.r.pretty_inspect}"
40
+ end
41
+
42
+ def execute(options = {})
43
+ c = command(options)
44
+ puts "BEFORE: #{c}".light_red if debug?
45
+ t1 = Time.now
46
+ r = ::EacRubyUtils::Envs::Process.new(c).to_h
47
+ i = Time.now - t1
48
+ puts "AFTER [#{i}]: #{c}".light_red if debug?
49
+ r
50
+ end
51
+
52
+ def system!(options = {})
53
+ return if system(options)
54
+ raise "system! command failed: #{self}"
55
+ end
56
+
57
+ def system(options = {})
58
+ c = command(options)
59
+ puts c.light_red if debug?
60
+ Kernel.system(c)
61
+ end
62
+
63
+ private
64
+
65
+ def debug?
66
+ ENV['DEBUG'].to_s.strip != ''
67
+ end
68
+
69
+ def append_command_options(c, options)
70
+ c = options[:input].command + ' | ' + c if options[:input]
71
+ c += ' < ' + Shellwords.escape(options[:input_file]) if options[:input_file]
72
+ c += ' > ' + Shellwords.escape(options[:output_file]) if options[:output_file]
73
+ c
74
+ end
75
+
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)
80
+ end
81
+
82
+ class ExecuteResult
83
+ attr_reader :r, :options
84
+
85
+ def initialize(r, options)
86
+ @r = r
87
+ @options = options
88
+ end
89
+
90
+ def result
91
+ return exit_code_zero_result if exit_code_zero?
92
+ return expected_error_result if expected_error?
93
+ raise 'Failed!'
94
+ end
95
+
96
+ def success?
97
+ exit_code_zero? || expected_error?
98
+ end
99
+
100
+ private
101
+
102
+ def exit_code_zero?
103
+ r[:exit_code] && r[:exit_code].zero?
104
+ end
105
+
106
+ def exit_code_zero_result
107
+ r[options[:output] || :stdout]
108
+ end
109
+
110
+ def expected_error_result
111
+ options[:exit_outputs][r[:exit_code]]
112
+ end
113
+
114
+ def expected_error?
115
+ options[:exit_outputs].is_a?(Hash) && options[:exit_outputs].key?(r[:exit_code])
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,13 @@
1
+ module EacRubyUtils
2
+ module Envs
3
+ class LocalEnv < ::EacRubyUtils::Envs::BaseEnv
4
+ def to_s
5
+ 'LOCAL'
6
+ end
7
+
8
+ def command_line(line)
9
+ line
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module EacRubyUtils
2
+ module Envs
3
+ class Process
4
+ def initialize(command)
5
+ @command = command
6
+ Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
7
+ @exit_code = wait_thr.value.to_i
8
+ @stdout = stdout.read
9
+ @stderr = stderr.read
10
+ end
11
+ end
12
+
13
+ def to_h
14
+ { exit_code: @exit_code, stdout: @stdout, stderr: @stderr, command: @command }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Envs
2
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
3
+ def initialize(user_hostname)
4
+ @user_hostname = user_hostname
5
+ end
6
+
7
+ def to_s
8
+ "SSH(#{@user_hostname})"
9
+ end
10
+
11
+ def command_line(line)
12
+ "ssh #{Shellwords.escape(@user_hostname)} #{Shellwords.escape(line)}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ VERSION = '0.1.0'.freeze
5
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eac_ruby_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Esquilo Azul Company
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.1
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.rdoc
48
+ - lib/eac_ruby_utils.rb
49
+ - lib/eac_ruby_utils/console/docopt_runner.rb
50
+ - lib/eac_ruby_utils/console/speaker.rb
51
+ - lib/eac_ruby_utils/envs.rb
52
+ - lib/eac_ruby_utils/envs/base_env.rb
53
+ - lib/eac_ruby_utils/envs/command.rb
54
+ - lib/eac_ruby_utils/envs/local_env.rb
55
+ - lib/eac_ruby_utils/envs/process.rb
56
+ - lib/eac_ruby_utils/envs/ssh_env.rb
57
+ - lib/eac_ruby_utils/version.rb
58
+ homepage:
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.14
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Utilities for E.A.C.'s Ruby projects.
82
+ test_files: []
83
+ has_rdoc: