ruby-unix-now 0.0.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.
- data/README.md +69 -0
- data/lib/ruby-unix-now.rb +57 -0
- metadata +56 -0
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# run | Ruby Unix Now
|
2
|
+
A tiny library which attempts to bridge the gap between ruby code and executing system commands. No more more string concatenation to build up commands.
|
3
|
+
|
4
|
+
The simplest way to execute a command in ruby is to embed your command in backticks.
|
5
|
+
|
6
|
+
`whoami` #=> john
|
7
|
+
|
8
|
+
So far, so good. Using run, the equivalent command would be
|
9
|
+
run :whoami
|
10
|
+
|
11
|
+
Not much difference. However, as soon as we start to introduce arguments, conditional logic and so on, string concatenation and backticks starts to get ugly.
|
12
|
+
|
13
|
+
command = "rsync -r, --progress"
|
14
|
+
command << " -z -h " if condition_x
|
15
|
+
command << " --from #{from_path} --to #{to_path}"
|
16
|
+
`#{command}`
|
17
|
+
|
18
|
+
Run tries to solve these problems my making it easy to
|
19
|
+
|
20
|
+
1. Embed variables
|
21
|
+
2. Add optional arguments
|
22
|
+
3. Lay out complicated commands
|
23
|
+
|
24
|
+
It does this just by assigning meaning to the standard Ruby structures.
|
25
|
+
|
26
|
+
- Hashes are options
|
27
|
+
- Symbols are either commands or flags
|
28
|
+
- Strings are values
|
29
|
+
- Arrays are lists of options
|
30
|
+
|
31
|
+
The equivalent run command to the one above would be
|
32
|
+
|
33
|
+
options = {}
|
34
|
+
options.merge!(:z, :h) if condition_x_
|
35
|
+
run :rsync, :r, :progress, options, --from from_path, --to to_path
|
36
|
+
|
37
|
+
Nore more embedding variables in string, no more arbitrary spaces to make
|
38
|
+
strings glue together correctly.
|
39
|
+
|
40
|
+
Another example taken from a real life script
|
41
|
+
|
42
|
+
Normal commands
|
43
|
+
|
44
|
+
`mkdir -p #{xapian_path}`
|
45
|
+
`rm -r -f #{to`
|
46
|
+
`cp -r, #{from} #{to}`
|
47
|
+
|
48
|
+
With Run
|
49
|
+
|
50
|
+
run :mkdir, :p, xapian_path
|
51
|
+
run :rm, :r, :f, to
|
52
|
+
run :cp, :r, from, to
|
53
|
+
|
54
|
+
Run also helps you along with some other common scenarios, such as aborting on error and echoing commands as they are executed.
|
55
|
+
|
56
|
+
If we want to check that the backtick command ran successfully, we have to add more code
|
57
|
+
|
58
|
+
unless $?.success?
|
59
|
+
'Ohoh'
|
60
|
+
exit -1
|
61
|
+
end
|
62
|
+
|
63
|
+
Run will by default raise an exception, but you can also configure it to exit with an error message.
|
64
|
+
|
65
|
+
## More examples
|
66
|
+
|
67
|
+
psql = ['psql', :d, source['database'], auth]
|
68
|
+
input = ['pg_dump', :i, auth, '--data-only', :table,'xapian_texts', config['database']]
|
69
|
+
run input, '|', psql
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
def log(msg, level = 0)
|
3
|
+
puts (' ' * (level * 2)) + '# ' + msg
|
4
|
+
end
|
5
|
+
|
6
|
+
def run(*arguments)
|
7
|
+
options = (arguments.last.is_a?(Hash) && arguments.pop) || {}
|
8
|
+
cmd = expand_cmd(*arguments)
|
9
|
+
log(cmd)
|
10
|
+
result = if options[:sync]
|
11
|
+
`#{cmd}`
|
12
|
+
else
|
13
|
+
system(cmd)
|
14
|
+
end
|
15
|
+
unless $?.success?
|
16
|
+
exit -1
|
17
|
+
end
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
def expand_cmd(*arguments)
|
22
|
+
command = arguments.shift.to_s
|
23
|
+
cmd = command + ' ' + arguments.map{|a| a.to_cmd_opt}.join(' ')
|
24
|
+
end
|
25
|
+
|
26
|
+
class Symbol
|
27
|
+
def to_cmd_opt
|
28
|
+
string = to_s
|
29
|
+
if string.length == 1
|
30
|
+
'-' + string
|
31
|
+
else
|
32
|
+
'--' + string
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Array
|
38
|
+
def to_cmd_opt
|
39
|
+
map{|a| a.to_cmd_opt}.join(' ')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class String
|
44
|
+
def to_cmd_opt
|
45
|
+
self
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Hash
|
50
|
+
def to_cmd_opt(style = :gnu)
|
51
|
+
map do |key, value|
|
52
|
+
prefix = ((style == :gnu && key.to_s.size > 1) ? '--' : '-')
|
53
|
+
assignment = (key.to_s.size == 1 || style == :unix) ? ' ' : '='
|
54
|
+
value.is_a?(TrueClass) ? "#{prefix}#{key}" : "#{prefix}#{key}#{assignment}#{value}"
|
55
|
+
end.join(' ')
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-unix-now
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tor Erik Linnerud
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-26 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A tiny library which attempts to bridge the gap between ruby code and executing system commands. No more more string concatenation to build up commands.
|
17
|
+
email: tel@jklm.no
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.md
|
24
|
+
files:
|
25
|
+
- lib/ruby-unix-now.rb
|
26
|
+
- README.md
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/toreriklinnerud/ruby-unix-now
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --charset=UTF-8
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: "Run system commands using the best of ruby syntax: symbols, hashes and arrays."
|
55
|
+
test_files: []
|
56
|
+
|