ro_thor 0.7.3 → 4.4.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: badb9e4c63810fa7f088e670062b32985f26e553
4
- data.tar.gz: b41774fd34f8e974a7b8bcbf26978873d86e3c1b
3
+ metadata.gz: 3ff07a26a7e853179293ceb3325a07e92e0fbce2
4
+ data.tar.gz: 8e0b37e48c52c83305ff3e3aa9b12ca375a1d8c2
5
5
  SHA512:
6
- metadata.gz: 695cb0099bf5ed5c4d3f95e91ff9400e6fb4f6d09c13a53a2aa3b5afef9f32074fa3e63b0518f15560bf4f9a1f1cca033a11485af1888fc3be27cbff70794cf6
7
- data.tar.gz: 5875e679e27f078b8a1a3186f3bff52c81407dbdf4ff37e2cd5054e40d636061feb6a4f6925e05a00a23d4c1cbe36393e8aa91772ee987d2a5cc2e53a8c636a4
6
+ metadata.gz: 30851282f9fe334b90d877fad0e7f05b85541ae925c5b5f0dd50aef43ae50aacbc1c551cb2c6b9e06ae1a73b4785a33df1ee294a14e5128f768bd16d4e0e304a
7
+ data.tar.gz: 0574ec1ab0c75832c94901ff8cfdb8e4a1813f92de93c6ec1963aa5e232e4028b93c2dbb9906be891834053731cf6d79cc08b9ba5ebd454d21c933f1afc34760
data/lib/ro_thor/array.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require_relative 'base'
1
2
  class Array
2
3
  unless method_defined? :ro_opts
3
4
  def ro_opts(*args, &blk)
@@ -5,11 +6,17 @@ class Array
5
6
  opts = {}
6
7
 
7
8
  self.each do |e|
8
- e.split(" ").each do |_e|
9
- if _e.match(%r{([^:]+):([^:]+)})
10
- opts[:"#{$~[1]}"] = $~[2]
11
- else
12
- a << _e
9
+ if e.is_a?(String)
10
+ e.split(" ").each do |_e|
11
+ if _e.match(%r{([^:]+):([^:]+)})
12
+ opts[:"#{$~[1]}"] = $~[2]
13
+ else
14
+ a << _e
15
+ end
16
+ end
17
+ elsif e.is_a?(Hash)
18
+ e.each do |k, v|
19
+ opts[:"#{k}"] = v
13
20
  end
14
21
  end
15
22
  end
@@ -17,4 +24,8 @@ class Array
17
24
  return a, opts
18
25
  end
19
26
  end
27
+
28
+ def hits(*args, &blk)
29
+ self.flatten.compact.uniq
30
+ end
20
31
  end
