easy_shell 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +1 -0
- data/easy_shell.gemspec +19 -0
- data/lib/easy_shell.rb +44 -0
- data/lib/easy_shell/core_ext/kernel.rb +5 -0
- data/lib/easy_shell/version.rb +3 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Harlan T Wood
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
easy_shell
|
2
|
+
==========
|
3
|
+
|
4
|
+
Easily execute shell commands from ruby, with options like verbose output,
|
5
|
+
confirm first, and continue (or abort) on failure.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'easy_shell'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install easy_shell
|
21
|
+
|
22
|
+
Sample Usage
|
23
|
+
------------
|
24
|
+
|
25
|
+
require 'easy_shell'
|
26
|
+
|
27
|
+
Vanilla usage
|
28
|
+
|
29
|
+
> run "date +%M"
|
30
|
+
=> Running "date +%M"
|
31
|
+
11
|
32
|
+
=> "11\n"
|
33
|
+
|
34
|
+
Quiet
|
35
|
+
|
36
|
+
> run "date +%M", :quiet => true
|
37
|
+
=> "12\n"
|
38
|
+
|
39
|
+
Require interactive confirmation before executing command
|
40
|
+
|
41
|
+
> run "date +%M", :confirm_first => true
|
42
|
+
=> Running "date +%M"
|
43
|
+
Execute [Yn]? [Yn]
|
44
|
+
13
|
45
|
+
=> "13\n"
|
46
|
+
|
47
|
+
> run "date +%M", :confirm_first => true
|
48
|
+
=> Running "date +%M"
|
49
|
+
Execute [Yn]? [Yn] n
|
50
|
+
=> nil
|
51
|
+
|
52
|
+
Raise exception when the command fails (not found or non-zero exit code)...
|
53
|
+
|
54
|
+
> run "nosuchcommand"
|
55
|
+
=> Running "nosuchcommand"
|
56
|
+
RuntimeError: ERROR running "nosuchcommand"
|
57
|
+
Output:
|
58
|
+
sh: nosuchcommand: command not found
|
59
|
+
[stacktrace removed]
|
60
|
+
|
61
|
+
...unless we ask to continue on failure
|
62
|
+
|
63
|
+
> run "nosuchcommand", :continue_on_failure => true
|
64
|
+
=> Running "nosuchcommand"
|
65
|
+
Continuing, ignoring error while running "nosuchcommand"
|
66
|
+
Output:
|
67
|
+
sh: nosuchcommand: command not found
|
68
|
+
=> "sh: nosuchcommand: command not found\n"
|
69
|
+
|
70
|
+
Contributing
|
71
|
+
------------
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/easy_shell.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy_shell/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "easy_shell"
|
8
|
+
gem.version = EasyShell::VERSION
|
9
|
+
gem.authors = ["Harlan T Wood"]
|
10
|
+
gem.email = ["code@harlantwood.net"]
|
11
|
+
gem.description = %q{Execute shell commands, with options like verbose output, confirm first, and continue or stop on failure}
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = "https://github.com/harlantwood/easy_shell"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/easy_shell.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "easy_shell/version"
|
2
|
+
require "easy_shell/core_ext/kernel"
|
3
|
+
|
4
|
+
module EasyShell
|
5
|
+
def self.run(cmd, options = {})
|
6
|
+
defaults = {
|
7
|
+
:quiet => false,
|
8
|
+
:confirm_first => false,
|
9
|
+
:continue_on_failure => false,
|
10
|
+
}
|
11
|
+
unknown_options = options.keys - defaults.keys
|
12
|
+
raise "Unknown options #{unknown_options.inspect}" unless unknown_options.empty?
|
13
|
+
options = defaults.merge(options)
|
14
|
+
cmd.gsub!(/\s+/, ' ')
|
15
|
+
cmd.strip!
|
16
|
+
cmd_sanitized = cmd.gsub(%r{(https://[-\w]+:)[-\w]+@}, '\1[password sanitized]@')
|
17
|
+
puts "=> Running #{cmd_sanitized.inspect}\n" unless options[:quiet]
|
18
|
+
|
19
|
+
return unless confirm("Execute [Yn]? ") if options[:confirm_first]
|
20
|
+
|
21
|
+
output = `#{cmd} 2>&1`
|
22
|
+
success = $?.success?
|
23
|
+
puts output if success && !options[:quiet]
|
24
|
+
|
25
|
+
unless success
|
26
|
+
if options[:continue_on_failure]
|
27
|
+
msg = "Continuing, ignoring error while running #{cmd_sanitized.inspect}"
|
28
|
+
msg += "\nOutput:\n#{output}" if output
|
29
|
+
puts msg
|
30
|
+
else
|
31
|
+
msg = "ERROR running #{cmd_sanitized.inspect}"
|
32
|
+
msg += "\nOutput:\n#{output}" if output
|
33
|
+
raise msg
|
34
|
+
end
|
35
|
+
end
|
36
|
+
output
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.confirm(msg)
|
40
|
+
STDOUT << "#{msg} [Yn] "
|
41
|
+
response = STDIN.gets.chomp
|
42
|
+
response.empty? || !!response.match(/^(y|yes)$/i)
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_shell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Harlan T Wood
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Execute shell commands, with options like verbose output, confirm first,
|
15
|
+
and continue or stop on failure
|
16
|
+
email:
|
17
|
+
- code@harlantwood.net
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- easy_shell.gemspec
|
28
|
+
- lib/easy_shell.rb
|
29
|
+
- lib/easy_shell/core_ext/kernel.rb
|
30
|
+
- lib/easy_shell/version.rb
|
31
|
+
homepage: https://github.com/harlantwood/easy_shell
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Execute shell commands, with options like verbose output, confirm first,
|
55
|
+
and continue or stop on failure
|
56
|
+
test_files: []
|