thrush 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +20 -0
  3. data/README.md +20 -0
  4. data/lib/thrush.rb +212 -0
  5. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWViNzg4YWFlOGEwYzUwZGU4YzY5YzAxZmU2ZGJhYmY2ZDQ5ZjgwMA==
5
+ data.tar.gz: !binary |-
6
+ ZjhjNTE2ZTJkMzZhZjU1NzgyZTc2Mjg5NzVjMDZiZDVlODcyMDA1MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YmNjMTBhODJlZWNjZDVlZGRkODQ1OGQyMjMyNzRlY2Y4OGQyNzM4MGU5Mjk2
10
+ ZGUzYjIyNGEwNTBmZWQ4ZTAzMTMwYWRhODJjYWU3YTU2MGJiOTI2NDRiNWVh
11
+ ZTg0NDgzN2Q4Mzc5MGE3MmI1MGYwZmM5NGNkYjAxYzcxYjQ5NmM=
12
+ data.tar.gz: !binary |-
13
+ MzUyMmI5ZmIyZjU5Mjg3ZTA3YzM0MzYyZWY1NjI1ZThkMzhkYWM4N2VjMjk0
14
+ ZWIxYzU5ZjdkOGQ3MzYyNjU4MGM0NTU3MzhkYjIwNWM3NjAzMzQxODc0ZDQ1
15
+ Njc0ZWI2ZDk3MGFlZDAzMDhhY2UwZTBiODA0ZDZmZjc4OWE2YzE=
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Z. D. Peacock
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ Thrush
2
+ ======
3
+
4
+ Ruby shell scripting DSL
5
+
6
+ Installation
7
+ ------------
8
+
9
+ ### Gem
10
+ 1. Run the following: `gem install thrush`
11
+ 1. Add `require 'thrush'` to the top of your Ruby script.
12
+
13
+ ### Bundler
14
+ 1. Install [bundler](http://bundler.io): `gem install bundler`
15
+ 1. Add the following to your __Gemfile__:
16
+
17
+ source 'https://rubygems.org'
18
+ gem 'thrush'
19
+
20
+ 1. Add `require 'thrush'` to the top of your Ruby script.
data/lib/thrush.rb ADDED
@@ -0,0 +1,212 @@
1
+ require 'docile'
2
+ require 'colored'
3
+ require 'open4'
4
+
5
+ # Building environment variables
6
+ @env = {__global__: {}, default: {}}
7
+ @flags = {}
8
+ @current_env = :default
9
+
10
+ def flags(args)
11
+ param = nil
12
+ args.each do |arg|
13
+ if arg[0..1] == '--'
14
+ param = arg[2..-1].to_sym
15
+ @flags[param] = true unless @flags.has_key? param
16
+ else
17
+ case param
18
+ when :debug
19
+ @flags[param] = true
20
+ when :env
21
+ @flags[param] = arg.to_sym
22
+ @current_env = arg.to_sym
23
+ else
24
+ @flags[param] = arg
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ class ThrushItemBuilder
31
+ def initialize
32
+ @params = {}
33
+ end
34
+
35
+ def method_missing(m, *args)
36
+ @params[m.to_sym] = args[0]
37
+ end
38
+
39
+ def build
40
+ @params
41
+ end
42
+ end
43
+
44
+ def config(env, &block)
45
+ ev = Docile.dsl_eval(ThrushItemBuilder.new, &block).build
46
+ @current_env = env.to_sym if @current_env == :default && ev.has_key?(:default) && ev[:default] == true
47
+ @env[env] = ev
48
+ end
49
+
50
+ def global(&block)
51
+ config(:__global__, &block)
52
+ end
53
+
54
+ def env(key)
55
+ return @env[@current_env][key] if @env[@current_env].has_key? key
56
+ return @env[:__global__][key] if @env[:__global__].has_key? key
57
+ nil
58
+ end
59
+
60
+ def cmd_vals(key, cmdval)
61
+ return cmdval[key] if cmdval.has_key? key
62
+ env(key)
63
+ end
64
+
65
+ def env?(env)
66
+ @current_env == env
67
+ end
68
+
69
+ def flag?(key)
70
+ k = key.to_sym
71
+ @flags.has_key?(k) && @flags[k]
72
+ end
73
+
74
+ def debug?
75
+ flag? :debug
76
+ end
77
+
78
+ # Defining commands
79
+ @commands = []
80
+
81
+ #TODO: Fix the local, ssh functions so that they will work even if they don't have a block passed in
82
+ def local(command, &block)
83
+ lc = ThrushItemBuilder.new
84
+ lc.command command
85
+ lc.type :local
86
+ @commands << Docile.dsl_eval(lc, &block).build
87
+ end
88
+
89
+ def rsync(&block)
90
+ rc = ThrushItemBuilder.new
91
+ rc.type :rsync
92
+ @commands << Docile.dsl_eval(rc, &block).build
93
+ end
94
+
95
+ def ssh(command, &block)
96
+ sc = ThrushItemBuilder.new
97
+ sc.command command
98
+ sc.type :ssh
99
+ @commands << Docile.dsl_eval(sc, &block).build
100
+ end
101
+
102
+ def header(h)
103
+ len = h.gsub(/\e\[(\d+)m/, '').length
104
+ l = '-' * len
105
+ "\n#{ h }\n#{ l }\n"
106
+ end
107
+
108
+ def run!
109
+ if @flags.has_key?(:help)
110
+ puts header("Thrush client help for (ENV = #{ @current_env.upcase })").blue.bold
111
+ puts "#{ env(:help) }\n"
112
+ exit
113
+ end
114
+
115
+ puts header("Thrush client for (ENV = #{ @current_env.upcase })").blue.bold
116
+ puts "** DEBUG MODE **".yellow if debug?
117
+ i = 1
118
+
119
+ @commands.each do |cmd_values|
120
+ if cmd_values.has_key?(:exc)
121
+ found = false
122
+ cmd_values[:exc].each { |flag| found = true if flag? flag }
123
+ next if found
124
+ end
125
+
126
+ if cmd_values.has_key?(:inc)
127
+ found = false
128
+ cmd_values[:inc].each { |flag| found = true if flag? flag }
129
+ next unless found
130
+ end
131
+
132
+ sudo = cmd_vals(:sudo, cmd_values)
133
+ debug_command = ''
134
+
135
+ case cmd_values[:type]
136
+ when :local
137
+ cmd = sudo ? %w(sudo) : []
138
+ cmd << cmd_vals(:command, cmd_values)
139
+
140
+ command = cmd.join(' ')
141
+
142
+ if debug?
143
+ debug_command = command
144
+ command = ''
145
+ end
146
+ when :rsync
147
+ debug = debug? ? 'n' : ''
148
+ cmd = ["rsync -#{ debug }vazcO --no-o --no-g -e ssh --exclude '.git' --exclude '.idea' --exclude '.DS_Store'"]
149
+
150
+ ignore = cmd_vals(:ignore, cmd_values)
151
+ ignore.each { |exclude| cmd << "--exclude '#{ exclude }'" } unless ignore.nil? || ignore.empty?
152
+
153
+ cmd << "--rsync-path='sudo rsync'" if sudo
154
+
155
+ path = cmd_vals(:path, cmd_values)
156
+ cmd << "--rsync-path='#{ path }'" unless path.nil? || path.empty?
157
+
158
+ cmd << cmd_vals(:src, cmd_values)
159
+ cmd << "#{ cmd_vals(:hostname, cmd_values) }:#{ cmd_vals(:dest, cmd_values) }"
160
+
161
+ command = cmd.join(' ')
162
+ debug_command = command if debug?
163
+ when :ssh
164
+ cmd = ["ssh #{ cmd_vals(:hostname, cmd_values) }"]
165
+ cmd << 'sudo' if sudo
166
+ cmd << "#{ cmd_vals(:command, cmd_values) }"
167
+
168
+ command = cmd.join(' ')
169
+ if debug?
170
+ debug_command = command
171
+ command = ''
172
+ end
173
+ else
174
+ command = ''
175
+ debug_command = ''
176
+ end
177
+
178
+ puts "\n#{ i }. #{ cmd_values[:step] }".green if cmd_values[:step]
179
+ i += 1
180
+
181
+ unless debug_command.empty?
182
+ puts "Command to run: #{ debug_command }"
183
+ end
184
+
185
+ unless command.empty?
186
+ #This code is simpler than requiring open4, but the output is delayed
187
+ #puts `#{ command }`
188
+ #unless $?.to_i == 0
189
+ # puts "Command failed: #{ command }"
190
+ # break
191
+ #end
192
+
193
+ status = Open4::popen4('sh') do |pid, stdin, stdout, stderr|
194
+ stdin.puts command
195
+ stdin.close
196
+
197
+ while (line = stdout.gets)
198
+ puts line
199
+ end
200
+
201
+ while (line = stderr.gets)
202
+ puts line.red
203
+ end
204
+ end
205
+
206
+ unless status == 0
207
+ puts "Command failed: #{ command }".red
208
+ break
209
+ end
210
+ end
211
+ end
212
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrush
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Z.d. Peacock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: docile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colored
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: open4
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ description: A DSL for building shell scripts
56
+ email: zdp@thoomtech.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - LICENSE
63
+ - lib/thrush.rb
64
+ homepage: http://github.com/thoom/thrush
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.1.10
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: ! 'Thrush: Shell DSL'
88
+ test_files: []