cmd_runner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 75dc4bed7102dec3ca424559b297593ef550fee4
4
+ data.tar.gz: 09bfc5189491f249602231122a021e7e48bc4ae8
5
+ SHA512:
6
+ metadata.gz: bbc0097d22eab29ed2e4b12417e55a427b352dbecca68ee86bc42cdbfb3ec418e43707e6059822b3054216fdeabe58f91cd3b99fc5d72d654d9758833dc126dd
7
+ data.tar.gz: f0ea2f24974b3035620ff3ad81f5da5653e856d7d6a66313b0ef9b5e34ccdfbb49f8fb9b7ce6cbdac3c2be21a9d7f264ade3342f10255f2342d1fdf1aa148c99
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ *.swo
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cmd_runner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Denis Yagofarov
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,40 @@
1
+ # CmdRunner
2
+
3
+ Simple wrapper for Open3 to execute command.
4
+ The command treated as an object and is able to be executed.
5
+ You can specify `logger` to point runtime messages to and
6
+ `work_dir` to change into it before each run.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'cmd_runner'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cmd_runner
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ cmd = CmdRunner::Command.new('ls')
26
+ cmd.work_dir = '/tmp'
27
+ cmd.logger = Logger.new('./ls.log')
28
+ cmd.execute!
29
+ #=> [0, "launch-qO1j87\ntextmate-501.sock\n", '']
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ As always:
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cmd_runner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cmd_runner"
8
+ spec.version = CmdRunner::VERSION
9
+ spec.authors = ["Denis Yagofarov"]
10
+ spec.email = ["denyago@gmail.com"]
11
+ spec.description = %q{Simple helper to run commands}
12
+ spec.summary = %q{Simple helper to run commands}
13
+ spec.homepage = 'https://github.com/denyago/cmd_runner'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry-debugger"
25
+ end
@@ -0,0 +1,3 @@
1
+ module CmdRunner
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cmd_runner.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'cmd_runner/version'
2
+ require 'logger'
3
+ require 'open3'
4
+
5
+ module CmdRunner
6
+ ##
7
+ # Class: Command
8
+ #
9
+ # Runs a command and grabs STDOUT, STDERR.
10
+ # Checks exit status to raise an exception in case of non-zero.
11
+ #
12
+ # Example:
13
+ # runner = CmdRunner::Command.new
14
+ # runner.work_dir = '/tmp' # to change dir before every command
15
+ # runner.logger = Logger.new # to log command results
16
+ # runner.execute!('ls') #=> [0, '.\n..\n', '']
17
+ #
18
+ class Command
19
+ # Change directory before each command
20
+ attr_accessor :work_dir
21
+ # Logger to output command results
22
+ attr_accessor :logger
23
+ # Command to execute
24
+ attr_writer :cmd
25
+
26
+ def cmd
27
+ if self.work_dir.to_s.empty?
28
+ @cmd
29
+ else
30
+ "cd #{self.work_dir} && " + @cmd
31
+ end
32
+ end
33
+
34
+ # Executes command.
35
+ #
36
+ # Raises:
37
+ # - {RuntimeError} if command returned non-zero status
38
+ #
39
+ # Returns {Array} with exit status, stdout, stderr
40
+ def execute!
41
+ result = ''
42
+ error = ''
43
+
44
+ stdin, stdout, stderr, wait_thr = Open3.popen3(self.cmd)
45
+ exit_status = wait_thr.value.exitstatus
46
+ result << stdout.read
47
+ error << stderr.read
48
+
49
+ raise "Command exited with non zero status: #{self.cmd}" unless exit_status.zero?
50
+ [exit_status, result, error]
51
+ ensure
52
+ logger.info '-'*25
53
+ logger.info "Command: #{self.cmd}"
54
+ logger.info result
55
+ logger.error error
56
+ logger.info '-'*25
57
+ end
58
+
59
+ private
60
+
61
+ # Initializer
62
+ #
63
+ # Params:
64
+ # - cmd {String} with command to run
65
+ #
66
+ def initialize(cmd)
67
+ @cmd = cmd
68
+ end
69
+
70
+ def logger
71
+ @logger ||= ::Logger.new(STDOUT)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe CmdRunner::Command do
4
+ subject { described_class.new(command) }
5
+
6
+ before do
7
+ subject.logger = Logger.new('/dev/null')
8
+ end
9
+
10
+ context 'successfully' do
11
+ let(:command) { 'ls' }
12
+
13
+ it 'runns' do
14
+ results = subject.execute!
15
+ expect(results[0]).to eq(0)
16
+ expect(results[1]).to_not be_empty
17
+ expect(results[2]).to be_empty
18
+ end
19
+
20
+ context 'in a work dir' do
21
+ it 'cd to work dir before it runns' do
22
+ subject.work_dir = 'foo'
23
+ expect(subject.cmd).to match /^cd foo && /
24
+ end
25
+ end
26
+ end
27
+
28
+ context 'unsuccessfully' do
29
+ let(:command) { 'exit 1' }
30
+
31
+ it 'runns' do
32
+ expect { subject.execute! }.to raise_error("Command exited with non zero status: exit 1")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+ require 'rspec/mocks'
3
+ require 'pry'
4
+
5
+ require 'cmd_runner'
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ config.mock_with :rspec
10
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmd_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Denis Yagofarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-debugger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple helper to run commands
70
+ email:
71
+ - denyago@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - cmd_runner.gemspec
82
+ - lib/cmd_runner.rb
83
+ - lib/cmd_runner/version.rb
84
+ - spec/cmd_runner/command_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: https://github.com/denyago/cmd_runner
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.1.5
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Simple helper to run commands
110
+ test_files:
111
+ - spec/cmd_runner/command_spec.rb
112
+ - spec/spec_helper.rb