command_line 1.0.0

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
+ SHA256:
3
+ metadata.gz: 7c96c7405e0e0bc9f0ab84fd180bd29934eac52693b49793223a62cc87a6e8bd
4
+ data.tar.gz: 2713ef9a363e555106a50d3bb9e3c182f559fef9c4c536427abf8ce6cf869352
5
+ SHA512:
6
+ metadata.gz: 2f4c26ed8af0508911fc9e607ca407d30bf6ce3ea76bad548b510027fd0664a2ec0ec6f541aedc6b25f8eb1407f289033fde8f0107dd9fc1cc7fc6d2056f3db1
7
+ data.tar.gz: fc00e60b70829934ea0b4bbbde98d12a7f68b487963b4a76d84c8d710ca56fa040b66a08d3197d8c7262a0205e6c7b319129b6b9d8b2a2acf491d33ca4476951
data/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ root = true
2
+
3
+ # Everywhere
4
+ [*]
5
+ end_of_line = lf # Unix-style newlines
6
+ trim_trailing_whitespace = true
7
+
8
+ # Ruby
9
+ [.rb, .rake, Rakefile]
10
+ indent_style = space
11
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /pkg/
4
+ /spec/reports/
5
+ /spec/examples.txt
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Exclude:
4
+ - '*.gemspec'
5
+ - Gemfile
6
+ - Rakefile
7
+ - spec/fixtures/**/*
8
+ - bin/**/*
9
+ Layout/AlignArguments:
10
+ EnforcedStyle: with_fixed_indentation
11
+ Layout/IndentFirstHashElement:
12
+ EnforcedStyle: consistent
13
+ Metrics/BlockLength:
14
+ Exclude:
15
+ - spec/**/*
16
+ Metrics/MethodLength:
17
+ Max: 15
data/.travis.yml ADDED
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+ before_install: # for bundler 2.0+
3
+ - gem install bundler
4
+ script: bundle exec rake $RAKE_TASK
5
+ matrix:
6
+ include:
7
+ # Linux - Tests and Linting
8
+ - os: linux
9
+ rvm: 2.6
10
+ - os: linux
11
+ rvm: 2.5
12
+ - os: linux
13
+ rvm: 2.4
14
+ # OS X - Tests
15
+ - os: osx
16
+ rvm: 2.6
17
+ env: RAKE_TASK=spec
18
+ - os: osx
19
+ rvm: 2.5
20
+ env: RAKE_TASK=spec
21
+ - os: osx
22
+ rvm: 2.4
23
+ env: RAKE_TASK=spec
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0 (TBD)
2
+
3
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Aaron Lasseigne
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # CommandLine
2
+
3
+ CommandLine provides an easier way to run command-line applications.
4
+ It captures all outputs, can handle applications that require stdin, and can pass environment variables.
5
+ It's also helpful for testing commmand-line applications.
6
+
7
+ This project is tested against and works on Linux, OS X, and Windows.
8
+
9
+ ## Installation
10
+
11
+ This project uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'command_line', '~> 1.0.0'
17
+ ```
18
+
19
+ If you want `command_line` available globally you can add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'command_line', '~> 1.0.0', require: 'command_line/global'
23
+ ```
24
+
25
+ Or manually install it yourself with:
26
+
27
+ ```sh
28
+ $ gem install command_line
29
+ ```
30
+
31
+ In a script you can make `command_line` available globally with:
32
+
33
+ ```ruby
34
+ require 'command_line/global'
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ All examples below assume that `command_line` has been made available globally.
40
+ If not, simply call `CommandLine.command_line` instead of `command_line`.
41
+
42
+ With `command_line` you to easily run commands and check their `stdout`, `stderr`, and exit status.
43
+
44
+ ```ruby
45
+ >> result = command_line('echo', 'hello')
46
+ => #<CommandLine::Result ...>
47
+ >> result.stdout
48
+ => "hello\n"
49
+ >> result.stderr
50
+ => ""
51
+ >> result.exited?
52
+ => true
53
+ >> result.exitstatus
54
+ => 0
55
+ >> result.success?
56
+ => true
57
+ >> result.failure?
58
+ => false
59
+ ```
60
+
61
+ If your application requires input use a block to send input.
62
+
63
+ ```ruby
64
+ command_line('command_expecting_input') do |stdin|
65
+ stdin.puts "first input"
66
+ stdin.puts "second input"
67
+ end
68
+ ```
69
+
70
+ Environment variables can be passed after the command and arguments are passed.
71
+
72
+ ```ruby
73
+ command_line('some_webserver', { 'PORT' => '80' })
74
+ ```
75
+
76
+ ### RSpec
77
+
78
+ To have direct access to `CommandLine.command_line` you can include it in `spec/spec_helper.rb`.
79
+
80
+ ```ruby
81
+ require 'command_line'
82
+
83
+ RSpec.configure do |config|
84
+ config.include CommandLine
85
+ end
86
+ ```
87
+
88
+ This will make `command_line` available in your test suite.
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DragonRuby/command_line.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+ RuboCop::RakeTask.new do |task|
7
+ task.options = ['--display-cop-names']
8
+ end
9
+
10
+ task default: %i[spec rubocop]
data/appveyor.yml ADDED
@@ -0,0 +1,19 @@
1
+ environment:
2
+ matrix:
3
+ - RUBY_VERSION: 26
4
+ - RUBY_VERSION: 25
5
+ - RUBY_VERSION: 24
6
+
7
+ install:
8
+ - set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH%
9
+ - bundle install
10
+
11
+ build: off
12
+
13
+ before_test:
14
+ - ruby -v
15
+ - gem -v
16
+ - bundle -v
17
+
18
+ test_script:
19
+ - bundle exec rake spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'command_line'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'command_line/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'command_line'
7
+ spec.version = CommandLine::VERSION
8
+ spec.authors = ['Aaron Lasseigne']
9
+ spec.email = ['aaron.lasseigne@dragonruby.org']
10
+
11
+ spec.summary = 'An easier way execute command line applications and get all of the output.'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/DragonRuby/command_line'
14
+ spec.license = 'MIT'
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/DragonRuby/command_line'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/DragonRuby/command_line/blob/master/CHANGELOG.md'
20
+ end
21
+
22
+ spec.required_ruby_version = '>= 2.4'
23
+
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.9'
34
+ spec.add_development_dependency 'rubocop', '~> 0.75.0'
35
+ spec.add_development_dependency 'yard', '~> 0.9.20'
36
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+
5
+ require 'command_line/result'
6
+ require 'command_line/version'
7
+
8
+ # CommandLine provides an easier way to run command-line applications. It
9
+ # captures all outputs, can handle applications that require stdin, and can pass
10
+ # environment variables. It's also helpful for testing commmand-line
11
+ # applications.
12
+ module CommandLine
13
+ module_function
14
+
15
+ # Run a command and get back the result.
16
+ #
17
+ # @param command [String] the command to run
18
+ # @param args [Array<String>] any arguments passed to the command
19
+ # @param [Hash] env: Pass environment variables to use. The key should
20
+ # be a String representing the environment variable name. The value
21
+ # is the value you want that variable to have.
22
+ #
23
+ # @yield [stdin] Handle any input on stdin that the command needs.
24
+ # @yieldparam stdin [IO]
25
+ #
26
+ # @example
27
+ # command_line('echo', 'hello')
28
+ #
29
+ # @example
30
+ # command_line('command_expecting_input') do |stdin|
31
+ # stdin.puts "first input"
32
+ # stdin.puts "second input"
33
+ # end
34
+ #
35
+ # @example
36
+ # command_line('some_webserver', { 'PORT' => '80' })
37
+ #
38
+ # @return [Result]
39
+ def command_line(command, *args, env: {})
40
+ stdout = ''
41
+ stderr = ''
42
+ status = nil
43
+
44
+ Open3.popen3(env, command, *args) do |i, o, e, wait_thr|
45
+ yield i if block_given?
46
+
47
+ stdout = o.read
48
+ stderr = e.read
49
+ status = wait_thr.value
50
+ end
51
+
52
+ Result.new(stdout, stderr, status)
53
+ end
54
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'command_line'
4
+
5
+ # Docs are included here so they can be discovered.
6
+
7
+ # Run a command and get back the result.
8
+ #
9
+ # @param command [String] the command to run
10
+ # @param args [Array<String>] any arguments passed to the command
11
+ # @param [Hash] env: Pass environment variables to use. The key should
12
+ # be a String representing the environment variable name. The value
13
+ # is the value you want that variable to have.
14
+ #
15
+ # @yield [stdin] Handle any input on stdin that the command needs.
16
+ # @yieldparam stdin [IO]
17
+ #
18
+ # @example
19
+ # command_line('echo', 'hello')
20
+ #
21
+ # @example
22
+ # command_line('command_expecting_input') do |stdin|
23
+ # stdin.puts "first input"
24
+ # stdin.puts "second input"
25
+ # end
26
+ #
27
+ # @example
28
+ # command_line('some_webserver', { 'PORT' => '80' })
29
+ #
30
+ # @return [Result]
31
+ def command_line(*args, &block)
32
+ CommandLine.command_line(*args, &block)
33
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandLine
4
+ # The result returned from running {CommandLine.command_line}.
5
+ class Result
6
+ def initialize(stdout, stderr, status)
7
+ @stdout = stdout
8
+ @stderr = stderr
9
+ @status = status
10
+ end
11
+
12
+ # The complete contents of stdout after running the application.
13
+ #
14
+ # @example
15
+ # command_line('echo', 'hi').stdout
16
+ # # => "hi\n"
17
+ #
18
+ # @return [String]
19
+ attr_reader :stdout
20
+
21
+ # The complete contents of stderr after running the application.
22
+ #
23
+ # @example
24
+ # command_line('grep').stderr
25
+ # # => "usage: grep ..."
26
+ #
27
+ # @return [String]
28
+ attr_reader :stderr
29
+
30
+ # Returns true if the application exited normally.
31
+ #
32
+ # @example
33
+ # command_line('grep').exited?
34
+ #
35
+ # @return [Boolean]
36
+ def exited?
37
+ @status.exited?
38
+ end
39
+
40
+ # The numeric exit status of the application if it exited normally.
41
+ #
42
+ # @example
43
+ # command_line('echo', 'hi').exitstatus
44
+ # # => 0
45
+ # command_line('grep').exitstatus
46
+ # # => 2
47
+ #
48
+ # @return [Integer,nil]
49
+ def exitstatus
50
+ @status.exitstatus
51
+ end
52
+
53
+ # Returns true if the command exited normally with a success status.
54
+ #
55
+ # @example
56
+ # command_line('echo', 'hi').success?
57
+ # # => true
58
+ #
59
+ # @return [Boolean]
60
+ def success?
61
+ !@status.nil? && @status.success?
62
+ end
63
+
64
+ # Returns true if the command failed to exit normally or exited with a
65
+ # failing status.
66
+ #
67
+ # @example
68
+ # command_line('grep').failure?
69
+ # # => true
70
+ #
71
+ # @return [Boolean]
72
+ def failure?
73
+ !success?
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandLine
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_line
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Lasseigne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-16 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.75.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.75.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.20
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.20
83
+ description: An easier way execute command line applications and get all of the output.
84
+ email:
85
+ - aaron.lasseigne@dragonruby.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".editorconfig"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - ".travis.yml"
95
+ - CHANGELOG.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - appveyor.yml
101
+ - bin/console
102
+ - bin/setup
103
+ - command_line.gemspec
104
+ - lib/command_line.rb
105
+ - lib/command_line/global.rb
106
+ - lib/command_line/result.rb
107
+ - lib/command_line/version.rb
108
+ homepage: https://github.com/DragonRuby/command_line
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ homepage_uri: https://github.com/DragonRuby/command_line
113
+ source_code_uri: https://github.com/DragonRuby/command_line
114
+ changelog_uri: https://github.com/DragonRuby/command_line/blob/master/CHANGELOG.md
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '2.4'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.0.3
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: An easier way execute command line applications and get all of the output.
134
+ test_files: []