@@ -0,0 +1,4 @@
1
+ require 'thor'
2
+ class RoThor < Thor
3
+
4
+ end
@@ -0,0 +1,44 @@
1
+ require_relative 'base'
2
+
3
+ class RoThor
4
+ module Helper
5
+ def to_clip(ctn, *args, &blk)
6
+ username = `whoami`.strip
7
+ system %Q(export DISPLAY=:0; export XAUTHORITY=/home/#{username}/.Xauthority;echo -n "#{ctn}"|xclip -sel clip)
8
+ end
9
+
10
+ def get_clip(*args, &blk)
11
+ shsu("xclip -sel clip -o")
12
+ end
13
+
14
+ def to_md5(s, *args, &blk)
15
+ Digest::MD5.hexdigest(s)
16
+ end
17
+
18
+ def in_pj(pj_name, *args, &blk)
19
+ old_dir = Dir.pwd
20
+ Dir.chdir RoFile.join(ENV['HOME'], "Dropbox/rbs/#{pj_name}")
21
+ yield
22
+ ensure
23
+ Dir.chdir old_dir
24
+ end
25
+
26
+ def mutex
27
+ @mutex ||= Mutex.new
28
+ end
29
+
30
+ def out(*args, &blk)
31
+ mutex.synchronize do
32
+ puts(*args, &blk)
33
+ end
34
+ end
35
+
36
+ def to_bool(r, *args, &blk)
37
+ if r
38
+ true
39
+ else
40
+ false
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'helper'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'nokogiri'
5
+ require 'cgi'
6
+ require 'uri'
7
+
8
+ class RoThor
9
+ class Http
10
+ include Helper
11
+
12
+ def h
13
+ @h ||= Faraday.new do |c|
14
+ c.use ::FaradayMiddleware::FollowRedirects, limit: 10
15
+ c.adapter :httpclient
16
+ end
17
+ end
18
+
19
+ def get(url, *args, &blk)
20
+ out "get #{url}"
21
+ h.get(url, *args, &blk)
22
+ end
23
+
24
+ def post(url, *args, &blk)
25
+ out "post #{url}"
26
+ h.post(url, *args, &blk)
27
+ end
28
+
29
+ def del(url, *args, &blk)
30
+ out "del #{url}"
31
+ h.delete(url)
32
+ end
33
+
34
+ def esc(smth, *args, &blk)
35
+ # CGI.escape(smth)
36
+ URI.escape(smth)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'base'
2
+
3
+ module RoErr
4
+ class StdErr < ::StandardError
5
+
6
+ end
7
+
8
+ class << self
9
+ def const_missing(cst_name)
10
+ k = Class.new(StdErr)
11
+ self.const_set(cst_name, k)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,94 @@
1
+ require_relative 'base'
2
+ require 'fileutils'
3
+ class RoThor
4
+ class RoFile
5
+ class << self
6
+ def method_missing(*args, &blk)
7
+ ::File.send(*args, &blk)
8
+ end
9
+
10
+ def f2f(from, to, *args, &blk)
11
+ p = parent(to)
12
+ msd(p)
13
+ binwrite(to, binread(from))
14
+ end
15
+
16
+ def abs(p, *args, &blk)
17
+ if p.match(%r{^~})
18
+ p.gsub(%r{^~}, ENV["HOME"])
19
+ else
20
+ File.absolute_path(p)
21
+ end
22
+ end
23
+
24
+ def mv(from, to, *args, &blk)
25
+ FileUtils.mv(from, to, force: true, &blk)
26
+ to
27
+ end
28
+
29
+ def ms(path, *args, &blk)
30
+ if !test(?f, path)
31
+ RoFile.write(path, "")
32
+ end
33
+ end
34
+
35
+ def rm(*args, &blk)
36
+ FileUtils.rm_rf(*args, &blk)
37
+ end
38
+
39
+ def parent(*args, &blk)
40
+ Pathname.new(*args, &blk).parent.to_s
41
+ end
42
+
43
+ def msp(f, *args, &blk)
44
+ p = parent(f)
45
+ FileUtils.mkpath(p)
46
+ end
47
+
48
+ def msd(p, *args, &blk)
49
+ FileUtils.mkpath(p)
50
+ end
51
+
52
+ def bn(*args, &blk)
53
+ basename(*args, &blk)
54
+ end
55
+
56
+ def fn(*args, &blk)
57
+ basename(*args, &blk).match(%r{([^\.]+)(.\w*$)?})[1]
58
+ end
59
+
60
+ def ext(*args, &blk)
61
+ basename(*args, &blk).match(%r{\.(\w+)$})[1]
62
+ end
63
+
64
+ def write(f, ctn)
65
+ p = parent(f)
66
+ msd(p)
67
+ ::File.write(f, ctn)
68
+ end
69
+
70
+ def exist?(path, *args, &blk)
71
+ ::File.exist?(path)
72
+ end
73
+
74
+ def rel(path, from_path, *args, &blk)
75
+ Pathname.new(path).relative_path_from(Pathname.new(from_path)).to_s
76
+ end
77
+
78
+ def cp(from, to_d, *args, &blk)
79
+ if file?(from) or directory?(from)
80
+ FileUtils.cp_r(from, to_d)
81
+ else
82
+ r = binread(from)
83
+ name = bn(from)
84
+ binwrite(join("#{to_d}", name), r)
85
+ end
86
+ RoFile.join to_d, basename(from)
87
+ end
88
+ end
89
+
90
+ def method_missing(*args, &blk)
91
+ ::File.new.send(*args, &blk)
92
+ end
93
+ end
94
+ end
data/lib/ro_thor/sh.rb CHANGED
@@ -1,18 +1,17 @@
1
- require 'thor'
1
+ require_relative 'base'
2
2
  require_relative 'array'
3
- class RoThor < Thor
3
+ class RoThor
4
4
  module Sh
5
5
  class Err < StandardError
6
6
  end
7
7
 
8
- def sh_shutup(*args, &blk)
9
- sh(*args, {shutup: true}, &blk)
10
- end
11
-
12
- def sh(*args, &blk)
8
+ def sh(cmd, *args, &blk)
13
9
  args, opts = args.ro_opts
14
- cmd = args.join(" ")
15
- puts "run '#{cmd}'"
10
+
11
+ unless opts[:shutup]
12
+ puts "run '#{cmd}' --- #{__FILE__}:#{__LINE__}"
13
+ end
14
+
16
15
  out, err, status = Open3.capture3(cmd)
17
16
 
18
17
  unless opts[:shutup]
@@ -20,16 +19,37 @@ class RoThor < Thor
20
19
  end
21
20
 
22
21
  unless status.success?
23
- raise Err, err
22
+ raise Err, "err cmd: #{cmd}\n#{out}\n#{err}\nerr cmd: #{cmd}"
24
23
  end
25
24
 
26
25
  out
27
26
  end
28
27
 
28
+ def shsu(*args, &blk)
29
+ sh(*args, {shutup: true}, &blk)
30
+ end
31
+
29
32
  def sys(*args, &blk)
30
- cmd = args.join(' ')
31
- puts cmd
32
- system cmd
33
+ cmd = args.join(" ")
34
+ puts "#{cmd}"
35
+ if defined? Pj and Pj.spork?
36
+ ::Sh.sh cmd
37
+ else
38
+ system cmd
39
+ if !$?.success?
40
+ raise ::RoThor::Sh::Err, "exit status: #{$?.exitstatus}"
41
+ end
42
+ end
43
+ end
44
+
45
+ # @deprecated run bash will lost current bash session cli history, if use `cd /path/to/dir; bash`, it cannot run after code; if use `cd /path/to/dir; bash -c "after code", it cannot change dir
46
+ # like in terminal type "cd /path/to/working_dir"
47
+ def terminal_cd(working_dir, after_bash=nil, *args, &blk)
48
+ # sys(%Q(cd #{working_dir}; bash -c "#{after_bash}"))
49
+ end
50
+
51
+ def notify(msg, *args, &blk)
52
+ sys "notify-send \"#{msg}\""
33
53
  end
34
54
 
35
55
  def exist_p?(pid)
@@ -0,0 +1,120 @@
1
+ require_relative 'base'
2
+ require_relative 'ro_err'
3
+ require 'net/ssh'
4
+ require_relative 'helper'
5
+ require_relative 'sh'
6
+ require_relative 'ro_file'
7
+
8
+ class RoThor
9
+ class SSH
10
+ include ::RoThor::Helper
11
+ include ::RoThor::Sh
12
+
13
+ attr_accessor :user_at_host, :se
14
+
15
+ def initialize(user_at_host, &blk)
16
+ @user_at_host = user_at_host
17
+ m = user_at_host.match(%r{(.*)@(.*)})
18
+ @se = ::Net::SSH.start(m[2], m[1])
19
+ end
20
+
21
+ def down(from, to, *args, &blk)
22
+ RoFile.msp(to)
23
+ sys "scp #{user_at_host}:#{from} #{to}"
24
+ end
25
+
26
+ def up(from, to, *args, &blk)
27
+ sys "scp #{from} #{user_at_host}:#{to}"
28
+ end
29
+
30
+ def sh(*args, &blk)
31
+ args_clone, opts = args.ro_opts
32
+
33
+ cmd = args.join(" ")
34
+ msg = opts[:status] || cmd
35
+
36
+ if msg.length > 100
37
+ msg = "\n#{msg}"
38
+ end
39
+
40
+ out("run '#{msg.strip}' on #{user_at_host}")
41
+ out_a = [] # ch.on_data only receive limited size data, so I must collect out multi times
42
+ clr = caller.clone
43
+ stderr_lines = []
44
+
45
+ ch = se.open_channel do |ch|
46
+ ch.exec("bash -ci '#{cmd}'") do |ch, success|
47
+ unless success
48
+ raise ::RoErr::RmtErr, "could not exe cmd: #{command.inspect}"
49
+ end
50
+
51
+ ch.on_data do |ch, data|
52
+ out data
53
+ out_a << data
54
+ end
55
+
56
+ ch.on_extended_data do |ch, type, data|
57
+ # type == 1 means it's stderr
58
+
59
+ if type == 1
60
+ unless data.match(%r{cannot set terminal process group \((-)?\d+\): Inappropriate ioctl for device})\
61
+ or data.match(%r{no job control in this shell})
62
+ # or data.match(%Q(Cookie#domain returns dot-less domain name now. Use Cookie#dot_domain if you need "." at the beginning.))
63
+ if data.len > 100
64
+ err_msg = "\nerr_msg:\n#{data}"
65
+ else
66
+ err_msg = "err_msg:#{data}"
67
+ end
68
+
69
+ stderr_lines << data
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ ch.wait
77
+ ch.close
78
+
79
+ stderr_lines = stderr_lines.hits
80
+ stderr_lines.delete_if do |l|
81
+ l.match(%r{^/.*warning\:.*$})
82
+ end
83
+
84
+ unless stderr_lines.empty?
85
+ if stderr_lines.any? do |l|
86
+ l.match(%r{\tfrom})
87
+ end
88
+ bt = []
89
+ err_msg_arr = []
90
+ stderr_lines.each do |l|
91
+ if l.match(%r{\tfrom})
92
+ l.split("\n").each do |_l|
93
+ bt << _l.gsub(%r{^\tfrom\s+}, "")
94
+ end
95
+ else
96
+ err_msg_arr << l
97
+ end
98
+ end
99
+
100
+ e = RoErr::RmtErr.new(uncol(err_msg_arr.join), backtrace: bt.map(&:strip).hits)
101
+ else
102
+ e = RoErr::RmtErr.new(uncol(stderr_lines.join("\n")), backtrace: clr)
103
+ end
104
+
105
+ if ::RoCell::Rmt.ignore_err
106
+ out_ins.warn e
107
+ else
108
+ raise e
109
+ end
110
+ end
111
+
112
+ out_a.join('')
113
+ end
114
+
115
+ def uncol(*args, &blk)
116
+ Term::ANSIColor.uncolor(*args, &blk)
117
+ end
118
+
119
+ end
120
+ end
data/lib/ro_thor.rb CHANGED
@@ -1,12 +1,37 @@
1
1
  require 'pathname'
2
2
  $LOAD_PATH.unshift(Pathname.new(__FILE__).parent.to_s)
3
- require 'ro_thor/array'
4
3
  require 'thor'
5
4
  require 'open3'
5
+ require 'ro_thor/base'
6
6
  require 'ro_thor/sh'
7
+ require 'ro_thor/ro_file'
8
+ require 'ro_thor/array'
9
+ require 'ro_thor/helper'
10
+ require 'ro_thor/ro_err'
11
+ require 'ro_thor/http'
12
+ require 'ro_thor/ssh'
13
+
14
+ # @example
15
+ #
16
+ # require 'ro_thor'
17
+ # module RoScript
18
+ # class C < RoThor
19
+ # def initialize(*args, &blk)
20
+ # super
21
+ # # do smth
22
+ # end
23
+ #
24
+ # usg :meth
25
+ #
26
+ # def meth(*args, &blk)
27
+ # # do smth
28
+ # end
29
+ # end
30
+ # end
7
31
 
8
32
  class RoThor < Thor
9
33
  include ::RoThor::Sh
34
+ include ::RoThor::Helper
10
35
 
11
36
  class << self
12
37
  def usg(str, *args, &blk)
@@ -21,20 +46,41 @@ class RoThor < Thor
21
46
  end
22
47
 
23
48
  def st(*args, &blk)
24
- start(*args, &blk)
49
+ start(args)
25
50
  end
26
- end
27
51
 
28
- def sys(*args, &blk)
29
- cmd = args.join(" ")
30
- puts "run '#{cmd}'"
31
- if spork?
32
- sh cmd
33
- else
34
- system cmd
52
+ def lamb(*args, &blk)
53
+ lambda(*args, &blk)
35
54
  end
36
55
  end
37
56
 
57
+ def open_url(url, user_data_dir=nil, *args, &blk)
58
+ user_data_dir ||= "#{ENV['HOME']}/.config/google-chrome"
59
+ sys %Q(google-chrome --user-data-dir=#{user_data_dir.strip} "#{url}")
60
+ end
61
+
62
+ def cur_pj_name(*args, &blk)
63
+ RoFile.basename(Dir.pwd)
64
+ end
65
+
66
+ def cur_pj_name2(*args, &blk)
67
+ cur_pj_name.gsub(%r{^ro_}, "")
68
+ end
69
+
70
+ def lamb(*args, &blk)
71
+ lambda(*args, &blk)
72
+ end
73
+
74
+ def out(*args, &blk)
75
+ puts(*args, &blk)
76
+ end
77
+
78
+
79
+
80
+ def down_d(*args, &blk)
81
+ RoFile.join("/media/roroco/disk750/Downloads", *args, &blk)
82
+ end
83
+
38
84
  def spork?(*args, &blk)
39
85
  defined? ::Spork and Spork.using_spork?
40
86
  end
@@ -88,5 +134,19 @@ class RoThor < Thor
88
134
  end
89
135
  end
90
136
 
137
+ def toggle_win(win_name)
138
+ wins = begin
139
+ shsu("xdotool search --onlyvisible #{win_name}").split("\n")
140
+ rescue RoThor::Sh::Err => e
141
+ out(e)
142
+ out(e.backtrace.join("\n"))
143
+ sleep(0.5)
144
+ retry
145
+ end
146
+ if wins.size > 0
147
+ sh "xdotool windowactivate #{wins.first.strip}"
148
+ end
149
+ end
150
+
91
151
  end
92
152
 
File without changes
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+
3
+ describe 'RoCmd::Thor::Rmt.new' do
4
+ let(:o) do
5
+ RoCmd::Thor::Rmt.new
6
+ end
7
+
8
+ WhenIvk.single_example do
9
+ it :flow do
10
+ r = o.flow
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'ro_thor'
2
+
3
+ describe 'RoThor' do
4
+ it 'try ro_thor/ro_err' do
5
+ expect do
6
+ raise ::RoErr::Nil
7
+ end.to raise_error(::RoErr::Nil)
8
+ end
9
+
10
+ end
11
+
@@ -0,0 +1,9 @@
1
+ require 'rails_helper'
2
+ require 'ro_thor/ssh'
3
+
4
+ describe 'RoThor::Rmt.new' do
5
+ it :sh do
6
+ s = RoThor::SSH.new('root@me')
7
+ s.sh('ls;ls')
8
+ end
9
+ end
data/spec/mysql/my.cnf CHANGED
File without changes
@@ -0,0 +1 @@
1
+ require_relative 'spec_helper'
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,17 @@
1
1
  require File.expand_path('../../conf/ro_thor', __FILE__)
2
2
 
3
- require 'ro_thor'
3
+ Spork.prefork do
4
+ require File.expand_path("../../conf/ro_thor", __FILE__)
5
+ require 'pj'
6
+ Pj.set_test
7
+
8
+ require 'ro_rspec_helpers_plus'
9
+
10
+ RoRspec.spec = __dir__
11
+
12
+ class RoThor
13
+ extend PjHelper
14
+ autoload_or_require_all_ro_dirs(rt)
15
+ end
16
+ end
17
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ro_thor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 4.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-21 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -33,9 +33,19 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/ro_thor.rb
35
35
  - lib/ro_thor/array.rb
36
+ - lib/ro_thor/base.rb
37
+ - lib/ro_thor/helper.rb
38
+ - lib/ro_thor/http.rb
39
+ - lib/ro_thor/ro_err.rb
40
+ - lib/ro_thor/ro_file.rb
36
41
  - lib/ro_thor/sh.rb
42
+ - lib/ro_thor/ssh.rb
37
43
  - spec/lib/core_ext/array_spec.rb
44
+ - spec/lib/ro_cmd/thor/dp_spec.rb
45
+ - spec/lib/ro_thor/ro_err_spec.rb
46
+ - spec/lib/ro_thor/ssh_spec.rb
38
47
  - spec/mysql/my.cnf
48
+ - spec/rails_helper.rb
39
49
  - spec/spec_helper.rb
40
50
  homepage: ''
41
51
  licenses: []
@@ -56,11 +66,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
66
  version: '0'
57
67
  requirements: []
58
68
  rubyforge_project:
59
- rubygems_version: 2.4.5
69
+ rubygems_version: 2.5.2
60
70
  signing_key:
61
71
  specification_version: 4
62
72
  summary: ''
63
73
  test_files:
64
- - spec/lib/core_ext/array_spec.rb
65
74
  - spec/spec_helper.rb
75
+ - spec/lib/ro_cmd/thor/dp_spec.rb
76
+ - spec/lib/core_ext/array_spec.rb
77
+ - spec/lib/ro_thor/ssh_spec.rb
78
+ - spec/lib/ro_thor/ro_err_spec.rb
79
+ - spec/rails_helper.rb
66
80
  - spec/mysql/my.cnf