komenda 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: c3b17f1984f7f2f3ccab6d25a6ecd9e2714d6710
4
+ data.tar.gz: 6d845d8f6974ad2a53d66875e5a0a691459adc1d
5
+ SHA512:
6
+ metadata.gz: 2442eed8d0dcc1acdb1f218e6ac219219dab35bc330d3747ffaf79d3282776a4a282c3e714644cc1f5878e3c5a34cd4fa2ddf1294150e8b30ee5e0decbc8a50a
7
+ data.tar.gz: f175f373cd17ff6e84debf25666d1f10584344d886a996059efb3af769345f1f4977bc68c0cffefc44456cb41936dcef5a92b1488e8478d6246b730f198b475a
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Cargo Media
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ Komenda [![Build Status](https://travis-ci.org/cargomedia/komenda.svg)](https://travis-ci.org/cargomedia/komenda)
2
+ =======
3
+ Komenda is a convenience wrapper around `Open3` to run shell commands in Ruby.
4
+
5
+ Usage
6
+ -----
7
+ Run a command:
8
+ ```ruby
9
+ Komenda.run("date")
10
+ ```
11
+
12
+ The `run()` method will block until the sub process finished.
13
+
14
+ It will expose the output and exit status as a `Komenda::Result` value:
15
+ ```ruby
16
+ result = Komenda.run("date")
17
+ result.stdout # => "Tue Nov 26 14:45:03 EST 2013\n"
18
+ result.stderr # => ""
19
+ result.output # => "Tue Nov 26 14:45:03 EST 2013\n" (combined stdout + stderr)
20
+ result.status # => 0
21
+ result.success? # => true
22
+ result.pid # => 32157
23
+ ```
24
+
25
+ TODO
26
+ ----
27
+ - `Komenda::Definition`: Allow to pass STDIN
28
+ - `Komenda::Definition`: Allow to pass environment variables
data/lib/komenda.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'open3'
2
+
3
+ require 'komenda/definition'
4
+ require 'komenda/runner'
5
+ require 'komenda/result'
6
+
7
+ module Komenda
8
+
9
+ # @param [String] cmd
10
+ # @return [Komenda::Result]
11
+ def self.run(cmd)
12
+ definition = Komenda::Definition.new(cmd)
13
+ runner = Komenda::Runner.new
14
+ runner.run(definition)
15
+ end
16
+
17
+ end
@@ -0,0 +1,12 @@
1
+ module Komenda
2
+ class Definition
3
+
4
+ attr_reader :cmd
5
+
6
+ # @param [String] cmd
7
+ def initialize(cmd)
8
+ @cmd = cmd
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module Komenda
2
+ class Result
3
+
4
+ # @param [Hash] output
5
+ # @option output_streams [String] :stdout
6
+ # @option output_streams [String] :stderr
7
+ # @param [Process::Status] status
8
+ def initialize(output, status)
9
+ @output = output
10
+ @status = status
11
+ end
12
+
13
+ def stdout
14
+ @output[:stdout]
15
+ end
16
+
17
+ def stderr
18
+ @output[:stderr]
19
+ end
20
+
21
+ def output
22
+ @output[:combined]
23
+ end
24
+
25
+ def exitstatus
26
+ @status.exitstatus
27
+ end
28
+
29
+ alias_method :status, :exitstatus
30
+
31
+ def success?
32
+ @status.success?
33
+ end
34
+
35
+ def pid
36
+ @status.pid
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ module Komenda
2
+ class Runner
3
+
4
+ # @param [Komenda::Definition] definition
5
+ # @return [Komenda::Result]
6
+ def run(definition)
7
+ output = {:stdout => '', :stderr => '', :combined => ''}
8
+ status = nil
9
+
10
+ Open3.popen3(definition.cmd) do |stdin, stdout, stderr, wait_thr|
11
+ stdin.close
12
+
13
+ streams_read_open = [stdout, stderr]
14
+ begin
15
+ streams_read_available, _, _ = IO.select(streams_read_open)
16
+
17
+ streams_read_available.each do |stream|
18
+ if stream.eof?
19
+ stream.close
20
+ streams_read_open.delete(stream)
21
+ else
22
+ data = stream.readpartial(4096)
23
+ output[:stdout] += data if stdout === stream
24
+ output[:stderr] += data if stderr === stream
25
+ output[:combined] += data
26
+ end
27
+ end
28
+ end until streams_read_open.empty?
29
+
30
+ status = wait_thr.value
31
+ end
32
+
33
+ Komenda::Result.new(output, status)
34
+ end
35
+
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: komenda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cargo Media
8
+ - njam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-10-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.1'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.0'
56
+ description: Convenience wrapper around `Open3` to run shell commands in Ruby.
57
+ email: hello@cargomedia.ch
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/komenda.rb
65
+ - lib/komenda/definition.rb
66
+ - lib/komenda/result.rb
67
+ - lib/komenda/runner.rb
68
+ homepage: https://github.com/cargomedia/komenda
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.8
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Convenience wrapper around `Open3` to run shell commands in Ruby.
92
+ test_files: []