piotrb_cli_utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/piotrb_cli_utils.rb +15 -0
- data/lib/piotrb_cli_utils/cmd_loop.rb +24 -0
- data/lib/piotrb_cli_utils/cri_command_support.rb +33 -0
- data/lib/piotrb_cli_utils/shell_helpers.rb +56 -0
- data/lib/piotrb_cli_utils/util.rb +21 -0
- data/lib/piotrb_cli_utils/version.rb +3 -0
- data/piotrb_cli_utils.gemspec +33 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d3cb0debcbe966ad1de8f288ddefc096d7dfcd68fb89f84b7ace3a4b3f3b63a1
|
4
|
+
data.tar.gz: b2edfdfaa4a185947a8d6ec85217c4a6d298e1b82064d32ce05a36d41a19c4af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d26d350387060a6306ba609ab15f2c9f2509e1e847b5663e8d9700382161f1ec0aa8a3b7f565f19bab1a97d9564155044fd5df56d8a38ab24c840baeaffa5c0
|
7
|
+
data.tar.gz: 6d576945365c77c9bc91ab252917f3571aea7f03bd35175e0b3f25bd4755b5d03d5b47ca8c2b9383968fc03dcc6e2099be0a8f6a6bead836d38b45725eda6331
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Piotr Banasik
|
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,40 @@
|
|
1
|
+
# PiotrbCliUtils
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/piotrb_cli_utils`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'piotrb_cli_utils'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install piotrb_cli_utils
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/piotrb_cli_utils.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://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 "piotrb_cli_utils"
|
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,15 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
require 'tty-reader'
|
5
|
+
require 'cri'
|
6
|
+
require 'paint'
|
7
|
+
|
8
|
+
require_relative 'piotrb_cli_utils/version'
|
9
|
+
require_relative 'piotrb_cli_utils/cmd_loop'
|
10
|
+
require_relative 'piotrb_cli_utils/cri_command_support'
|
11
|
+
require_relative 'piotrb_cli_utils/shell_helpers'
|
12
|
+
require_relative 'piotrb_cli_utils/util'
|
13
|
+
|
14
|
+
module PiotrbCliUtils
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiotrbCliUtils
|
4
|
+
module CmdLoop
|
5
|
+
def run_cmd_loop(prompt = '=> ')
|
6
|
+
reader = TTY::Reader.new(interrupt: :noop)
|
7
|
+
reader.on(:keyctrl_c, :keyescape) do
|
8
|
+
return :abort
|
9
|
+
end
|
10
|
+
|
11
|
+
reader.on(:keyctrl_d) do
|
12
|
+
return :eof
|
13
|
+
end
|
14
|
+
|
15
|
+
catch(:stop) do
|
16
|
+
loop do
|
17
|
+
line = reader.read_line(prompt)
|
18
|
+
line.strip!
|
19
|
+
yield(line)
|
20
|
+
end
|
21
|
+
end || :stopped
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiotrbCliUtils
|
4
|
+
module CriCommandSupport
|
5
|
+
def define_cmd(name, summary: nil, help: false, &block)
|
6
|
+
cmd_name = name
|
7
|
+
Cri::Command.define do
|
8
|
+
name cmd_name
|
9
|
+
summary summary
|
10
|
+
option nil, :help, 'show help for this command'
|
11
|
+
run do |opts, args, cmd|
|
12
|
+
if opts[:help]
|
13
|
+
puts cmd.help
|
14
|
+
else
|
15
|
+
if block
|
16
|
+
block.call(opts, args, cmd)
|
17
|
+
elsif help
|
18
|
+
puts cmd.help
|
19
|
+
else
|
20
|
+
warn "no action defined for cmd: #{cmd.name}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def exit_cmd
|
28
|
+
define_cmd('exit') do |_opts, _args, _cmd|
|
29
|
+
throw :stop, :exit
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiotrbCliUtils
|
4
|
+
module ShellHelpers
|
5
|
+
def join_cmd(args)
|
6
|
+
if args.is_a? Array
|
7
|
+
Shellwords.join(args)
|
8
|
+
else
|
9
|
+
args
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def run_shell(command, return_status: false, echo_command: true, quiet: false, indent: 0)
|
14
|
+
command = join_cmd(command)
|
15
|
+
puts "#{' ' * indent}$> #{command}" if echo_command
|
16
|
+
command += ' 1>/dev/null 2>/dev/null' if quiet
|
17
|
+
system command.to_s
|
18
|
+
code = $CHILD_STATUS.exitstatus
|
19
|
+
raise("failed with code: #{code}") if !return_status && code > 0
|
20
|
+
|
21
|
+
code
|
22
|
+
end
|
23
|
+
|
24
|
+
def run_with_each_line(command)
|
25
|
+
command = join_cmd(command)
|
26
|
+
Open3.popen2e(command) do |_stdin, stdout_and_stderr, wait_thr|
|
27
|
+
pid = wait_thr.pid # pid of the started process.
|
28
|
+
until stdout_and_stderr.eof?
|
29
|
+
raw_line = stdout_and_stderr.gets
|
30
|
+
yield(raw_line)
|
31
|
+
end
|
32
|
+
wait_thr.value # Process::Status object returned.
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def capture_shell(command, error: true, echo_command: true, indent: 0, raise_on_error: false, detailed_result: false)
|
37
|
+
command = join_cmd(command)
|
38
|
+
puts "#{' ' * indent}<< #{command}" if echo_command
|
39
|
+
command += ' 2>/dev/null' unless error
|
40
|
+
value = `#{command}`
|
41
|
+
code = $CHILD_STATUS.exitstatus
|
42
|
+
if raise_on_error && code > 0
|
43
|
+
raise("capture_shell: #{command.inspect} failed with code: #{code}")
|
44
|
+
end
|
45
|
+
|
46
|
+
if detailed_result
|
47
|
+
OpenStruct.new({
|
48
|
+
status: code,
|
49
|
+
output: value
|
50
|
+
})
|
51
|
+
else
|
52
|
+
value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiotrbCliUtils
|
4
|
+
module Util
|
5
|
+
def fail_with(*messages, code: 1)
|
6
|
+
messages.each do |message|
|
7
|
+
warn(Paint[message, :red])
|
8
|
+
end
|
9
|
+
exit code
|
10
|
+
end
|
11
|
+
|
12
|
+
def log(message, depth: 0, newline: true)
|
13
|
+
message = Array(message)
|
14
|
+
message.each do |m|
|
15
|
+
indent = ' ' * depth
|
16
|
+
print indent + m
|
17
|
+
print "\n" if newline
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/piotrb_cli_utils/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "piotrb_cli_utils"
|
5
|
+
spec.version = PiotrbCliUtils::VERSION
|
6
|
+
spec.authors = ["Piotr Banasik"]
|
7
|
+
spec.email = ["piotr.banasik@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{CLI Utils for my command line scripts}
|
10
|
+
# spec.description = %q{}
|
11
|
+
spec.homepage = "https://github.com/piotr/cli_utils"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/piotr/cli_utils"
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "tty-reader"
|
31
|
+
spec.add_dependency "cri"
|
32
|
+
spec.add_dependency "paint"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piotrb_cli_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Banasik
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tty-reader
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: paint
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- piotr.banasik@gmail.com
|
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/piotrb_cli_utils.rb
|
70
|
+
- lib/piotrb_cli_utils/cmd_loop.rb
|
71
|
+
- lib/piotrb_cli_utils/cri_command_support.rb
|
72
|
+
- lib/piotrb_cli_utils/shell_helpers.rb
|
73
|
+
- lib/piotrb_cli_utils/util.rb
|
74
|
+
- lib/piotrb_cli_utils/version.rb
|
75
|
+
- piotrb_cli_utils.gemspec
|
76
|
+
homepage: https://github.com/piotr/cli_utils
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
homepage_uri: https://github.com/piotr/cli_utils
|
81
|
+
source_code_uri: https://github.com/piotr/cli_utils
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 2.3.0
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubygems_version: 3.0.3
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: CLI Utils for my command line scripts
|
101
|
+
test_files: []
|