base_script 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91535aa854da3d358a28b6b7e4884289660acbaf
4
+ data.tar.gz: f9193287d1d234469f4e3f4d3d0f8e3d23d683da
5
+ SHA512:
6
+ metadata.gz: 8fa846e22950009fcb784386cd5a1252cec899bf619eec6615993e2559fe374d47c674ae4c801f5e77ad872bb648639ea7a07b1e1149027067862379412b3c50
7
+ data.tar.gz: 13d62f528063361c76f0c96af574640498bed0db4b55841b3e152b8bb1e5f21fd34657918137a773357722e43c32b347d416542c71cea53820ad1fd8de9cbbb2
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # See: base_script.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Paul Annesley
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,60 @@
1
+ # BaseScript
2
+
3
+ Small base for CLI scripts; signal handling, indented logging, colors
4
+ ticks/crosses, injectable args/IO.
5
+
6
+ It was kicking around the `lib/` directory of various Rails projects I've
7
+ built. Now it's a gem with a [version number][semver].
8
+
9
+
10
+ ## Installation
11
+
12
+ With bundler:
13
+
14
+ $ echo 'gem "base_script"' >> Gemfile
15
+ $ bundle
16
+
17
+ Manually:
18
+
19
+ $ gem install base_script
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "base_script"
25
+
26
+ class HelloScript < BaseScript
27
+
28
+ def run
29
+ if arg("lunar")
30
+ log "hello moon"
31
+ else
32
+ log "hello world"
33
+ end
34
+
35
+ log "Doing some work, ctrl-c to cleanly interrupt.."
36
+ indented do
37
+ 100.times do |i|
38
+ exit_on_signals
39
+ vlog "Step #{i}"
40
+ do_some_work() unless dry?
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( http://github.com/<my-github-username>/base_script/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
55
+
56
+ Author: [Paul Annesley][pda]
57
+
58
+
59
+ [semver]: http://semver.org/
60
+ [pda]: https://twitter.com/pda
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'base_script/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "base_script"
8
+ spec.version = BaseScript::VERSION
9
+ spec.authors = ["Paul Annesley"]
10
+ spec.email = ["paul@annesley.cc"]
11
+ spec.summary = %q{CLI script simple base class.}
12
+ spec.description = %q{Small base for CLI scripts; signal handling, indented logging, colors ticks/crosses, injectable args/IO.}
13
+ spec.homepage = "https://github.com/pda/base_script"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module BaseScript
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,133 @@
1
+ require "base_script/version"
2
+
3
+ # A base class for implementing CLI scripts.
4
+ # ARGV and in/out IO's are injected, so can be mocked & tested.
5
+ # Basic signal handling by calling exit_on_signals inside work loops etc.
6
+ # Requires Ruby 2.0.0+ for keyword args etc.
7
+ class BaseScript
8
+
9
+ EXIT_SUCCESS = 0
10
+ INDENT = " "
11
+
12
+ def initialize(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr)
13
+ @argv = argv
14
+
15
+ @input = stdin
16
+ @output = stdout
17
+ @error_output = stderr
18
+ sync_io!
19
+
20
+ @indentation = 0
21
+ end
22
+
23
+ private
24
+
25
+ # I/O
26
+ attr_reader :input
27
+ attr_reader :output
28
+ attr_reader :error_output
29
+
30
+ # Set I/O streams as unbuffered if they support it.
31
+ def sync_io!
32
+ [input, output, error_output].each do |io|
33
+ io.sync = true if io.respond_to?(:sync=)
34
+ end
35
+ end
36
+
37
+ ##
38
+ # Argument handling.
39
+
40
+ def args
41
+ @_args ||= @argv.reduce({}) do |memo, argument|
42
+ key, value = argument.split("=", 2)
43
+ option = key.gsub(/\A-+/, "") # strip leading hyphens.
44
+ memo[option] = value || true # store as true for value-less options.
45
+ memo
46
+ end
47
+ end
48
+
49
+ # Fetch a --key or --key=value argument.
50
+ # Returns the provided default if not set.
51
+ def arg(key, default = nil)
52
+ args.fetch(key.to_s, default)
53
+ end
54
+
55
+ # Like #arg, but raises KeyError if missing.
56
+ def arg!(key)
57
+ args.fetch(key.to_s) do
58
+ raise KeyError, "--#{key} argument required"
59
+ end
60
+ end
61
+
62
+ def dry?; arg("dry-run") end
63
+ def verbose?; arg("v") end
64
+
65
+ ##
66
+ # Logging.
67
+
68
+ def log(message)
69
+ message += "\n" unless message[-1] == ?\n
70
+ output << indent_string(message)
71
+ end
72
+
73
+ def vlog(message)
74
+ log(message) if verbose?
75
+ end
76
+
77
+ def indented
78
+ @indentation += 1
79
+ yield
80
+ ensure
81
+ @indentation -= 1
82
+ end
83
+
84
+ def indent_string(content)
85
+ spaces = INDENT * @indentation
86
+ content.each_line.map {|line| "#{spaces}#{line}" }.join
87
+ end
88
+
89
+ # Colorize text if output is a tty.
90
+ def colorize(text, code)
91
+ if output.respond_to?(:tty?) && output.tty?
92
+ "\033[#{code}m#{text}\033[0m"
93
+ else
94
+ text
95
+ end
96
+ end
97
+
98
+ # A green tick.
99
+ def tick; colorize("✔", 32) end
100
+
101
+ # A red cross.
102
+ def cross; colorize("✘", 31) end
103
+
104
+ ##
105
+ # Signal handling.
106
+
107
+ # Call this method prior to doing work inside a loop.
108
+ # Alternatively, call at start of script to install handlers, and
109
+ # then at safe-exit points throughout script.
110
+ # Don't set up signal handlers (first call) and then fail to call again.
111
+ def exit_on_signals
112
+ install_signal_handlers unless defined?(@_signal)
113
+
114
+ if @_signal
115
+ log "Exiting due to SIG#{@_signal}"
116
+ exit(1)
117
+ end
118
+ end
119
+
120
+ def install_signal_handlers
121
+ @_signal = nil
122
+ @_previous_signal_handlers = {}
123
+ %w{INT TERM}.each do |signal|
124
+ log "Installing #{signal} handler" if verbose?
125
+ @_previous_signal_handlers[signal] = Signal.trap(signal) do
126
+ log "Received SIG#{signal}, will exit at next opportunity"
127
+ @_signal = signal
128
+ Signal.trap(signal, @_previous_signal_handlers[signal])
129
+ end
130
+ end
131
+ end
132
+
133
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base_script
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Annesley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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
+ description: Small base for CLI scripts; signal handling, indented logging, colors
42
+ ticks/crosses, injectable args/IO.
43
+ email:
44
+ - paul@annesley.cc
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - base_script.gemspec
55
+ - lib/base_script.rb
56
+ - lib/base_script/version.rb
57
+ homepage: https://github.com/pda/base_script
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: CLI script simple base class.
81
+ test_files: []