shelltastic 0.2.5

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ ## Changelog
2
+ * Add multiple command arguments. Bump Version 0.2.5
3
+ * Change Open4 back to popen4. Update README. Bump minor version 0.2.3
4
+ * Bump version 0.2.2. Move timer object out of IO#popen; inject object insteadMove #string_nil_or_blank? to utils module. Now extend this module in IO.
5
+ * Bump version to 0.1.2 for bug fix
6
+ * Fix syntax error in timer
7
+ * Add rake console and fix yard doc
8
+ * Add string check for command and clean up readme
9
+ * Add yard markdown docs
10
+ * Add summary to gemspec
11
+ * Add deps to gemfile spec
12
+ * Add timer test
13
+ * Add run to Command
14
+ * Initial commit
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shelltastic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brad Smith
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,55 @@
1
+ # Shelltastic
2
+ Shelltastic is simple *nix shell wrapper for ruby. You can pass a single command or multiple commands to run.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'shelltastic'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install shelltastic
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ ShellTastic::Command.run("date")
22
+ ```
23
+
24
+ The above will return an Array of hash meta-data.
25
+
26
+ ```ruby
27
+ [{ :output, :pid, :error, :start, :stop, :total_time, :exitstatus }]
28
+ ```
29
+
30
+ For example, the above command's return would look something like this:
31
+
32
+ ```ruby
33
+ [{:output=>"Sun Feb 3 17:41:45 EST 2013", :pid=>17507, :error=>"", :start=>2013-02-03 17:41:45 -0500, :stop=>2013-02-03 17:41:45 -0500, :total_time=>0.004405272, :exitstatus=>0}]
34
+ ```
35
+
36
+ You can also pass multiple commands separated by commas or pass an array.
37
+
38
+ ```ruby
39
+ ShellTastic::Command.run("date", "whoami")
40
+ ```
41
+
42
+ ```ruby
43
+ ShellTastic::Command.run(["date", "whoami"])
44
+ ```
45
+
46
+ ```ruby
47
+ [{:output=>"Sat Apr 6 15:26:05 EDT 2013", :pid=>92558, :error=>"", :start=>2013-04-06 15:26:05 -0400, :stop=>2013-04-06 15:26:05 -0400, :command=>"date", :total_time=>0.010004, :exitstatus=>0}, {:output=>"bradleydsmith", :pid=>92559, :error=>"", :start=>2013-04-06 15:26:05 -0400, :stop=>2013-04-06 15:26:05 -0400, :command=>"whoami", :total_time=>0.008262, :exitstatus=>0}]
48
+ ```
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
8
+
9
+ desc "Open an irb session preloaded with this library"
10
+ task :console do
11
+ sh "irb -Ilib -r shelltastic"
12
+ end
13
+
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'open4'
4
+
5
+ module ShellTastic
6
+ class IO
7
+ extend ShellTastic::Utils
8
+ class << self
9
+ # A wrapper around popen4
10
+ #
11
+ # @see #Command.run
12
+ # @param command [String] command(s) to run
13
+ # @return [Array, Hash] command_results the command results from shell
14
+ # [{ :output, :pid, :error, :start, :stop, :total_time, :exitstatus }]
15
+ def popen(command, timer=ShellTastic::Timer)
16
+ string_nil_or_blank?(command)
17
+ command_results = {}
18
+ begin
19
+ start = timer.start
20
+ return_code = Open4::popen4(command) do |pid, stdin, stdout, stderr|
21
+ command_results.store(:output,stdout.read.strip)
22
+ command_results.store(:pid,pid)
23
+ command_results.store(:error,stderr.read.strip)
24
+ stdin.close
25
+ end
26
+ stop = timer.stop
27
+ total_time = timer.total_time
28
+ command_results.merge!(start: start,
29
+ stop: stop,
30
+ command: command,
31
+ total_time: total_time,
32
+ exitstatus: return_code.exitstatus)
33
+ rescue Errno::ENOENT => e
34
+ raise ShellTastic::CommandException.new("Shell command #{command} failed with status #{$?} and ERROR: #{e.message}")
35
+ end
36
+ command_results
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ end
@@ -0,0 +1,4 @@
1
+ module ShellTastic
2
+ # @raise: Describes an Exception that a method may throw
3
+ class CommandException < RuntimeError; end
4
+ end
@@ -0,0 +1,32 @@
1
+ module ShellTastic
2
+ module Timer
3
+ class << self
4
+ # Creates a start time object
5
+ # @param [nil]
6
+ # @return [Time] Time now object
7
+ def start
8
+ @start = Time.now
9
+ end
10
+
11
+ # Creates a stop time object
12
+ # @param [nil]
13
+ # @return [Time] Time now object
14
+ def stop
15
+ @stop = Time.now
16
+ end
17
+
18
+ # Calculates the total time elapsed
19
+ # @see #start and #stop
20
+ # @param [Hash, nil] opts the opts passed in
21
+ # @option [Boolean] milliseconds the time in milliseconds
22
+ # @return [Time] Time elapsed between #start and #stop
23
+ def total_time(milliseconds = false)
24
+ if milliseconds
25
+ (@stop - @start) * 1000.0
26
+ else
27
+ @stop - @start
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module ShellTastic
2
+
3
+ module Utils
4
+ # A dirty string check for nil empty or just whitespace
5
+ # @param [String] str the string the needs to be checked
6
+ # @return [Boolean, nil] true if str is empty, nil, or just whitespace
7
+ def string_nil_or_blank? str
8
+ # raise if command is empty or nil
9
+ if str !~ /[^[:space:]]/ || str.nil? || str.empty?
10
+ raise ShellTastic::CommandException.new("Command is emtpy or nil")
11
+ end
12
+ end
13
+
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ module Shelltastic
2
+ VERSION = "0.2.5"
3
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "shelltastic/version"
2
+ require_relative "shelltastic/utils"
3
+ require_relative "shelltastic/command_io"
4
+ require_relative "shelltastic/timer"
5
+ require_relative "shelltastic/exceptions"
6
+
7
+ # @! ShellTastic
8
+ module ShellTastic
9
+ # Command is the namespace for the actual meat of the gem
10
+ class Command
11
+ class << self
12
+ # encapsualtes the popen call
13
+ # @param command [String] command or multiple commands to be executed
14
+ # @param timer [Object] timer object, @see ShellTastic::Timer
15
+ # @return [Array] Array of hashes for each command executed @see IO::popen
16
+ # @example
17
+ # ShellTastic::Command.run "whoami"
18
+ # ShellTastic::Command.run "whoami", "date"
19
+ def run(*command)
20
+ command.flatten.map { |cmd| ShellTastic::IO.popen(cmd, ShellTastic::Timer) }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shelltastic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "shelltastic"
8
+ gem.version = Shelltastic::VERSION
9
+ gem.authors = ["Brad Smith"]
10
+ gem.email = ["bradleydsmith@gmail.com"]
11
+ gem.description = %q{Shelltastic is another *nix shell wrapper}
12
+ gem.summary = %q{*nix shell wrapper}
13
+ gem.homepage = ""
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
+
20
+ gem.add_dependency('open4', '>= 1.3.0')
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe ShellTastic do
4
+ it "should run a shell command" do
5
+ result = ShellTastic::Command.run("ls -l")
6
+ result.first.fetch(:exitstatus).should eq(0)
7
+ end
8
+
9
+ it "should run multiple commands" do
10
+ result = ShellTastic::Command.run("ls -l", "date")
11
+ result.size.should eq(2)
12
+ end
13
+
14
+ it "should run take an array of commands to run" do
15
+ result = ShellTastic::Command.run(["ls -l", "date"])
16
+ result.size.should eq(2)
17
+ end
18
+
19
+
20
+ it "should raise a command exception" do
21
+ expect {
22
+ result = ShellTastic::Command.run("lss -l")
23
+ }.to raise_error(ShellTastic::CommandException)
24
+ end
25
+
26
+ it "should alert if command is empty or nil" do
27
+ expect {
28
+ ShellTastic::Command.run("")
29
+ }.to raise_error("Command is emtpy or nil")
30
+ end
31
+
32
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'shelltastic'
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ShellTastic::Timer do
4
+ it "should report start time" do
5
+ start = ShellTastic::Timer.start
6
+ start.class.should eq(Time)
7
+ end
8
+
9
+ it "should report stop time" do
10
+ stop = ShellTastic::Timer.stop
11
+ stop.class.should eq(Time)
12
+ end
13
+
14
+ it "should report total time" do
15
+ start = ShellTastic::Timer.start
16
+ sleep 2
17
+ stop = ShellTastic::Timer.stop
18
+ total = ShellTastic::Timer.total_time
19
+ total.round.should eq(2)
20
+ end
21
+
22
+ end
data/tags ADDED
@@ -0,0 +1,23 @@
1
+ !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2
+ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3
+ !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
4
+ !_TAG_PROGRAM_NAME Exuberant Ctags //
5
+ !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
6
+ !_TAG_PROGRAM_VERSION 5.9~svn20110310 //
7
+ Command lib/shelltastic.rb /^ class Command$/;" c class:ShellTastic
8
+ CommandException lib/shelltastic/exceptions.rb /^ class CommandException < RuntimeError; end$/;" c class:ShellTastic
9
+ IO lib/shelltastic/command_io.rb /^ module IO$/;" m class:ShellTastic
10
+ ShellTastic lib/shelltastic.rb /^module ShellTastic$/;" m
11
+ ShellTastic lib/shelltastic/command_io.rb /^module ShellTastic$/;" m
12
+ ShellTastic lib/shelltastic/exceptions.rb /^module ShellTastic$/;" m
13
+ ShellTastic lib/shelltastic/timer.rb /^module ShellTastic$/;" m
14
+ ShellTastic lib/shelltastic/utils.rb /^module ShellTastic$/;" m
15
+ Shelltastic lib/shelltastic/version.rb /^module Shelltastic$/;" m
16
+ Timer lib/shelltastic/timer.rb /^ module Timer$/;" m class:ShellTastic
17
+ Utils lib/shelltastic/utils.rb /^ module Utils$/;" m class:ShellTastic
18
+ popen lib/shelltastic/command_io.rb /^ def popen(command, timer=ShellTastic::Timer)$/;" f class:ShellTastic.IO
19
+ run lib/shelltastic.rb /^ def run(command, *args)$/;" f class:ShellTastic.Command
20
+ start lib/shelltastic/timer.rb /^ def start$/;" f class:ShellTastic.Timer
21
+ stop lib/shelltastic/timer.rb /^ def stop$/;" f class:ShellTastic.Timer
22
+ string_nil_or_blank? lib/shelltastic/utils.rb /^ def string_nil_or_blank? str$/;" f class:ShellTastic.Utils
23
+ total_time lib/shelltastic/timer.rb /^ def total_time(milliseconds = false)$/;" f class:ShellTastic.Timer
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shelltastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brad Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: open4
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Shelltastic is another *nix shell wrapper
63
+ email:
64
+ - bradleydsmith@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - CHANGELOG.md
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/shelltastic.rb
77
+ - lib/shelltastic/command_io.rb
78
+ - lib/shelltastic/exceptions.rb
79
+ - lib/shelltastic/timer.rb
80
+ - lib/shelltastic/utils.rb
81
+ - lib/shelltastic/version.rb
82
+ - shelltastic.gemspec
83
+ - spec/shelltastic_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/timer_spec.rb
86
+ - tags
87
+ homepage: ''
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ segments:
100
+ - 0
101
+ hash: 3694164165919668282
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: 3694164165919668282
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.24
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: ! '*nix shell wrapper'
117
+ test_files:
118
+ - spec/shelltastic_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/timer_spec.rb