scmd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.log
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ .rvmrc
8
+ .rbenv-version
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scmd.gemspec
4
+ gemspec
5
+
6
+ gem 'bundler', '~>1.1'
7
+ gem 'rake', '~>0.9.2'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 <TODO: copyright holders>
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.
@@ -0,0 +1,72 @@
1
+ # Scmd
2
+
3
+ Wrapper to `open4` for running system commands.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'scmd'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install scmd
18
+
19
+ ## Usage
20
+
21
+ Create a command object:
22
+
23
+ ```ruby
24
+ cmd = Scmd.new("echo hi")
25
+
26
+ cmd.to_s #=> "echo hi"
27
+ cmd.inspect #=> #<Scmd::Command:0x83220514 @cmd_str="echo hi" @exitcode=nil>
28
+
29
+ cmd.pid #=> nil
30
+ cmd.exitcode #=> nil
31
+ cmd.stdout #=> ''
32
+ cmd.stderr #=> ''
33
+ ```
34
+
35
+ Run it:
36
+
37
+ ```ruby
38
+ cmd.run
39
+ ```
40
+
41
+ Results:
42
+
43
+ ```ruby
44
+ # written to the cmd instance
45
+ cmd.pid #=> 12345
46
+ cmd.exitcode #=> 0
47
+ cmd.stdout #=> 'hi'
48
+ cmd.stderr #=> ''
49
+
50
+ # the cmd instance is returned by `run` for chaining as well
51
+ cmd.run.stdout #=> 'hi'
52
+ ```
53
+
54
+ Some helpers:
55
+
56
+ ```ruby
57
+ puts cmd.stderr if !cmd.success?
58
+ ```
59
+
60
+ Raise an exception if not successful with `run!`:
61
+
62
+ ```ruby
63
+ Scmd.new("cd /path/that/does/not/exist").run! #=> Scmd::Command::Failure
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'assert/rake_tasks'
4
+ Assert::RakeTasks.install
5
+
6
+ require 'bundler/gem_tasks'
7
+
8
+ task :default => :build
@@ -0,0 +1,9 @@
1
+ require 'scmd/command'
2
+
3
+ module Scmd
4
+
5
+ def self.new(*args, &block)
6
+ Command.new(*args, &block)
7
+ end
8
+
9
+ end
@@ -0,0 +1,63 @@
1
+ # Scmd::Command is a base wrapper for handling system commands. Initialize it
2
+ # with with a string specifying the command to execute. You can then run the
3
+ # command and inspect its results. It can be used as is, or inherited from to
4
+ # create a more custom command wrapper.
5
+ #
6
+ # Notes:
7
+ # * Uses `open4`. Open4 is more reliable for actually getting the subprocesses
8
+ # exit code (compared to `open3`).
9
+ # * The inspect method is overwritten to only display the name of the class and
10
+ # the command string. This is to help reduce ridiculous inspect strings due to
11
+ # result data that is stored in instance variables.
12
+ # * See the README.md for a walkthrough of the API.
13
+
14
+ require 'open4'
15
+
16
+ module Scmd
17
+ class Command
18
+
19
+ class Failure < RuntimeError; end
20
+
21
+ attr_reader :cmd_str
22
+ attr_reader :pid, :exitcode, :stdout, :stderr
23
+
24
+ def initialize(cmd_str)
25
+ @cmd_str = cmd_str
26
+ reset_results
27
+ end
28
+
29
+ def success?; @exitcode == 0; end
30
+ def to_s; @cmd_str.to_s; end
31
+ def inspect
32
+ "#<#{self.class}:0x#{self.object_id.to_s(16)} @cmd_str=#{self.cmd_str.inspect} @exitcode=#{@exitcode.inspect}>"
33
+ end
34
+
35
+ def run
36
+ run! rescue Failure
37
+ self
38
+ end
39
+
40
+ def run!
41
+ begin
42
+ status = Open4::popen4(@cmd_str) do |pid, stdin, stdout, stderr|
43
+ @pid = pid.to_i
44
+ @stdout += stdout.read.strip
45
+ @stderr += stderr.read.strip
46
+ end
47
+ @exitcode = status.to_i
48
+ rescue Errno::ENOENT => err
49
+ @exitcode = -1
50
+ @stderr = err.message
51
+ end
52
+
53
+ raise Failure, @stderr if !success?
54
+ self
55
+ end
56
+
57
+ def reset_results
58
+ @pid = @exitcode = nil
59
+ @stdout = @stderr = ''
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Scmd
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/scmd/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "scmd"
6
+ gem.version = Scmd::VERSION
7
+ gem.description = %q{Wrapper to `open4` for running system commands.}
8
+ gem.summary = %q{Wrapper to `open4` for running system commands.}
9
+
10
+ gem.authors = ["Kelly Redding and Team Insight"]
11
+ gem.email = ["appdev@reelfx.com"]
12
+ gem.homepage = "http://github.com/teaminsight/scmd"
13
+
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency("assert")
20
+ gem.add_dependency("open4")
21
+ end
@@ -0,0 +1,57 @@
1
+ require "assert"
2
+ require 'scmd/command'
3
+
4
+ class CommandTests < Assert::Context
5
+ desc "a command"
6
+ setup do
7
+ @success_cmd = Scmd::Command.new("echo hi")
8
+ @failure_cmd = Scmd::Command.new("cd /path/that/does/not/exist")
9
+ end
10
+ subject { @success_cmd }
11
+
12
+ should have_readers :cmd_str, :pid, :exitcode, :stdout, :stderr
13
+ should have_instance_methods :run, :run!
14
+
15
+ should "know and return its cmd string" do
16
+ assert_equal "echo hi", subject.cmd_str
17
+ assert_equal "echo hi", subject.to_s
18
+ end
19
+
20
+ should "default its result values" do
21
+ assert_nil subject.pid
22
+ assert_nil subject.exitcode
23
+ assert_equal '', subject.stdout
24
+ assert_equal '', subject.stderr
25
+ end
26
+
27
+ should "run the command and set appropriate result data" do
28
+ @success_cmd.run
29
+
30
+ assert_not_nil @success_cmd.pid
31
+ assert_equal 0, @success_cmd.exitcode
32
+ assert @success_cmd.success?
33
+ assert_equal 'hi', @success_cmd.stdout
34
+ assert_equal '', @success_cmd.stderr
35
+
36
+ @failure_cmd.run
37
+
38
+ assert_not_nil @failure_cmd.pid
39
+ assert_not_equal 0, @failure_cmd.exitcode
40
+ assert_not @failure_cmd.success?
41
+ assert_equal '', @failure_cmd.stdout
42
+ assert_not_equal '', @failure_cmd.stderr
43
+ end
44
+
45
+ should "raise an exception on `run!` and a non-zero exitcode" do
46
+ assert_raises Scmd::Command::Failure do
47
+ @failure_cmd.run!
48
+ end
49
+ end
50
+
51
+ should "return itself on `run`, `run!`" do
52
+ assert_equal @success_cmd, @success_cmd.run
53
+ assert_equal @success_cmd, @success_cmd.run!
54
+ assert_equal @failure_cmd, @failure_cmd.run
55
+ end
56
+
57
+ end
@@ -0,0 +1,5 @@
1
+ # this file is automatically required in when you require 'assert' in your tests
2
+ # put test helpers here
3
+
4
+ # add root dir to the load path
5
+ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
@@ -0,0 +1,9 @@
1
+ require 'assert/setup'
2
+
3
+ # this file is required in when the 'irb' rake test is run.
4
+ # b/c 'assert/setup' is required above, the test helper will be
5
+ # required in as well.
6
+
7
+ # put any IRB setup code here
8
+
9
+ require 'scmd'
@@ -0,0 +1,14 @@
1
+ require "assert"
2
+ require 'scmd'
3
+
4
+ class ScmdTest < Assert::Context
5
+ desc "Scmd"
6
+ subject { Scmd }
7
+
8
+ should have_instance_method :new
9
+
10
+ should "build a `Command` with the `new` method" do
11
+ assert_kind_of Scmd::Command, subject.new('echo hi')
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scmd
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Kelly Redding and Team Insight
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-18 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ name: assert
32
+ version_requirements: *id001
33
+ prerelease: false
34
+ - !ruby/object:Gem::Dependency
35
+ type: :runtime
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ name: open4
46
+ version_requirements: *id002
47
+ prerelease: false
48
+ description: Wrapper to `open4` for running system commands.
49
+ email:
50
+ - appdev@reelfx.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/scmd.rb
64
+ - lib/scmd/command.rb
65
+ - lib/scmd/version.rb
66
+ - scmd.gemspec
67
+ - test/command_tests.rb
68
+ - test/helper.rb
69
+ - test/irb.rb
70
+ - test/scmd_tests.rb
71
+ homepage: http://github.com/teaminsight/scmd
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.11
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Wrapper to `open4` for running system commands.
104
+ test_files:
105
+ - test/command_tests.rb
106
+ - test/helper.rb
107
+ - test/irb.rb
108
+ - test/scmd_tests.rb