verbose-shell 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/verbose-shell/version.rb +5 -0
- data/lib/verbose-shell.rb +86 -0
- data/verbose-shell.gemspec +28 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a663b642055003a2385b8b226e7ea1998a8b58f2
|
4
|
+
data.tar.gz: fc6deb09aa6c056b35c4df563692fb19fa9118fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 901addbdf0ff7287b55d9d8dac156f13620c7ee1b11a313f9650ddc40cecddfba5e52a1158ceaed5f7711956d89e4c4e7630e7c5700ab542af6f26b03f78dd07
|
7
|
+
data.tar.gz: a0874e66dcf32757d5beeb8d094b63bedf3365e770dd96b0b87cc2caf50cde2967b1d30c805fbb6ffbcdd3b2e434463e3f439771e21cadf12a273b2139e25a4b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 David Caldwell
|
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,86 @@
|
|
1
|
+
# VerboseShell
|
2
|
+
|
3
|
+
VerboseShell adds `-x`-like logging to FileUtils functions. Example:
|
4
|
+
|
5
|
+
require 'verbose-shell'
|
6
|
+
Vsh = VerboseShell
|
7
|
+
Vsh.system(*%W'ls -l')
|
8
|
+
Vsh.verbose = true
|
9
|
+
Vsh.system(*%W'echo Hello')
|
10
|
+
Vsh.cp('/etc/passwd', '/tmp/passwd')
|
11
|
+
|
12
|
+
This outputs:
|
13
|
+
|
14
|
+
+ echo Hello
|
15
|
+
Hello
|
16
|
+
+ cp /etc/passwd /tmp/passwd
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'verbose-shell'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install verbose-shell
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Set `VerboseShell.verbose` to `true` or `false`. The default is `false`, so
|
37
|
+
no logging will occur until it is changed.
|
38
|
+
|
39
|
+
All FileUtils functions are supported. They may not log correctly--if you
|
40
|
+
find one that doesn't log in a sane way, file a bug or (hopefully) a pull
|
41
|
+
request.
|
42
|
+
|
43
|
+
In addition, the following functions are available (all will log when
|
44
|
+
`verbose` is set):
|
45
|
+
|
46
|
+
- `VerboseShell.system(*args)`: Similar to Kernel.system except it only
|
47
|
+
accepts the multiple argument form (for safety). It is therefore never run
|
48
|
+
through the shell, and shell meta characters are unsupported (so no `|`,
|
49
|
+
`&&`, `||`, etc.). When `verbose` is not set, output will be captured and
|
50
|
+
stored in an exception if there is an error, or discarded if there is no
|
51
|
+
error. If `verbose` is set, output will display immediately and not be
|
52
|
+
captured (even on errors).
|
53
|
+
|
54
|
+
- `VerboseShell.system_or_true(*args)`: Similar to the above, but will never throw
|
55
|
+
exceptions.
|
56
|
+
|
57
|
+
- `VerboseShell.capture(*args)`: A wrapper around IO.popen(). Returns the
|
58
|
+
stdout of the command given. Like `VerboseShell.system()`, only the
|
59
|
+
multiple argument form is accepted (so no shell is involved).
|
60
|
+
|
61
|
+
- `which?(exe_name)`: Searches through `ENV['PATH']` and returns the full
|
62
|
+
path of exe_name if found, and `nil` if not found.
|
63
|
+
|
64
|
+
- `install_D(source, dest)`: Like `install -D` (see INSTALL(1)).
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. You
|
69
|
+
can also run `bin/console` for an interactive prompt that will allow you to
|
70
|
+
experiment.
|
71
|
+
|
72
|
+
To install this gem onto your local machine, run `bundle exec rake
|
73
|
+
install`. To release a new version, update the version number in
|
74
|
+
`version.rb`, and then run `bundle exec rake release`, which will create a
|
75
|
+
git tag for the version, push git commits and tags, and push the `.gem` file
|
76
|
+
to [rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Bug reports and pull requests are welcome on GitHub at
|
81
|
+
https://github.com/caldwell/verbose-shell.
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of
|
86
|
+
the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "verbose-shell"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'fileutils'
|
3
|
+
require 'pastel'
|
4
|
+
|
5
|
+
class VerboseShell
|
6
|
+
@verbose = 0
|
7
|
+
class << self
|
8
|
+
attr_accessor :verbose
|
9
|
+
def verbose=(val); @verbose = val == true ? 1 : (val || 0); end
|
10
|
+
|
11
|
+
def system_trace(*args)
|
12
|
+
return unless @verbose > 0
|
13
|
+
puts "+ "+args.map{|a| a =~ /\s/ ? '"'+a+'"' : a}.join(' ') # TODO: Fix quoting so it's more shell-like
|
14
|
+
end
|
15
|
+
|
16
|
+
class ShellError < RuntimeError
|
17
|
+
attr_accessor :output, :cmd, :exit_code
|
18
|
+
def initialize(output, cmd, exit_code)
|
19
|
+
@output = output
|
20
|
+
@cmd = cmd
|
21
|
+
@exit_code = exit_code
|
22
|
+
end
|
23
|
+
|
24
|
+
def message
|
25
|
+
"\n\n"+(['']*20+@output.split("\n"))[-20..-1].join("\n").lstrip + "\n"+Pastel.new.bright_white.on_red("💩 Command \"#{@cmd[0]}\" returned #{@exit_code}")
|
26
|
+
end
|
27
|
+
def to_s; message; end
|
28
|
+
end
|
29
|
+
|
30
|
+
def system(*args)
|
31
|
+
system_trace *args
|
32
|
+
if @verbose > 0
|
33
|
+
Kernel.system *args or raise ShellError.new('', args, $?.exitstatus).message.lstrip
|
34
|
+
else
|
35
|
+
output = IO.popen(args + [{:err => [:child, :out]}]) {|io| io.read}
|
36
|
+
raise ShellError.new(output, args, $?.exitstatus) if $? != 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def system_or_true(*args)
|
41
|
+
system *args
|
42
|
+
rescue
|
43
|
+
end
|
44
|
+
|
45
|
+
def capture(*args) # ``
|
46
|
+
system_trace *args
|
47
|
+
IO.popen(args + (@verbose == 0 ? [{:err => "/dev/null"}] : []), "r") {|io| io.read}.strip
|
48
|
+
end
|
49
|
+
|
50
|
+
def which?(exe)
|
51
|
+
system_trace 'which', exe
|
52
|
+
[exe, *ENV['PATH'].split(File::PATH_SEPARATOR).map {|p| File.join(p, exe)}].find {|f| File.executable?(f)}
|
53
|
+
end
|
54
|
+
|
55
|
+
def install_D(source, dest)
|
56
|
+
system_trace 'install', '-D', source, dest
|
57
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
58
|
+
FileUtils.install(source, dest)
|
59
|
+
end
|
60
|
+
|
61
|
+
def method_to_a(method)
|
62
|
+
a = method.to_s.split('_')
|
63
|
+
a[1] = "-#{a[1]}" if a[1]
|
64
|
+
a
|
65
|
+
end
|
66
|
+
|
67
|
+
def chmod_formatter(method, mode, file)
|
68
|
+
method_to_a(method) + [mode.class == String ? mode : sprintf("%04o", mode)] + (file.class == Array ? file : [file])
|
69
|
+
end
|
70
|
+
def chmod_R_formatter(method, mode, file)
|
71
|
+
chmod_formatter(method, mode, file)
|
72
|
+
end
|
73
|
+
def chown_formatter(method, user, group, file)
|
74
|
+
method_to_a(method) + [[user,group && ":#{group}"].select{|x|x}.join('')] + (file.class == Array ? file : [file])
|
75
|
+
end
|
76
|
+
def chown_R_formatter(method, user, group, file)
|
77
|
+
chown_formatter(method, user, group, file)
|
78
|
+
end
|
79
|
+
|
80
|
+
def method_missing(method, *args, &block)
|
81
|
+
system_trace self.respond_to?("#{method}_formatter") ? self.send("#{method}_formatter", method, *args)
|
82
|
+
: method_to_a(method) + args
|
83
|
+
FileUtils.send(method, *args, &block)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "verbose-shell/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "verbose-shell"
|
8
|
+
spec.version = Verbose::Shell::VERSION
|
9
|
+
spec.authors = ["David Caldwell"]
|
10
|
+
spec.email = ["david@porkrind.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{sh -x like logging of FileUtils functions.}
|
13
|
+
spec.description = %q{sh -x like logging of FileUtils functions.}
|
14
|
+
spec.homepage = 'https://github.com/caldwell/verbose-shell'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'pastel', '~> 0.7.2'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: verbose-shell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Caldwell
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pastel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.7.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: sh -x like logging of FileUtils functions.
|
56
|
+
email:
|
57
|
+
- david@porkrind.org
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/verbose-shell.rb
|
70
|
+
- lib/verbose-shell/version.rb
|
71
|
+
- verbose-shell.gemspec
|
72
|
+
homepage: https://github.com/caldwell/verbose-shell
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.5.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: sh -x like logging of FileUtils functions.
|
96
|
+
test_files: []
|